From ef493074d4ff9126255f3d1a27a09d092eeacad2 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Mon, 2 Dec 2019 22:04:14 +0100 Subject: [PATCH] Refactor error controller --- controllers/FrontController.php | 54 ++++++++++----------------------- index.php | 2 +- templates/error.tpl | 7 +---- 3 files changed, 18 insertions(+), 45 deletions(-) diff --git a/controllers/FrontController.php b/controllers/FrontController.php index a56af7e..e5e8b47 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -17,7 +17,7 @@ use Slim\Http\Request; use Slim\Http\Response; use Slim\Views\Smarty; use Symfony\Component\Debug\ExceptionHandler; -use Symfony\Component\Debug\Exception\FatalThrowableError; +use Symfony\Component\Debug\Exception\FlattenException; /** * Main controller. @@ -233,62 +233,40 @@ class FrontController extends BaseController * * @param Request $request PSR-7 request * @param Response $response PSR-7 response - * @param Exception $exception Error to display + * @param Throwable $error Error to display * * @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) { + $exception = FlattenException::createFromThrowable($error); $handler = new ExceptionHandler(); - $handler->handle($exception); + $response->getBody()->write($handler->getHtml($exception)); + + return $response->withStatus($exception->getStatusCode()); } else { + if ($error instanceof Exception) { + $message = $error->getMessage(); + } else { + $message = ''; + } + $this->view->render( $response, 'error.tpl', [ 'config' => $this->config, - 'errors' => $exception->getMessage(), + 'error' => $message, 'class' => 'video', 'title' => $this->localeManager->t('Error'), 'canonical' => $this->getCanonicalUrl($request), '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); } /** diff --git a/index.php b/index.php index b69d13d..6dd0a45 100644 --- a/index.php +++ b/index.php @@ -56,7 +56,7 @@ $downloadController = new DownloadController($container); // Error handling. $container['errorHandler'] = [$frontController, 'error']; -$container['phpErrorHandler'] = [$frontController, 'fatalError']; +$container['phpErrorHandler'] = [$frontController, 'error']; // Routes. $app->get( diff --git a/templates/error.tpl b/templates/error.tpl index 076e15f..55adeb5 100644 --- a/templates/error.tpl +++ b/templates/error.tpl @@ -4,11 +4,6 @@ {include file="inc/logo.tpl"}

{t}An error occurred{/t}

{t}Please check the URL of your video.{/t} -

- {foreach $errors as $error} - {$error|escape} -
- {/foreach} -

+

{$error|escape}

{include file='inc/footer.tpl'}