Rails global error handling

by rbrant on May 6, 2006

I’d been getting the dreaded , “Application error (Rails) message” for a little while and wasn’t sure what the cause was. After digging around a bit I figured out that it was my own error handler that was causing the error. Pretty ironic, eh? I used some code from AWDR to catch all exceptions, which was working, but when the code got to the part where it emailed me the error, it was barfing on a typo. So rather than looping into infinity, it bubbles back up to Rescue.rb which will generate that error message. There is a way to avoid this error via environment.rb, by making sure this line is there:

ActionMailer::Base.raise_delivery_errors = false

After all, if there is some exception that’s raised when trying to send the error, it would be better to just not send the email and display your custom 500 page. You’re not going to get the email anyway. Might as well make it look nice.

And speaking of global error handling, I have to thank Jamis Buck for the great exception_notification plugin, which is super easy to install and gives you everything you need to track errors in production. Below is the quick guide to installation. There are some nice ways to customize it which you can read about in the plugin’s readme file.

in the apps’ root, run:

ruby scriptplugin install exception_notification

and then in the controller in which you want to trap errors include it like this;

    class ApplicationController < ActionController::Base
    include ExceptionNotifiable

Mine's in my application controller as I want to catch everything, but you can put it in specific controllers.

To set emails to and from, you put the following in environment.rb:

ExceptionNotifier.exception_recipients = %w([your to email])
ExceptionNotifier.sender_address = %w([your from email]))

Nothing else to it! Pretty sweet indeed. I'm really getting into plugins of late; very powerful, portable, and easy to use. Oh yeah, there was one minor issue I had to deal with. Because my host is still running 1.0, I had to edit these lines:

def render_404
    respond_to do |type|
      type.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found" }
      type.all  { render :nothing => true, :status => "404 Not Found" }
    end
  end

  def render_500
    respond_to do |type|
      type.html { render :file => "#{RAILS_ROOT}/public/500.html", :status => "500 Error" }
      type.all  { render :nothing => true, :status => "500 Error" }
    end
  end

to:

def render_404
    render :file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found"
  end

  def render_500
    render :file => "#{RAILS_ROOT}/public/500.html", :status => "500 Error"
  end

Leave a Comment

Previous post:

Next post: