public
Description: Rails plugin to rescue common Rails exceptions, render an error with your look and feel, and return an appropriate HTTP status code
Homepage:
Clone URL: git://github.com/technicalpickles/safety_valve.git
Sun Oct 12 10:28:31 -0700 2008
commit  ae33d19c5a460784f9ee6ffbc92b7ec6b159a775
tree    6f84cd5b3deb6e23318e9012a378e04307bec89b
parent  5b373704f0735845effaff4444bfb996296c9b6e
name age message
file MIT-LICENSE Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
file README Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
file Rakefile Sun Oct 12 10:28:31 -0700 2008 Use jeweler. [technicalpickles]
directory generators/ Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
file init.rb Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
directory lib/ Sun Oct 12 10:28:31 -0700 2008 Use jeweler. [technicalpickles]
file safety_valve.gemspec Sun Oct 12 10:28:31 -0700 2008 Use jeweler. [technicalpickles]
directory tasks/ Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
directory test/ Sat Oct 11 18:20:07 -0700 2008 Initial commit. [technicalpickles]
README
SafetyValve
===========

Because that pressure has to go somewhere.

http://www.youtube.com/watch?v=7jF5r_AK9q4&feature=related

Errors are bound to happen. When your users encounter one, by default, they will see a generic page provided by Rails. 
Wouldn't it be nice if you could give them an error page that fits the look and feel of your app?

That's where comes in: it provides you with some sane defaults for rescuing from some common Rails exceptions. It also 
returns a HTTP status code that is appropriate

For now, it only handles exceptions would be 404 (File Not Found) errors. This includes:

 * ActiveRecord::RecordNotFound
 * ActionController::RoutingError
 * ActionController::UnknownAction
 * ActionController::UnknownController

Example
=======

Update your app/controller/application.rb to look something like:

    class ApplicationController < ActionController::Base
      include SafetyValve::Controller
      # ... rest of your code here
    end
    
Generate the error templates:

    script/generate errors
    
This creates a template at app/views/errors/404.html.erb. It can be whatever you want, but here's what it looks like by 
default:

    <div class="error">
      The resource you requested was not found.
    </div>
    

We can now do functional tests that make sure we get 404 errors when accessing resources that don't exist. Here's an 
example in shoulda:

    class EventsControllerTest < Test::Rails::ControllerTestCase
      context "trying to view an event that doesn't exist" do
        setup do
          get :show, :id => 'gibberish aieeeee'
        end

        should_respond_with :not_found # 404 works as well
      end
    end
    
The effect
==========

If you're controller does a Model.find(params[:id]), and the record doesn't exist, you'll see 
ActiveRecord::RecordNotFound. SafetyValve will handle rescuing this, and will see that app/views/errors/404.html.erb (or 
404.html.haml) is rendered.

Gotchas
=======

A few of the exceptions will always bubble up to the top in development mode, so you won't see the effect:

 * ActionController::RoutingError

References
==========

This plugin was inspired by some of these fine posts:

 * http://coderkitty.sweetperceptions.com/2008/7/6/meaningful-404s-and-500s
 * http://henrik.nyh.se/2008/07/rails-404
 * http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-exception-handling

Copyright
=========

Copyright (c) 2008 Josh Nichols, released under the MIT license