简体   繁体   English

如何在rescue_from中渲染500页

[英]How to render 500 page in rescue_from

I would like to send e-mail when having an exception in my application and render the regular 500 page. 我想在我的应用程序中出现异常时发送电子邮件并呈现常规500页。 I could not find how to perform the 500 page render: 我找不到如何执行500页渲染:

class ApplicationController < ActionController::Base
  rescue_from StandardError do
     send_email_of_error
     # what goes here? 
  end

  ...
end

Raising the exception again will likely to what you want: 再次提出异常可能是你想要的:

rescue_from StandardError do |exception|
  send_email_of_error
  raise exception
end

You could also call render to render your own page, the docs have an example doing this. 你也可以调用render来渲染自己的页面, 文档有一个例子。

But why reinvent the wheel? 但为什么重新发明轮子? The exception notifier gem already does this and is customizable and tested. 异常通知程序gem已经执行此操作并且可以自定义和测试。

This is an approach that maybe fits your needs: 这种方法可能符合您的需求:

class ApplicationController < ActionController::Base
  rescue_from Exception, :with => :render_500

  def render_500(exception)
    @exception = exception
    render :template => "shared/500.html", :status => 500
  end
end

Possibly, it can be useful to look on sentry, what makes notifications automatically with bunch of information in system. 可能,查看哨兵是有用的,是什么使系统中的一堆信息自动发出通知。

gem https://github.com/getsentry/raven-ruby gem https://github.com/getsentry/raven-ruby

demo https://github.com/getsentry/sentry-demo 演示https://github.com/getsentry/sentry-demo

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

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