From 98f4356fbec9a12b0a058c9c272f78c2dc501906 Mon Sep 17 00:00:00 2001 From: Serreau Jovann Date: Fri, 20 Mar 2026 15:54:26 +0100 Subject: [PATCH] Display error message on 500 error page Co-Authored-By: Claude Opus 4.6 (1M context) --- src/EventListener/ExceptionListener.php | 5 ++++- templates/error/500.html.twig | 7 ++++++- tests/EventListener/ExceptionListenerTest.php | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php index 85e4e9e..20e2d40 100644 --- a/src/EventListener/ExceptionListener.php +++ b/src/EventListener/ExceptionListener.php @@ -34,7 +34,10 @@ class ExceptionListener } $response = new Response( - $this->twig->render($template, ['status_code' => $statusCode]), + $this->twig->render($template, [ + 'status_code' => $statusCode, + 'error_message' => $exception->getMessage(), + ]), $statusCode, ); diff --git a/templates/error/500.html.twig b/templates/error/500.html.twig index fe51320..7d0207a 100644 --- a/templates/error/500.html.twig +++ b/templates/error/500.html.twig @@ -13,7 +13,12 @@
{{ status_code }}

Erreur serveur

-

Une erreur inattendue s'est produite. Nos equipes ont ete notifiees.

+

Une erreur inattendue s'est produite. Nos equipes ont ete notifiees.

+ {% if error_message is defined and error_message %} +
+

{{ error_message }}

+
+ {% endif %} Retour a l'accueil diff --git a/tests/EventListener/ExceptionListenerTest.php b/tests/EventListener/ExceptionListenerTest.php index 372103e..f19bcfb 100644 --- a/tests/EventListener/ExceptionListenerTest.php +++ b/tests/EventListener/ExceptionListenerTest.php @@ -43,7 +43,7 @@ class ExceptionListenerTest extends TestCase $twig = $this->createMock(Environment::class); $twig->expects(self::once()) ->method('render') - ->with('error/404.html.twig', ['status_code' => 404]) + ->with('error/404.html.twig', ['status_code' => 404, 'error_message' => 'Not found']) ->willReturn('

404

'); $listener = new ExceptionListener($twig, 'prod'); @@ -61,7 +61,7 @@ class ExceptionListenerTest extends TestCase $twig = $this->createMock(Environment::class); $twig->expects(self::once()) ->method('render') - ->with('error/500.html.twig', ['status_code' => 500]) + ->with('error/500.html.twig', ['status_code' => 500, 'error_message' => 'Server error']) ->willReturn('

500

'); $listener = new ExceptionListener($twig, 'prod'); @@ -78,7 +78,7 @@ class ExceptionListenerTest extends TestCase $twig = $this->createMock(Environment::class); $twig->expects(self::once()) ->method('render') - ->with('error/500.html.twig', ['status_code' => 403]) + ->with('error/500.html.twig', ['status_code' => 403, 'error_message' => 'Forbidden']) ->willReturn('

403

'); $listener = new ExceptionListener($twig, 'prod');