Refactor error controller

This commit is contained in:
Pierre Rudloff 2019-12-02 22:04:14 +01:00
parent f2717772a6
commit ef493074d4
3 changed files with 18 additions and 45 deletions

View file

@ -17,7 +17,7 @@ use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
use Slim\Views\Smarty; use Slim\Views\Smarty;
use Symfony\Component\Debug\ExceptionHandler; use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError; use Symfony\Component\Debug\Exception\FlattenException;
/** /**
* Main controller. * Main controller.
@ -233,62 +233,40 @@ class FrontController extends BaseController
* *
* @param Request $request PSR-7 request * @param Request $request PSR-7 request
* @param Response $response PSR-7 response * @param Response $response PSR-7 response
* @param Exception $exception Error to display * @param Throwable $error Error to display
* *
* @return Response HTTP response * @return Response HTTP response
*/ */
public function error(Request $request, Response $response, Exception $exception) public function error(Request $request, Response $response, Throwable $error)
{ {
if ($this->config->debug) { if ($this->config->debug) {
$exception = FlattenException::createFromThrowable($error);
$handler = new ExceptionHandler(); $handler = new ExceptionHandler();
$handler->handle($exception); $response->getBody()->write($handler->getHtml($exception));
return $response->withStatus($exception->getStatusCode());
} else { } else {
if ($error instanceof Exception) {
$message = $error->getMessage();
} else {
$message = '';
}
$this->view->render( $this->view->render(
$response, $response,
'error.tpl', 'error.tpl',
[ [
'config' => $this->config, 'config' => $this->config,
'errors' => $exception->getMessage(), 'error' => $message,
'class' => 'video', 'class' => 'video',
'title' => $this->localeManager->t('Error'), 'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request), 'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(), 'locale' => $this->localeManager->getLocale(),
] ]
); );
return $response->withStatus(500);
} }
return $response->withStatus(500);
}
/**
* Display an error page for fatal errors.
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param Throwable $error Error to display
*
* @return Response HTTP response
*/
public function fatalError(Request $request, Response $response, Throwable $error)
{
if ($this->config->debug) {
$handler = new ExceptionHandler();
$handler->handle(new FatalThrowableError($error));
} else {
$this->view->render(
$response,
'error.tpl',
[
'config' => $this->config,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
}
return $response->withStatus(500);
} }
/** /**

View file

@ -56,7 +56,7 @@ $downloadController = new DownloadController($container);
// Error handling. // Error handling.
$container['errorHandler'] = [$frontController, 'error']; $container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'fatalError']; $container['phpErrorHandler'] = [$frontController, 'error'];
// Routes. // Routes.
$app->get( $app->get(

View file

@ -4,11 +4,6 @@
{include file="inc/logo.tpl"} {include file="inc/logo.tpl"}
<h2>{t}An error occurred{/t}</h2> <h2>{t}An error occurred{/t}</h2>
{t}Please check the URL of your video.{/t} {t}Please check the URL of your video.{/t}
<p><i> <p><i>{$error|escape}</i></p>
{foreach $errors as $error}
{$error|escape}
<br/>
{/foreach}
</i></p>
</main> </main>
{include file='inc/footer.tpl'} {include file='inc/footer.tpl'}