From b24267648834c174e6124d4d130d7dc680100de5 Mon Sep 17 00:00:00 2001 From: fipwmaqzufheoxq92ebc <29818044+fipwmaqzufheoxq92ebc@users.noreply.github.com> Date: Tue, 25 Aug 2020 18:34:27 +0200 Subject: [PATCH] Add error handling. --- app.php | 5 ++- controllers/ExceptionController.php | 53 +++++++++++++++++++++++++++++ localization/strings.pot | 6 ++++ views/errors/403.blade.php | 12 +++++++ views/errors/404.blade.php | 3 ++ views/errors/500.blade.php | 3 ++ views/errors/base.blade.php | 13 +++++++ 7 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 controllers/ExceptionController.php create mode 100644 views/errors/403.blade.php create mode 100644 views/errors/404.blade.php create mode 100644 views/errors/500.blade.php create mode 100644 views/errors/base.blade.php 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') + +
+
+

@yield('title')

+
+
+@stop diff --git a/views/errors/404.blade.php b/views/errors/404.blade.php new file mode 100644 index 00000000..247aa3e9 --- /dev/null +++ b/views/errors/404.blade.php @@ -0,0 +1,3 @@ +@extends('errors.base') + +@section('title', $__t('Page not found')) diff --git a/views/errors/500.blade.php b/views/errors/500.blade.php new file mode 100644 index 00000000..7ee4fa04 --- /dev/null +++ b/views/errors/500.blade.php @@ -0,0 +1,3 @@ +@extends('errors.base') + +@section('title', $__t('A server error occured while processing your request. You might found a bug in grocy!')) diff --git a/views/errors/base.blade.php b/views/errors/base.blade.php new file mode 100644 index 00000000..c4dec078 --- /dev/null +++ b/views/errors/base.blade.php @@ -0,0 +1,13 @@ +@extends('layout.default') + +@section('content') + +
+
+

@yield('title')

+
+ {!! nl2br(e($exception->getTraceAsString())) !!} +
+
+
+@stop