0.0
Low commit activity in last 3 years
No release in over a year
We're forever writing the same rescue code for our Rails apps. This gem will hopefully stop that :-)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0

Runtime

>= 4.2, < 7.0.0
 Project Readme

SimpleErrors - a simple way to rescue errors in Rails applications

On practically every job we rescue errors in the ApplicationController in the same way, so it was time for a gem. This gem rescues from defined error classes (along with the standard Rails ActiveRecordNotFound and RoutingError classes) to either a 404 or 500 error page.

If you need something more complicated than that (e.g. 403 errors), you still need to rescue in ApplicationController. They're not mutually exclusive, though.

Installation

Add this line to your application's Gemfile:

gem 'simple_errors'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_errors

Configuration and setup

There's a generator to set up the files you need, and add a mixin in the application controller:

rails generate simple_errors

This will set up:

  • /app/views/layouts/error.html.erb - a layout for errors.
  • /app/views/errors/500.html.erb - the 500 error page
  • /app/views/errors/404.html.erb - the 404 error page

It also removes the default error pages in /public.

Configuring ApplicationController

After you've run the generator, your Application Controller will look like this:

class ApplicationController < ActionController::Base
  include SimpleErrors::Rescue
  # other stuff
end

There are a couple of class methods provided by this gem to configure it.

Rescuing from other classes

If your application raises errors of other types, you might want to rescue to 404 instead of 500. Do that by adding a call to rescue_with_not_found, passing one or many classes:

class ApplicationController < ActionController::Base
  rescue_with_not_found Rooftop::RecordNotFoundError, Rooftop::Rails::AncestorMismatch
  include SimpleErrors::Rescue
  # other stuff
end  

Doing something before rescuing

Sometime you want your error page to render something - for example, call the same method as a before_filter does to get navigation items.

class ApplicationController < ActionController::Base
  rescue_with_not_found Rooftop::RecordNotFoundError, Rooftop::Rails::AncestorMismatch
  include SimpleErrors::Rescue
  before_rescue do
    @foo = SomeMenu.find(1234)
  end
end

The before_rescue block is evaluated in the context of the rendering call, so if you set @foo in the example above it'll be available in your view.

Contributing

  1. Fork it ( https://github.com/errorstudio/simple_errors/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request