Improved error handling

This commit is contained in:
Sergio Brighenti 2019-02-10 13:48:20 +01:00
parent 4f7c91cc3b
commit bb0e3c80e7
4 changed files with 52 additions and 19 deletions

View file

@ -5,6 +5,7 @@
+ Improved video.js alignment with large videos. + Improved video.js alignment with large videos.
+ Optimized output zip release size. + Optimized output zip release size.
+ Templates cleanup and optimizations. + Templates cleanup and optimizations.
+ Improved error handling.
## v2.4.1 ## v2.4.1
+ Fixed error message when the file is too large. (#15) + Fixed error message when the file is too large. (#15)

View file

@ -25,14 +25,14 @@ $config = array_replace_recursive([
'maintenance' => false, 'maintenance' => false,
'db' => [ 'db' => [
'connection' => 'sqlite', 'connection' => 'sqlite',
'dsn' => __DIR__ . '/../resources/database/xbackbone.db', 'dsn' => BASE_DIR . 'resources/database/xbackbone.db',
'username' => null, 'username' => null,
'password' => null, 'password' => null,
], ],
], require __DIR__ . '/../config.php'); ], require BASE_DIR . 'config.php');
if (!$config['displayErrorDetails']) { if (!$config['displayErrorDetails']) {
$config['routerCacheFile'] = __DIR__ . '/../resources/cache/routes.cache.php'; $config['routerCacheFile'] = BASE_DIR . 'resources/cache/routes.cache.php';
} }
$container = new Container(['settings' => $config]); $container = new Container(['settings' => $config]);
@ -44,7 +44,7 @@ $container['config'] = function ($container) use ($config) {
$container['logger'] = function ($container) { $container['logger'] = function ($container) {
$logger = new Logger('app'); $logger = new Logger('app');
$streamHandler = new RotatingFileHandler(__DIR__ . '/../logs/log.txt', 10, Logger::DEBUG); $streamHandler = new RotatingFileHandler(BASE_DIR . 'logs/log.txt', 10, Logger::DEBUG);
$streamHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", "Y-m-d H:i:s", true)); $streamHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", "Y-m-d H:i:s", true));
$logger->pushHandler($streamHandler); $logger->pushHandler($streamHandler);
@ -53,21 +53,21 @@ $container['logger'] = function ($container) {
}; };
$container['session'] = function ($container) { $container['session'] = function ($container) {
return new Session('xbackbone_session', __DIR__ . '/../resources/sessions'); return new Session('xbackbone_session', BASE_DIR . 'resources/sessions');
}; };
$container['database'] = function ($container) use (&$config) { $container['database'] = function ($container) use (&$config) {
$dsn = $config['db']['connection'] === 'sqlite' ? __DIR__ . '/../' . $config['db']['dsn'] : $config['db']['dsn']; $dsn = $config['db']['connection'] === 'sqlite' ? BASE_DIR . $config['db']['dsn'] : $config['db']['dsn'];
return new DB($config['db']['connection'] . ':' . $dsn, $config['db']['username'], $config['db']['password']); return new DB($config['db']['connection'] . ':' . $dsn, $config['db']['username'], $config['db']['password']);
}; };
$container['lang'] = function ($container) { $container['lang'] = function ($container) {
return Lang::build(Lang::recognize(), __DIR__ . '/../resources/lang/'); return Lang::build(Lang::recognize(), BASE_DIR . 'resources/lang/');
}; };
$container['view'] = function ($container) use (&$config) { $container['view'] = function ($container) use (&$config) {
$view = new \Slim\Views\Twig(__DIR__ . '/../resources/templates', [ $view = new \Slim\Views\Twig(BASE_DIR . 'resources/templates', [
'cache' => __DIR__ . '/../resources/cache', 'cache' => BASE_DIR . 'resources/cache',
'autoescape' => 'html', 'autoescape' => 'html',
'debug' => $config['displayErrorDetails'], 'debug' => $config['displayErrorDetails'],
'auto_reload' => $config['displayErrorDetails'], 'auto_reload' => $config['displayErrorDetails'],
@ -93,8 +93,15 @@ $container['view'] = function ($container) use (&$config) {
return $view; return $view;
}; };
$container['phpErrorHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, \Throwable $error) use (&$container) {
$container->logger->critical('Fatal runtime error during app execution', [$error, $error->getTraceAsString()]);
return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $error]);
};
};
$container['errorHandler'] = function ($container) { $container['errorHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $exception) use (&$container) { return function (\Slim\Http\Request $request, \Slim\Http\Response $response, \Exception $exception) use (&$container) {
if ($exception instanceof \App\Exceptions\MaintenanceException) { if ($exception instanceof \App\Exceptions\MaintenanceException) {
return $container->view->render($response->withStatus(503), 'errors/maintenance.twig'); return $container->view->render($response->withStatus(503), 'errors/maintenance.twig');
@ -104,11 +111,17 @@ $container['errorHandler'] = function ($container) {
return $container->view->render($response->withStatus(403), 'errors/403.twig'); return $container->view->render($response->withStatus(403), 'errors/403.twig');
} }
$container->logger->critical('Fatal error during app execution', [$exception, $exception->getTraceAsString()]); $container->logger->critical('Fatal exception during app execution', [$exception, $exception->getTraceAsString()]);
return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $exception]); return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $exception]);
}; };
}; };
$container['notAllowedHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $methods) use (&$container) {
return $container->view->render($response->withStatus(405)->withHeader('Allow', implode(', ', $methods)), 'errors/405.twig');
};
};
$container['notFoundHandler'] = function ($container) { $container['notFoundHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) { return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) {
$response->withStatus(404)->withHeader('Content-Type', 'text/html'); $response->withStatus(404)->withHeader('Content-Type', 'text/html');

View file

@ -28,16 +28,21 @@ location /logs {
return 403; return 403;
} }
index index.html index.htm index.php;
charset utf-8;
location / { location / {
try_files $uri /index.php$is_args$args; try_files $uri $uri/ /index.php?$query_string;
} }
location ~ \.php { error_page 404 /index.php;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$; location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
} }

View file

@ -0,0 +1,14 @@
{% extends 'base.twig' %}
{% block title %}Method Not Allowed{% endblock %}
{% block content %}
<div class="container-fluid mt-5">
<div class="text-center">
<h1 class="display-1">405 Method Not Allowed</h1>
<p class="lead">The method received in the request-line is not supported by the target resource.</p>
</div>
</div>
{% endblock %}
{% block footer %}{% endblock %}