How to Install the Exception Notifier Plugin with Ruby on Rails
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:
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.
But Action Mailer will display the entire log file unless you’re running on Edge Rails:
http://dev.rubyonrails.org/changeset/8781
Our entire site (Funadvice.com) was developed using Ruby on Rails :)
hey – would you mind telling me how you do your nice code blocks? I have a wordpress blog as well and could never work it out. I tried just copying one of your pre tags over (from your page source into my ‘edit html’ window), in case it was some standard wordpress css, but it doesn’t work for me.
thanks – max (nubyonrails.wordpress.com)
@ nubyonrails:
Hi Max,
here you go: http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/
also, you may want to set your website in your profile to your blog for when you leave comments:
http://en.forums.wordpress.com/topic.php?id=20232&replies=7
cheers
[…] How to Install the Exception Notifier Plugin with Ruby on Rails « // Internet Duct Tape […]
Don’t you have to include the plugin. Per the readme:
First, include the ExceptionNotifiable mixin in whichever controller you want
to generate error emails (typically ApplicationController):
class ApplicationController < ActionController::Base
include ExceptionNotifiable
…
end
[…] tutorials to help out: – http://internetducttape.com/2008/02/06/rails-guide-exception-notifier-plugin/ – […]
[…] tutorials to help out: – http://internetducttape.com/2008/02/06/rails-guide-exception-notifier-plugin/ – […]
thats quite a handy tutorial. It took me 5 minutes to get my notifier going!
Hey, this was *extremely* helpful. The instructions for getting the development environment to trigger the message were just what I needed. Thanks!
Watch for a bug when using this plugin with Rails 2.1. The workarounds are described here: http://www.ruby-forum.com/topic/155319
I found the workarounds to solve the two problems identified, and was able to get the plugin working with the instructions above. Thanks!
[…] gets in front of a human, they get none of the detail we’d expect from something like the ExceptionNotifier plugin. We want HTTP headers, we want session variables. So we need to pass some more detail into […]
Thank you for an excellent How-To. I had it installed and configured in all of 5 minutes.
Thanks
GJ
I have tried but it not working at all, not even rendering in logs nor I am getting any mails,
plz help me, thanks in advance,.
I am having a similar problem as Pavan. I have restrained from commenting and asking because it’s been so long since the last post and most posts have been successes, but I cannot get mine to work either. I have action_mailer installed and working, it’s been working on my site for 8 months and actively sending out emails, there are not any problems there. However, nothing shows up in the logs at all (except the usual rendering and error blurbs); Nothing about trying to invoke the ExceptionNotifier or ActionMailer. I have definitely included all the appropriate pieces in all the appropriate places (environment.rb after the class def, “include ExceptionNotifiable” inside each controller w/ addl. local_address.clear and a method named “error” to test it with). No luck with success or errors, just dormancy.
I found a solution to my problem. Apparently the ExceptionNotifier won’t deliver email when the app is being run as the development environment. The local_addresses.clear won’t resolve that issue. There is a solution if you do want email notifications in your development environment.
Simply add the following statement inside the class definition in app/controllers/application.rb:
The explanation of why and where I found the help can be found here:
http://agilewebdevelopment.com/plugins/exception_notifier
Oop!
I forgot address the crux of the issue by including the statement to add:
alias :rescue_action_locally :rescue_action_in_public
[…] I set up email notification for errors with help from this blog post. Since then I’ve been getting the occasional email from Booko telling me when things go […]
Thanks Kelv, that helped with getting my email sent in development. However, I got this error after i did that:
ActionView::TemplateError (protected method `filter_parameters’ called for #)
I found out that to get around this you need to do:
script/install plugin git://github.com/rails/exception_notification.git
This is the updated version of it that isnt in the normal script/plugin install exception_notification . Hope this helps someone. I would recommend changing that in the article too.
I think the first part of the tutorial forgot to mention that your have to add
include ExceptionNotifiable
to your ApplicationController. It was required for me to work in production (Rails 2.2), and the plugins README mentions it too. So it’s not only for testing like said in the text above.
Besides that: Thanks for the nice tutorial!
Great tutorial… for Rails 2.
Any chance you’ll be updating this for Rails 3?