5

I would like to send e-mail when having an exception in my application and render the regular 500 page. I could not find how to perform the 500 page render:

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

  ...
end
mbdev
  • 6,343
  • 14
  • 46
  • 63

2 Answers2

12

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.

But why reinvent the wheel? The exception notifier gem already does this and is customizable and tested.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
2

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
awenkhh
  • 5,811
  • 1
  • 21
  • 24
  • 7
    One should never rescue from Exception. Use StandardError instead as in Andrew Marshall's answer. – dmur Nov 21 '14 at 19:53
  • @dmur it would be helpful for others if you explain your comment. And the question was how to render a 500 page. That is not covered in Andrew Marshalls answer at all. Wether you rescue from Exception or from StandardError - what I never would do because you don't know anything about the error so let it crash and fix it - is not important here. In my answer I should have written "AnyException" – awenkhh Nov 22 '14 at 21:47
  • 2
    You're right, here's an explanation: http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – dmur Nov 25 '14 at 21:22