Add error handling.

This commit is contained in:
fipwmaqzufheoxq92ebc 2020-08-25 18:34:27 +02:00
parent e3aa78280d
commit b242676488
7 changed files with 94 additions and 1 deletions

View File

@ -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();

View File

@ -0,0 +1,53 @@
<?php
namespace Grocy\Controllers;
use Exception;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpForbiddenException;
use Slim\Exception\HttpInternalServerErrorException;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpSpecializedException;
use Throwable;
class ExceptionController extends BaseController
{
/**
* @var \Slim\App
*/
private $app;
public function __construct(\Slim\App $app, \DI\Container $container)
{
parent::__construct($container);
$this->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
]);
}
}

View File

@ -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 ""

View File

@ -0,0 +1,12 @@
@extends('errors.base')
@section('title', $__t('You are not allowed to view this page!'))
@section('content')
<meta http-equiv="refresh" content="5;url=/">
<div class="row">
<div class="col-xs-12 col-md-6 text-center">
<h2 class="title">@yield('title')</h2>
</div>
</div>
@stop

View File

@ -0,0 +1,3 @@
@extends('errors.base')
@section('title', $__t('Page not found'))

View File

@ -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!'))

View File

@ -0,0 +1,13 @@
@extends('layout.default')
@section('content')
<meta http-equiv="refresh" content="5;url=/">
<div class="row">
<div class="col-xs-12 col-md-6">
<h2 class="title">@yield('title')</h2>
<div>
{!! nl2br(e($exception->getTraceAsString())) !!}
</div>
</div>
</div>
@stop