简体   繁体   English

如何在Symfony的自定义错误页面中显示异常文本?

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

I'm working on Symfony and I created a custom error page, following the instruction of the Symfony documentation. 我正在研究Symfony,我按照Symfony文档的说明创建了一个自定义错误页面。

So, I have my app/Ressources/TwigBundle/views/Exception/error.html.twig 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 that if I do this in a controller: 它工作正常,但在我的错误视图中,我想显示异常文本,所以如果我在控制器中执行此操作:

throw new \Exception('Ooops !');

...i can display the value 'Ooops !' ...我可以显示'哎呀!'的价值 in my Twig View. 在我的Twig视图中。

Do someone knows which Twig variable have this information ? 有人知道哪个Twig变量有此信息吗?

我终于找到了答案:Exception Message只能通过Twig错误模板中的exception.message变量访问。

@Maxime @Maxime

Indeed that's the way i do it as well. 确实,这也是我这样做的方式。

I just want to mentioned that I encountered the problem when using exception.message that some of the generic errors are also rendered in your custom error page which will lead to exception messages like 我只是想提到我在使用exception.message时遇到了一些问题,即一些常规错误也会在自定义错误页面中呈现,这将导致异常消息,如

"No route found for GET /any_route_that_is_not_defined " “找不到GET / any_route_that_is_not_defined的路由”

which is the default when calling a page that really does not exists by definition 这是默认情况下调用根据定义确实不存在的页面

I didn't found a way to surpress or translate these messages yet :/ 我还没有找到一种方法来压制或翻译这些消息:/

Have a look at the Symfony doc on how to customize error pages . 看看Symfony doc 如何自定义错误页面

You have the message in {{ status_text }} and the exception code in {{ status_code }} . 您有{{ status_text }}的消息和{{ status_code }}的异常代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM