From 5c06c0870f8e425c2d4ed0f7c330c13e1605628e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 20 Oct 2020 18:32:46 +0200 Subject: [PATCH 1/1] Dislay an error if an exception occurs in the error handler Related to #1598 --- application/Utils.php | 9 +++++++++ .../front/controller/visitor/ErrorController.php | 5 +---- index.php | 12 +++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/application/Utils.php b/application/Utils.php index bcfda65c..37be9a13 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -463,3 +463,12 @@ function t($text, $nText = '', $nb = 1, $domain = 'shaarli') { return dn__($domain, $text, $nText, $nb); } + +/** + * Converts an exception into a printable stack trace string. + */ +function exception2text(Throwable $e): string +{ + return $e->getMessage() . PHP_EOL . $e->getFile() . $e->getLine() . PHP_EOL . $e->getTraceAsString(); +} + diff --git a/application/front/controller/visitor/ErrorController.php b/application/front/controller/visitor/ErrorController.php index 10aa84c8..8da11172 100644 --- a/application/front/controller/visitor/ErrorController.php +++ b/application/front/controller/visitor/ErrorController.php @@ -28,10 +28,7 @@ class ErrorController extends ShaarliVisitorController // Internal error (any other Throwable) if ($this->container->conf->get('dev.debug', false)) { $this->assignView('message', $throwable->getMessage()); - $this->assignView( - 'stacktrace', - nl2br(get_class($throwable) .': '. PHP_EOL . $throwable->getTraceAsString()) - ); + $this->assignView('stacktrace', exception2text($throwable)); } else { $this->assignView('message', t('An unexpected error occurred.')); } diff --git a/index.php b/index.php index 220847f5..b6ee8ebc 100644 --- a/index.php +++ b/index.php @@ -151,6 +151,12 @@ $app->group('/api/v1', function () { $this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory'); })->add('\Shaarli\Api\ApiMiddleware'); -$response = $app->run(true); - -$app->respond($response); +try { + $response = $app->run(true); + $app->respond($response); +} catch (Throwable $e) { + die(nl2br( + 'An unexpected error happened, and the error template could not be displayed.' . PHP_EOL . PHP_EOL . + exception2text($e) + )); +} -- 2.41.0