Super Exception Notifier plugin

Plugin details

The SuperExceptionNotifier plugin provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application, as well as a default set of error page templates to render based on the status code assigned to an error. The plugin is configurable, allowing programmers to specify (all on a per environment basis!)

Websitehttp://code.google.com/p/super-exception-notifier/ Repositoryhttp://super-exception-notifier.googlecode.com/svn/trunk/super_exception_notifier/ Author Peter H. Boling Tags exception, notify LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://super-exception-notifier.googlecode.com/svn/trunk/super_exception_notifier/

Usage
==========

First, include the ExceptionNotifiable mixin in whichever controller you want to generate error emails (typically ApplicationController):

  class ApplicationController < ActionController::Base
    include ExceptionNotifiable
    ...
  end


Then, specify the email recipients in your environment:

  ExceptionNotifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)


And that's it! The defaults take care of the rest.

Configuration
=============

You can tweak other values to your liking, as well. In your environment file, just set any or all of the following values:

  # defaults to exception.notifier@default.com
  ExceptionNotifier.sender_address =
    %("Application Error" )

  # defaults to "[ERROR] "
  ExceptionNotifier.email_prefix = "[APP-#{RAILS_ENV.capitalize} ERROR] "

  #defaults to false - meaning by default it sends email.  Setting true will cause it to only render the error pages, and NOT email.
  ExceptionNotifier.render_only = true

  #defaults to %W( 405 500 503 )
  ExceptionNotifier.send_email_error_codes = %W( 400 403 404 405 410 500 501 503 )

  #	defaults to: %("Exception Notifier" )
  ExceptionNotifier.sender_address = %("ASW #{RAILS_ENV.capitalize} Error" )
  
  #defaults explained further down in detail
  ExceptionNotifier.view_path = 'app/views/error'

  #defaults explained further down in detail
  self.http_error_codes = { "400" => "Bad Request" }

  #defaults explained further down in detail
  self.rails_error_classes = { NameError => "503" }


Email notifications will only occur when the IP address is determined not to be local. You can specify certain addresses to always be local so that you'll get a detailed error instead of the generic error page. You do this in your controller (or even per-controller):

  consider_local "64.72.18.143", "14.17.21.25"


You can specify subnet masks as well, so that all matching addresses are considered local:

  consider_local "64.72.18.143/24"


The address "127.0.0.1" is always considered local. If you want to completely reset the list of all addresses (for instance, if you wanted "127.0.0.1" to NOT be considered local), you can simply do, somewhere in your controller:

  local_addresses.clear


SuperExceptionNotifier allows customization of the error classes that will be handled, and which HTTP status codes they will be handled as: (default values are shown)

  self.http_error_codes = { "200" => "OK"
					"400" => "Bad Request",
          "403" => "Forbidden",
          "404" => "Not Found",
          "405" => "Method Not Allowed",
          "410" => "Gone",
          "500" => "Internal Server Error",
          "501" => "Not Implemented",
          "503" => "Service Unavailable" }


Q: Why is "200" listed as an error code?

A: You may want to have multiple custom errors that the standard HTTP status codes weren't designed to accommodate, and for which you need to render customized pages. Explanation and examples are a little further down...

Then you can specify which of those should send out emails! By default, the email notifier will only notify on critical errors (405 500 503 statuses). For example, ActiveRecord::RecordNotFound and ActionController::UnknownAction errors will simply render the contents of /vendor/plugins/exception_notifier/views/exception_notifiable/###.html file, where ### is 400 and 501 respectively.

  ExceptionNotifier.send_email_error_codes = %w( 400 405 500 503 )


You can also configure the text of the HTTP request's response status code: (by default only the last 6 will be handled, the first 6 are made up error classes)

  self.rails_error_classes = { 
    AccessDenied => "403",
    PageNotFound => "404",
    InvalidMethod => "405",
    ResourceGone => "410",
    CorruptData => "500",
    NotImplemented => "501",
    NameError => "503",
    TypeError => "503",
    ActiveRecord::RecordNotFound => "400",
    ::ActionController::UnknownController => "404",
    ::ActionController::UnknownAction => "501",
    ::ActionController::RoutingError => "404",
    ::ActionController::MissingTemplate => "404"
} 


To make up your own error classes, you can define them in environment.rb, or in application.rb:

#These error classes can be raised in before filters, or controller actions like so:
#  def owner_required
#    raise AccessDenied unless current_user.id == @photo.user_id
#  end
#They can also be wrapped in methods in application.rb (or a mixin for it) like so:
#  def access_denied
#    raise AccessDenied
#  end
#And then used like so (as before filter in a controller):
#  def owner_required
#    access_denied unless current_user.id == @photo.user_id
#  end
#What happens when the error is raised is managed by the exception notifier plugin, and customizations are in application.rb, as well as at the end of this initializer
class AccessDenied    < StandardError; end
class ResourceGone    < StandardError; end
class NotImplemented  < StandardError; end
class PageNotFound    < StandardError; end
class InvalidMethod   < StandardError; end
class CorruptData     < StandardError; end


You may also configure which HTTP status codes will send out email: (by default = [], email sending is defined by status code only)

  ExceptionNotifier.send_email_error_classes = [	
    NameError, 
    TypeError, 
    ActionController::RoutingError 
  ]


Email will be sent if the error matches one of the error classes to send email for OR if the error's assigned HTTP status code is configured to send email!

You can also customize what is rendered. SuperExceptionNotifier will render the first file it finds in this order:

RAILS_ROOT/public/###.html
RAILS_ROOT/ExceptionNotifier.view_path/###.html
RAILS_ROOT/vendor/plugins/super_exception_notifier/views/exception_notifiable/###.html"


And if none of those paths has a valid file to render, this one wins:

RAILS_ROOT/vendor/plugins/super_exception_notifier/views/exception_notifiable/500.html"


You can configure ExceptionNotifier.view_path in your environment file like this:

  ExceptionNotifier.view_path = 'app/views/error'


So public trumps your custom path which trumps the plugin's default path.

Custom Error Pages
====================

You can render CUSTOM error pages! Here's how:

1. Make sure 200 is one of your status codes (optional)

  self.http_error_codes = { "200" => "OK" }


2. Setup your custom error class: e.g.

in config/environment.rb:

      class InsufficientFundsForWithdrawal < StandardError; end


3. Setup SuperExceptionNotifier to handle the error:

in app/controllers/application.rb:

      self.rails_error_classes = { InsufficientFundsForWithdrawal => "200" }


4. Set your custom error's view path:

  ExceptionNotifier.view_path = 'app/views/error'


5. Create a view for the error. SuperExceptionNotifier munges the error's class by converting to a string and then replacing consecutive ':' with '' and then downcases it:

  touch app/views/error/insufficient_funds_for_withdrawal.html


6. That's it! All errors that are set to be handled with a status of "200" will render a custom page.

7. If you want to have errors that render custom pages also send emails then you'll need to

  ExceptionNotifier.send_email_error_classes = [ InsufficientFundsForWithdrawal ]



Customization
=============

By default, the notification email includes four parts: request, session, environment, and backtrace (in that order). You can customize how each of those sections are rendered by placing a partial named for that part in your app/views/exception_notifier directory (e.g., session.rhtml). Each partial has access to the following variables:

    * @controller: the controller that caused the error
    * @request: the current request object
    * @exception: the exception that was raised
    * @host: the name of the host that made the request
    * @backtrace: a sanitized version of the exception's backtrace
    * @rails_root: a sanitized version of RAILS_ROOT
    * @data: a hash of optional data values that were passed to the notifier
    * @sections: the array of sections to include in the email 


You can reorder the sections, or exclude sections completely, by altering the ExceptionNotifier.sections variable. You can even add new sections that describe application-specific data--just add the section's name to the list (whereever you'd like), and define the corresponding partial. Then, if your new section requires information that isn't available by default, make sure it is made available to the email using the exception_data macro:

  class ApplicationController < ActionController::Base
    ...
    protected
      exception_data :additional_data

      def additional_data
        { :document => @document,
          :person => @person }
      end
    ...
  end


In the above case, @document and @person would be made available to the email renderer, allowing your new section(s) to access and display them. See the existing sections defined by the plugin for examples of how to write your own.
Advanced Customization

If you want to seriously modify the rules for the notification, you will need to implement your own rescue_action_in_public method. You can look at the default implementation in SuperExceptionNotifier for an example of how to go about that.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 7 months ago