How to display exception text on user error page in symfony?

I am working on Symfony and have created a custom error page following the instructions in the Symfony documentation.

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

throw new \Exception('Ooops !'); 

... I can display the value "Ooops!" in my twig view.

Does anyone know which Twig variable has this information?

+6
source share
3 answers

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

+16
source

@Maxime

Indeed, the way I do it.

I just want to mention that I ran into a problem when using exception.message, that some of the common errors also appear on your custom error page, which will lead to exception messages, like

"No route found for GET / any_route_that_is_not_defined"

which is the default value when invoking a page that essentially does not exist

I have not yet found a way to suppress or translate these messages: /

+1
source

Take a look at the Symfony document on how to customize error pages .

You have a message in {{ status_text }} and an exception code in {{ status_code }} .

0
source

Source: https://habr.com/ru/post/957508/


All Articles