Skip to content

Commit

Permalink
Refactor error controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Dec 2, 2019
1 parent f271777 commit ef49307
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 44 deletions.
52 changes: 15 additions & 37 deletions controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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.
Expand Down Expand Up @@ -233,62 +233,40 @@ public function info(Request $request, Response $response)
*
* @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);
} 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);
}
$response->getBody()->write($handler->getHtml($exception));

/**
* 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));
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);
return $response->withStatus(500);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

// Error handling.
$container['errorHandler'] = [$frontController, 'error'];
$container['phpErrorHandler'] = [$frontController, 'fatalError'];
$container['phpErrorHandler'] = [$frontController, 'error'];

// Routes.
$app->get(
Expand Down
7 changes: 1 addition & 6 deletions templates/error.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

0 comments on commit ef49307

Please sign in to comment.