From bb0e3c80e711ee0aae43ebc41ef0c59edf029270 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Sun, 10 Feb 2019 13:48:20 +0100 Subject: [PATCH] Improved error handling --- CHANGELOG.md | 1 + bootstrap/app.php | 35 ++++++++++++++++++++--------- nginx.conf | 21 ++++++++++------- resources/templates/errors/405.twig | 14 ++++++++++++ 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 resources/templates/errors/405.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index d030e0e..8062bf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ + Improved video.js alignment with large videos. + Optimized output zip release size. + Templates cleanup and optimizations. ++ Improved error handling. ## v2.4.1 + Fixed error message when the file is too large. (#15) diff --git a/bootstrap/app.php b/bootstrap/app.php index c1ab7fb..312e072 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -25,14 +25,14 @@ $config = array_replace_recursive([ 'maintenance' => false, 'db' => [ 'connection' => 'sqlite', - 'dsn' => __DIR__ . '/../resources/database/xbackbone.db', + 'dsn' => BASE_DIR . 'resources/database/xbackbone.db', 'username' => null, 'password' => null, ], -], require __DIR__ . '/../config.php'); +], require BASE_DIR . 'config.php'); 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]); @@ -44,7 +44,7 @@ $container['config'] = function ($container) use ($config) { $container['logger'] = function ($container) { $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)); $logger->pushHandler($streamHandler); @@ -53,21 +53,21 @@ $container['logger'] = 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) { - $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']); }; $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) { - $view = new \Slim\Views\Twig(__DIR__ . '/../resources/templates', [ - 'cache' => __DIR__ . '/../resources/cache', + $view = new \Slim\Views\Twig(BASE_DIR . 'resources/templates', [ + 'cache' => BASE_DIR . 'resources/cache', 'autoescape' => 'html', 'debug' => $config['displayErrorDetails'], 'auto_reload' => $config['displayErrorDetails'], @@ -93,8 +93,15 @@ $container['view'] = function ($container) use (&$config) { 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) { - 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) { 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'); } - $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]); }; }; +$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) { return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) { $response->withStatus(404)->withHeader('Content-Type', 'text/html'); diff --git a/nginx.conf b/nginx.conf index 0b4ff1f..4df88bf 100644 --- a/nginx.conf +++ b/nginx.conf @@ -28,16 +28,21 @@ location /logs { return 403; } +index index.html index.htm index.php; + +charset utf-8; + location / { - try_files $uri /index.php$is_args$args; + try_files $uri $uri/ /index.php?$query_string; } -location ~ \.php { - try_files $uri =404; - fastcgi_split_path_info ^(.+\.php)(/.+)$; - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_param SCRIPT_NAME $fastcgi_script_name; - fastcgi_index index.php; +error_page 404 /index.php; + +location ~ \.php$ { 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; } \ No newline at end of file diff --git a/resources/templates/errors/405.twig b/resources/templates/errors/405.twig new file mode 100644 index 0000000..ab95c2a --- /dev/null +++ b/resources/templates/errors/405.twig @@ -0,0 +1,14 @@ +{% extends 'base.twig' %} + +{% block title %}Method Not Allowed{% endblock %} + +{% block content %} +
+
+

405 Method Not Allowed

+

The method received in the request-line is not supported by the target resource.

+
+
+{% endblock %} + +{% block footer %}{% endblock %} \ No newline at end of file