// Internet Duct Tape

How to Install the Exception Notifier Plugin with Ruby on Rails

Posted in Ruby on Rails, Technology by engtech on February 06, 2008

Learning Ruby

Exception Notifier is a Rails plugin that will email you when an error occurs in your Rails application with full debugging information. It’s as useful as you can imagine, and running it is the difference between happy users and grumpy users who don’t use your web app because every second click looks like this:

Rails Error Message

Agile Web Development with Rails v2 has the skinny on how to install this plugin starting on pg 629. In my infinite Rails Newbieness, I still had a heck of a time getting it working properly despite excellent guides like this one or the official install notes.

The Newb’s Guide to getting the Exception Notifier plugin to work in Rails

#1: That was easy – Installing the Exception Notifier Plugin

Step #1: On the console in your Rails application root directory type:

 
ruby script/plugin install exception_notification

Step #2: Add the following line to your config/environment.rb file AT THE END OF THE FILE:

 

# Include your application configuration below

ExceptionNotifier.exception_recipients = %w(your@emailaddress.com)

Step #3: Since you’re already changing configuration options, you might as well change these two from the default while you’re at it.

 
ExceptionNotifier.sender_address = %("Application Error" <app.error@myapp.com>)

# defaults to "[ERROR] "

ExceptionNotifier.email_prefix = "[APP] "

Changing the sender_address can go a long way to preventing the emails from being marked as spam.

Step #4: Restart the server! You’ve installed a new plugin which means you have to restart the server in order to use it.

Gotcha #1:

 

active_support/dependencies.rb:266:in `load_missing_constant': uninitialized constant ExceptionNotifier (NameError)

This means that you put the ExceptionNotifier.exception_recipients line in the wrong spot. It goes at the end of the file, not in the class.

#2: The Postman Rings Never – How do I debug the email notification?

Step #1: Open up a console windows and do a tail -f log/development.log and you’ll be able to see the Exception Notifier plugin trying to handle the emails.

It will show information like who the email is being sent to, and delicious tidbits like the email is crashing with an SMTP Authentication Error.

 

endering ./script/../config/../public/500.html (500 Error)

rendering section "request"

rendering section "session"

rendering section "environment"

rendering section "backtrace"

Sent mail:

From: Exception Notifier <exception.notifier@default.com>

To: engtechwp@gmail.com

Subject: [ERROR] mycontroller#error (Net::SMTPAuthenticationError) "334 HASHINFO"

Mime-Version: 1.0

Content-Type: text/plain; charset=utf-8

A Net::SMTPAuthenticationError occurred in mycontroller#error:

#3: But Does It Blend? Generating Exception Notificiations on Development

Step #1: Create a controller action that will always generate an error

Edit one of your controller files and add these lines

 

def error

raise RuntimeError, "Generating an error"

end

You don’t need to create a view for it.

Step #2: Change your development settings to let exceptions generate email notifications. In config/environments/development.rb change these two lines

 

#config.action_controller.consider_all_requests_local = true

config.action_controller.consider_all_requests_local = false # debugging exception_notifier

#config.action_mailer.raise_delivery_errors = false

config.action_mailer.raise_delivery_errors = true # debugging exception_notifier

Step #3: Tell Exception Notifier to ignore it’s local address rules

In app/controllers/application.rb

 

include ExceptionNotifiable

local_addresses.clear # always send email notifications instead of displaying the error

You’ll want to remove these changes once you know the Exception Notification plugin is sending emails.

Step #4: Try it out! Navigate to the http://yourapp/controller/error action you created in step #1 of this section. Instead of seeing the debugging trace you’ll see the standard application error page that your users see. But did you get the email?

#4: The Spice Must Flow – Configuring Action Mailer

If you already have a working ActionMailer configuration then skip this section.

The default settings for Action Mailer will use SMTP on localhost. Give it a try and see if it works. If it doesn’t get sent then it may be because you’ve never configured Action Mailer to know anything about how to send an email! Configuring Action Mailer is  covered on pg 567 of Agile Web Development with Rails v2.

You can see if the email was sent or not by looking at your development log file and seeing if there are any dread SMTP errors like

 
535 5.7.3 Authentication unsuccessful. 

Exchange can be a cruel mistress.

The settings go in config/environment.rb (or one of the files in the environments subdirectory if you have different mail settings for different servers). You’ll have to figure out the correct settings by checking your mail program or by bribing the IT guy.

 
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {

:address => "domain.of.smtp.host.net",

:port => 25,

:domain => "domain.of.sender.net",

:authentication => :login,

:user_name => "user",

:password => "secret"

}

More information about the ActionMailer configuration options.

I’d like to give a big thank you to all of the commenters on this post, without which I wouldn’t have gotten this working.