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
This commit is contained in:
Tony Murray
2020-05-28 14:39:19 -05:00
committed by GitHub
parent b2d6540ff2
commit 8b37eaf752
7 changed files with 107 additions and 9 deletions

View File

@@ -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',

View File

@@ -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',

View File

@@ -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',

View File

@@ -0,0 +1,73 @@
<?php
/**
* MaximumExecutionTimeExceeded.php
*
* Show nice explanation if the user hits their configured PHP max_execution_time
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
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,
]);
}
}

View File

@@ -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',

View File

@@ -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) {

View File

@@ -0,0 +1,22 @@
<?php
return [
'database_connect' => [
'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)'
],
];