mirror of
https://github.com/grocy/grocy.git
synced 2026-04-06 21:06:15 +02:00
Add error handling.
This commit is contained in:
parent
e3aa78280d
commit
b242676488
5
app.php
5
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();
|
||||
|
|
|
|||
53
controllers/ExceptionController.php
Normal file
53
controllers/ExceptionController.php
Normal 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
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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 ""
|
||||
|
|
|
|||
12
views/errors/403.blade.php
Normal file
12
views/errors/403.blade.php
Normal 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
|
||||
3
views/errors/404.blade.php
Normal file
3
views/errors/404.blade.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
@extends('errors.base')
|
||||
|
||||
@section('title', $__t('Page not found'))
|
||||
3
views/errors/500.blade.php
Normal file
3
views/errors/500.blade.php
Normal 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!'))
|
||||
13
views/errors/base.blade.php
Normal file
13
views/errors/base.blade.php
Normal 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
|
||||
Loading…
Reference in New Issue
Block a user