diff --git a/config/config.example.yml b/config/config.example.yml index 3cd1fe0..022e612 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -51,3 +51,6 @@ genericFormats: best: Best bestvideo+bestaudio: Remux best video with best audio worst: Worst + +# Enable debug mode. +debug: false diff --git a/controllers/FrontController.php b/controllers/FrontController.php index f480499..fa9aa59 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -9,6 +9,7 @@ namespace Alltube\Controller; use Alltube\Exception\PasswordException; use Alltube\Locale; use Alltube\Video; +use Throwable; use Exception; use Psr\Container\ContainerInterface; use Slim\Container; @@ -16,6 +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; /** * Main controller. @@ -250,6 +252,37 @@ class FrontController extends BaseController 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); + } + /** * Generate the canonical URL of the current page. * diff --git a/index.php b/index.php index 17754ef..3c6a151 100644 --- a/index.php +++ b/index.php @@ -45,6 +45,7 @@ $jsonController = new JsonController($container); $downloadController = new DownloadController($container); $container['errorHandler'] = [$frontController, 'error']; +$container['phpErrorHandler'] = [$frontController, 'fatalError']; $app->get( '/',