When working in a web environment, it can be difficult to know the health of your application without digging through some log files or worse, waiting for the customer to complain.

Ruby On Rails provides a nice and easy alternative for emailing you whenever an error occurs within your application.

And best of all, it is a plugin called exception_notification, which requires very little effort to roll into your application. To install the exception_notification plugin, just type the following from the root of your rails app:

> ruby script\plugin install exception_notification

And then any controller that you want to notify you of errors needs to include ExceptionNotifiable. In my case, I would like to be notified for all controllers, so I added this into my application controller:

[ruby]class ApplicationController < ActionController::Base
include ExceptionNotifiable[/ruby]

Then, you just need to configure the email address to send the error notifications in your config\environment.rb file:

[ruby]ExceptionNotifier.exception_recipients =
%w(some-admin@email.com another-admin@email.com)[/ruby]

And that's it!

Note: One prerequisite to doing all of this is you must also configure your ActionMailer for sending emails.

5 Responses to “Global Error Handling with Exception Notification”

  1. Phlip Says:

    Thank you!

    And the first thing I do after “And that’s it!” is how do I test it? (A raw test with an example ActionMailer class works.)

    How do I hot-wire my app to fail, then run a test and watch it fail? Or how do I write a functional test

    My present attempt (via Rails 1.2.3) to put a raise ‘die’ into the controllers and run the site doesn’t work, and doesn’t throw any errors.

  2. Brian McQuay Says:

    Awesome plugin. I haven’t actually used it but I’m excited to try it out now.

  3. Rita Says:

    Hi I added plug in in my project, configured it like the steps given above. But I am not receiving any mails. can anyone help me what could be missing?

  4. Dave Says:

    Would be useful to know how to get notifications from cron script/runner exceptions (batch jobs) not just controller actions

  5. Tom Davies Says:

    Hi Dave,

    I agree that would be useful. Perhaps you could just wrap your code in a begin rescue block and call a Notifier method to mail you?

    Tom

Leave a Reply