diff --git a/app.php b/app.php index c036c41b..7ff895d1 100644 --- a/app.php +++ b/app.php @@ -65,6 +65,9 @@ if (!empty(GROCY_BASE_PATH)) // Add default middleware $app->addRoutingMiddleware(); -$app->addErrorMiddleware(true, false, false); +$errorMiddleware = $app->addErrorMiddleware(true, false, false); +$errorMiddleware->setDefaultErrorHandler( + new \Grocy\Controllers\ExceptionController($app, $container) +); $app->run(); diff --git a/controllers/ExceptionController.php b/controllers/ExceptionController.php new file mode 100644 index 00000000..a5207823 --- /dev/null +++ b/controllers/ExceptionController.php @@ -0,0 +1,53 @@ +app = $app; + } + + public function __invoke(ServerRequestInterface $request, + Throwable $exception, + bool $displayErrorDetails, + bool $logErrors, + bool $logErrorDetails, + ?LoggerInterface $logger = null) + { + $response = $this->app->getResponseFactory()->createResponse(); + if ($exception instanceof HttpNotFoundException) { + return $this->renderPage($response->withStatus(404), 'errors/404', [ + 'exception' => $exception + ]); + } + if ($exception instanceof HttpForbiddenException) { + return $this->renderPage($response->withStatus(403), 'errors/403', [ + 'exception' => $exception + ]); + } + + return $this->renderPage($response->withStatus(500), 'errors/500', [ + 'exception' => $exception + ]); + + } +} \ No newline at end of file diff --git a/localization/strings.pot b/localization/strings.pot index 7d4469f5..628432c6 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -1936,3 +1936,9 @@ msgstr "" msgid "You are not allowed to view this page!" msgstr "" + +msgid "A server error occured while processing your request. You might found a bug in grocy!" +msgstr "" + +msgid "Page not found" +msgstr "" diff --git a/views/errors/403.blade.php b/views/errors/403.blade.php new file mode 100644 index 00000000..2a88f73e --- /dev/null +++ b/views/errors/403.blade.php @@ -0,0 +1,12 @@ +@extends('errors.base') + +@section('title', $__t('You are not allowed to view this page!')) + +@section('content') + +