Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display exception text in a custom error page on Symfony?

Tags:

php

symfony

I'm working on Symfony and I created a custom error page, following the instruction of the Symfony documentation.

So, I have my app/Ressources/TwigBundle/views/Exception/error.html.twig file. It works fine, but in my error view, I want to display the Exception text, so that if I do this in a controller:

throw new \Exception('Ooops !');

...i can display the value 'Ooops !' in my Twig View.

Do someone knows which Twig variable have this information ?

like image 906
maxime Avatar asked Nov 06 '13 13:11

maxime


People also ask

Why do we see error pages in Symfony?

And when they do, we show our users an error page. Hopefully, a hilarious error page. Right now, our 404 page isn’t very hilarous, except for the little Pacman ghost that’s screaming “Exception detected”. He’s adorable. We see this big descriptive error page because we’re in the dev environment, and Symfony wants to help us fix our mistake.

How does Symfony handle exception handling?

Behind the scenes, Symfony dispatches an event whenever an exception happens. We haven’t talked about events yet, but this basically means that if you want, you can be nofitied whenever an exception is thrown anywhere in your code.

What happens when an exception is not handled by a page?

When an exception is thrown by an ASP.NET page and is not handled, the exception percolates up to the ASP.NET runtime, which displays the configured error page.

How to navigate to vendor/Symfony in PhpStorm?

In PHPStorm, I can navigate to vendor/symfony/symfony, right click, then select “Find in Path”. Let’s look for the “An Error Occurred” text.


1 Answers

I finally found the answer : the Exception Message is simply accessible by the exception.message variable in the Twig error template.

like image 110
maxime Avatar answered Nov 15 '22 15:11

maxime