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\Views\Smarty;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\Debug\Exception\FlattenException;
/**
* Main controller.
@ -233,63 +233,41 @@ class FrontController extends BaseController
*
* @param Request $request PSR-7 request
* @param Response $response PSR-7 response
* @param Exception $exception Error to display
*
* @return Response HTTP response
*/
public function error(Request $request, Response $response, Exception $exception)
{
if ($this->config->debug) {
$handler = new ExceptionHandler();
$handler->handle($exception);
} else {
$this->view->render(
$response,
'error.tpl',
[
'config' => $this->config,
'errors' => $exception->getMessage(),
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
}
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)
public function error(Request $request, Response $response, Throwable $error)
{
if ($this->config->debug) {
$exception = FlattenException::createFromThrowable($error);
$handler = new ExceptionHandler();
$handler->handle(new FatalThrowableError($error));
$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,
'error' => $message,
'class' => 'video',
'title' => $this->localeManager->t('Error'),
'canonical' => $this->getCanonicalUrl($request),
'locale' => $this->localeManager->getLocale(),
]
);
}
return $response->withStatus(500);
}
}
/**
* Generate the canonical URL of the current page.

View file

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

View file

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