简体   繁体   English

在任何失败情况下,如何使用Ruby on Rails显示自定义错误页面?

[英]How can I display a custom error page with Ruby on Rails in any failure case?

I know that if an exception is raised in my main app, I can use the exceptions_app configuration attribute to serve a dynamic error page. 我知道,如果在主应用程序中引发异常,则可以使用exceptions_app配置属性来提供动态错误页面。

However, if this error page is dynamic, it might also fail. 但是,如果此错误页面是动态的,则它也可能会失败。 In my case, I'm sharing some of the display logic with the main app, and it happened that that logic was the source of the exception, and rendering the error page raised it again. 就我而言,我与主应用程序共享了一些显示逻辑,并且碰巧该逻辑是异常的来源,并且渲染错误页面再次引发了该异常。 My end users then saw the Passenger error page, which is all purple and weird and frightening. 然后,我的最终用户看到了“旅客错误”页面,页面全是紫色,而且很奇怪又令人恐惧。

How can I reliably display a custom error page to my users, possibly with a static page fallback? 如何可靠地向用户显示自定义错误页面,可能会有静态页面回退?

There is actually a whole series of configuration switches and redirection possibilities depending on your stack. 实际上,取决于您的堆栈,一整套配置开关和重定向的可能性。

Here is a decision flow diagram showing all the possibilities. 这是显示所有可能性的决策流程图。 If you have a dynamic error page, you should really also add a static rendering of it in public/500.html , and symlink it to some other file and reference that one in your web server (in the diagram, Apache's ErrorDocument ). 如果您有一个动态错误页面,则实际上还应该在public/500.html添加它的静态呈现,并将其符号链接到其他文件,并在您的Web服务器中引用该文件(在图中为Apache的ErrorDocument )。

You cannot simply serve public/<HTTP status code>.html directly. 您不能简单地直接提供public/<HTTP status code>.html Indeed, the web server would then consider /<HTTP status code> requests as requests for the static page, and our errors controller would not trigger Rails routing anymore, preempting dynamic errors display. 实际上,Web服务器随后会将/<HTTP status code>请求视为对静态页面的请求,并且我们的错误控制器不再触发Rails路由,从而避免了动态错误的显示。

You should set config.action_dispatch.show_exceptions to false ( more details ), as well as disable your app runner (Passenger in the diagram) error page, otherwise you will leak exceptions content. 您应该将config.action_dispatch.show_exceptions设置为false更多详细信息 ),并禁用您的应用程序运行程序(图中的Passenger)错误页面,否则您将泄漏异常内容。

Ruby on Rails 3自定义错误决策流程

If you want to go to that level of detail, it is very important that you add integration tests on a user-hidden, voluntarily broken page, as you are disabling some debugging and failsafe features. 如果要详细说明,将集成测试添加到用户隐藏的,自愿损坏的页面上非常重要,因为您将禁用某些调试和故障保护功能。

References 参考

You could also be interested in the following references: 您可能还会对以下参考资料感兴趣:

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

相关问题 Ruby on Rails如何显示验证错误消息? - Ruby on Rails How can i display validation error message? 自定义错误页面 - Ruby on Rails - Custom Error Page - Ruby on Rails 如何在我正在查看的页面上显示项目数? (在rails / shopify上使用液体/红宝石) - How I can display the number of items on the page I am looking at? (using liquid / ruby on rails / shopify) 如何在Ruby on Rails中显示ActiveRecord :: JDBCError的自定义错误消息? - How to display custom error message for ActiveRecord::JDBCError in Ruby on Rails? 如何在.html.erb页面上显示控制器的变量(循环中)? 红宝石在铁轨上 - how can I display controller's variable (which is on a loop) on .html.erb page? ruby on rails 在用Rails提交红宝石表单之前,如何在页面上显示开放图协议的数据? - How can I display data of an open graph protocol on a page before a form is submitted in ruby on rails? 如何在Ruby on Rails的会话对象中显示值? - How can i display values in session object in Ruby on Rails? 如何在Ruby On Rails中显示日历列表的“明天”? - How can I display “Tomorrow” of calendar list in Ruby On Rails? 如何在 Ruby on Rails 中显示更好的 HAML 错误? - How can I display better HAML errors in Ruby on Rails? Ruby / Rails:如何以科学记数法显示数字? - Ruby/Rails: How can I display a number in scientific notation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM