]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Dislay an error if an exception occurs in the error handler 1602/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 20 Oct 2020 16:32:46 +0000 (18:32 +0200)
committerArthurHoaro <arthur@hoa.ro>
Tue, 20 Oct 2020 16:32:46 +0000 (18:32 +0200)
Related to #1598

application/Utils.php
application/front/controller/visitor/ErrorController.php
index.php

index bcfda65c9ca14cef75aac304396f0512dba500d5..37be9a13fdf89df0e9eeadc03c4b8c0a10f7c043 100644 (file)
@@ -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();
+}
+
index 10aa84c806ea444b85a692e1d6d9d188eed4cba4..8da11172ffa52c6c8ae5967760b5a5043c05cca7 100644 (file)
@@ -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.'));
             }
index 220847f5ec4f3469af3e2eb32e08b5f6cc2c4ab9..b6ee8ebc482b585e01bf858bc97ae90184b172c1 100644 (file)
--- 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)
+    ));
+}