Improve fallback error handling

This commit is contained in:
Pierre Rudloff 2020-10-18 12:03:57 +02:00
parent 9becaeaabe
commit 07f19a80f4
2 changed files with 38 additions and 2 deletions

36
classes/ErrorHandler.php Normal file
View file

@ -0,0 +1,36 @@
<?php
namespace Alltube;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Throwable;
/**
* Class ErrorHandler
* @package Alltube
*/
class ErrorHandler
{
/**
* Last resort if the error has not been caught by the Slim error handler for some reason.
* @param Throwable $e
* @return void
*/
public static function handle(Throwable $e)
{
error_log($e);
if (class_exists(HtmlErrorRenderer::class)) {
// If dev dependencies are loaded, we can use symfony/error-handler.
$renderer = new HtmlErrorRenderer(true);
$exception = $renderer->render($e);
http_response_code($exception->getStatusCode());
die($exception->getAsString());
} else {
http_response_code(500);
die('Error when starting the app: ' . htmlentities($e->getMessage()));
}
}
}

View file

@ -6,6 +6,7 @@ use Alltube\ConfigFactory;
use Alltube\Controller\DownloadController;
use Alltube\Controller\FrontController;
use Alltube\Controller\JsonController;
use Alltube\ErrorHandler;
use Alltube\LocaleManagerFactory;
use Alltube\LocaleMiddleware;
use Alltube\LoggerFactory;
@ -87,6 +88,5 @@ try {
$app->run();
} catch (Throwable $e) {
// Last resort if the error has not been caught by the error handler for some reason.
die('Error when starting the app: ' . htmlentities($e->getMessage()));
ErrorHandler::handle($e);
}