From 8b37eaf752d824736cfa70dc1e88bf5dd82c4834 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Thu, 28 May 2020 14:39:19 -0500 Subject: [PATCH] Maximum Execution Time Exceeded show error (#11720) * Maximum Execution Time Exceeded show error Migrate translations to php translation files. Fix invalid view class in Handler * Update MaximumExecutionTimeExceeded.php * Update MaximumExecutionTimeExceeded.php --- .../Exceptions/DatabaseConnectException.php | 2 +- LibreNMS/Exceptions/DuskUnsafeException.php | 4 +- LibreNMS/Exceptions/LdapMissingException.php | 8 +- .../MaximumExecutionTimeExceeded.php | 73 +++++++++++++++++++ .../Exceptions/UnserializableRouteCache.php | 4 +- app/Exceptions/Handler.php | 3 +- resources/lang/en/exceptions.php | 22 ++++++ 7 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 LibreNMS/Exceptions/MaximumExecutionTimeExceeded.php create mode 100644 resources/lang/en/exceptions.php diff --git a/LibreNMS/Exceptions/DatabaseConnectException.php b/LibreNMS/Exceptions/DatabaseConnectException.php index 977c1d80f8..9afd884d4d 100644 --- a/LibreNMS/Exceptions/DatabaseConnectException.php +++ b/LibreNMS/Exceptions/DatabaseConnectException.php @@ -56,7 +56,7 @@ class DatabaseConnectException extends \Exception implements UpgradeableExceptio */ public function render(\Illuminate\Http\Request $request) { - $title = __('Error connecting to database'); + $title = trans('exceptions.database_connect.title'); return $request->wantsJson() ? response()->json([ 'status' => 'error', diff --git a/LibreNMS/Exceptions/DuskUnsafeException.php b/LibreNMS/Exceptions/DuskUnsafeException.php index 3c73112b1b..74128bb936 100644 --- a/LibreNMS/Exceptions/DuskUnsafeException.php +++ b/LibreNMS/Exceptions/DuskUnsafeException.php @@ -50,8 +50,8 @@ class DuskUnsafeException extends \Exception implements UpgradeableException */ public function render(\Illuminate\Http\Request $request) { - $title = __('It is unsafe to run Dusk in production'); - $message = __('Run ":command" to remove Dusk or if you are a developer set the appropriate APP_ENV', ['command' => './scripts/composer_wrapper.php install --no-dev']); + $title = trans('exceptions.dusk_unsafe.title'); + $message = trans('exceptions.dusk_unsafe.message', ['command' => './scripts/composer_wrapper.php install --no-dev']); return $request->wantsJson() ? response()->json([ 'status' => 'error', diff --git a/LibreNMS/Exceptions/LdapMissingException.php b/LibreNMS/Exceptions/LdapMissingException.php index c4545689c4..a8a9d02695 100644 --- a/LibreNMS/Exceptions/LdapMissingException.php +++ b/LibreNMS/Exceptions/LdapMissingException.php @@ -27,8 +27,10 @@ namespace LibreNMS\Exceptions; class LdapMissingException extends AuthenticationException { + private const DEFAULT_MESSAGE = 'PHP does not support LDAP, please install or enable the PHP LDAP extension'; + public function __construct( - string $message = 'PHP does not support LDAP, please install or enable the PHP LDAP extension', + string $message = self::DEFAULT_MESSAGE, int $code = 0, \Exception $previous = null ) { @@ -43,8 +45,8 @@ class LdapMissingException extends AuthenticationException */ public function render(\Illuminate\Http\Request $request) { - $title = __('PHP LDAP support missing'); - $message = __($this->getMessage()); + $title = trans('exceptions.ldap_missing.title'); + $message = ($this->message == self::DEFAULT_MESSAGE) ? trans('exceptions.ldap_missing.message') : $this->getMessage(); return $request->wantsJson() ? response()->json([ 'status' => 'error', diff --git a/LibreNMS/Exceptions/MaximumExecutionTimeExceeded.php b/LibreNMS/Exceptions/MaximumExecutionTimeExceeded.php new file mode 100644 index 0000000000..15bfd7d18f --- /dev/null +++ b/LibreNMS/Exceptions/MaximumExecutionTimeExceeded.php @@ -0,0 +1,73 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Exceptions; + +use LibreNMS\Interfaces\Exceptions\UpgradeableException; +use Illuminate\Support\Str; +use Symfony\Component\Debug\Exception\FatalErrorException; + +class MaximumExecutionTimeExceeded extends \Exception implements UpgradeableException +{ + /** + * Try to convert the given Exception to a FilePermissionsException + * + * @param \Exception $exception + * @return static + */ + public static function upgrade($exception) + { + // cannot write to storage directory + if ($exception instanceof FatalErrorException && + Str::startsWith($exception->getMessage(), 'Maximum execution time of')) { + return new static($exception->getMessage(), $exception->getCode(), $exception); + } + + return null; + } + + /** + * Render the exception into an HTTP or JSON response. + * + * @param \Illuminate\Http\Request + * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response + */ + public function render(\Illuminate\Http\Request $request) + { + $title = preg_match('/ (\d+) /', $this->message, $matches) + ? trans_choice('exceptions.maximum_execution_time_exceeded.title', $matches[1], ['seconds' => $matches[1]]) + : $this->getMessage(); + + $message = trans('exceptions.maximum_execution_time_exceeded.message'); + + return $request->wantsJson() ? response()->json([ + 'status' => 'error', + 'message' => "$title: $message", + ]) : response()->view('errors.generic', [ + 'title' => $title, + 'content' => $message, + ]); + } +} diff --git a/LibreNMS/Exceptions/UnserializableRouteCache.php b/LibreNMS/Exceptions/UnserializableRouteCache.php index 41d14bd3f5..5cfc9d9325 100644 --- a/LibreNMS/Exceptions/UnserializableRouteCache.php +++ b/LibreNMS/Exceptions/UnserializableRouteCache.php @@ -67,8 +67,8 @@ class UnserializableRouteCache extends \Exception implements UpgradeableExceptio */ public function render(\Illuminate\Http\Request $request) { - $title = __('Error caused by PHP version mismatch'); - $message = __('The version of PHP your webserver is running (:web_version) does not match the CLI version (:cli_version).', ['cli_version' => $this->cli_php_version, 'web_version' => $this->web_php_version]); + $title = trans('exceptions.unserializable_route_cache.title'); + $message = trans('exceptions.unserializable_route_cache.message', ['cli_version' => $this->cli_php_version, 'web_version' => $this->web_php_version]); return $request->wantsJson() ? response()->json([ 'status' => 'error', diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 94e80c3b78..f900b98b26 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -32,6 +32,7 @@ class Handler extends ExceptionHandler \LibreNMS\Exceptions\DatabaseConnectException::class, \LibreNMS\Exceptions\DuskUnsafeException::class, \LibreNMS\Exceptions\UnserializableRouteCache::class, + \LibreNMS\Exceptions\MaximumExecutionTimeExceeded::class, ]; public function render($request, Exception $exception) @@ -39,7 +40,7 @@ class Handler extends ExceptionHandler // If for some reason Blade hasn't been registered, try it now try { if (!app()->bound('view')) { - app()->register(\App\Providers\ViewServiceProvider::class); + app()->register(\Illuminate\View\ViewServiceProvider::class); app()->register(\Illuminate\Translation\TranslationServiceProvider::class); } } catch (\Exception $e) { diff --git a/resources/lang/en/exceptions.php b/resources/lang/en/exceptions.php new file mode 100644 index 0000000000..8582b9b489 --- /dev/null +++ b/resources/lang/en/exceptions.php @@ -0,0 +1,22 @@ + [ + 'title' => 'Error connecting to database' + ], + 'dusk_unsafe' => [ + 'title' => 'It is unsafe to run Dusk in production', + 'message' => 'Run ":command" to remove Dusk or if you are a developer set the appropriate APP_ENV' + ], + 'ldap_missing' => [ + 'title' => 'PHP LDAP support missing', + 'message' => 'PHP does not support LDAP, please install or enable the PHP LDAP extension' + ], + 'maximum_execution_time_exceeded' => [ + 'title' => 'Maximum execution time of :seconds second exceeded|Maximum execution time of :seconds seconds exceeded', + 'message' => 'Page load exceeded your maximum execution time configured in PHP. Either increase max_execution_time in your php.ini or improve server hardware' + ], + 'unserializable_route_cache' => [ + 'title' => 'Error caused by PHP version mismatch', + 'message' => 'The version of PHP your web server is running (:web_version) does not match the CLI version (:cli_version)' + ], +];