0.01
No commit activity in last 3 years
No release in over 3 years
Rescue Handler for Rack
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.2
>= 0
>= 0
~> 0.3
>= 0
>= 0
>= 0.10
 Project Readme

Rack::Rescue

Rack Rescue is a middleware for handling exceptions in your rack application.

Rack::Rescue will set the correct status, render a template, and optionally run any number of callbacks when an exception is experienced.

Just insert the middleware in your stack, and any unhandled exception will trigger the handling. Unknown exceptions trigger a basic error page, with a 500 status.

Rather than launch into a long-winded chat about what it can do, I’ll suffice with a list.

  • Per exception status codes
  • Per exception templates (with a fallback template)
  • Content type negotiation (:html, :xml, :js etc)
  • cascading tempate locations
  • template naming of the usual <name>.<format>.<(erb|haml|other tilt template)>
  • template naming including the RACK_ENV
    • <name>.<RACK_ENV>.<format>.<(tilt template engine ext)>
    • If the more specific template is not found, it will fall back to just name, format and ext
  • template inheritance
  • Arbitrary exception hooks
  • Integration with Wrapt layouts

Basic Usage

In config.ru


  use Rack::Rescue
  run MyApp

Adding Exceptions


  # Add to the default list
  Rack::Rescue::Exceptions.add_defaults("MyException", "AnotherException", :status => 404)
  Rack::Rescue::Exceptions.add_defaults("DifferetOne", :status => 455, :template => "different")

  # Add to a specific Rack::Rescue
  use Rack::Rescue do |rr|
    rr.add("SomeException", :status => 478, :template => "something")
  end

Add a template location

By default templates should be located in

<handler root>/rack_rescue_templates//

To add a new handler root


  Rack::Rescue::Handler.roots << File.join(Dir.pwd, "views")

This will then look in “./views/rack_rescue_template” directory for you templates. If it doesn’t find them in there it will fall back to the templates located in any other roots, or finall to the default templates it ships with.

Template Naming

Templates are named with the following form, in the following preference:

1. name.env.format.ext # error.development.html.erb
2. name.format.ext # error.html.erb

If the first is not matched, the second will be also checked

Arbitrary Exception hooks

When an exception is detected, any exception hooks that have been declared are executed before the template is rendered and the response returned to the client.


  Rack::Rescue.add_handler do |exception, env, options|
    # email me the exception!
  end

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a
    future version unintentionally.
  • Commit, do not mess with rakefile, version, or history.
    (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Daniel Neighman. See LICENSE for details.