Exception Handler

Gem Version Code Climate Dependency Status Coverage Status Build Status

ExceptionHandler Rails Gem (adapted from this tutorial & this tutorial)

Works with the config.exceptions_app hook in Rails' middleware stack:

config.exceptions_app sets the exceptions application invoked by the ShowExceptionmiddleware
when an exception happens. Defaults to ActionDispatch::PublicExceptions.new(Rails.public_path).

Custom Error Pages

You can deploy custom error pages. These allow you to serve your own design error pages in production; showing both the error, and the problem. This is a big step forward from the standard Rails error reporting facility

There are two types of error page:

- 404 errors
- 500 errors (+ other)

The 404 error is standard (missing page) - we use your own layout for this. The 500 & other errors are server issues, and so we have included an errors layout (/views/layouts/errors.html.haml).

The errors layout in important. If you try and load your "standard" layout with an internal server error, all your "supporting" functionality is called too. Problem? You're likely going to cause even more errors.

Custom error pages are included by default. You can change them by using the rails generate exception_handler:assets --views:

500 Errors 404 Errors
500 Server Error Pages 404 Server Error Pages

Saving Errors To DB

Adapted & refactored from this tutorial

Sometimes, you want to save your errors to your database (admin areas, multi-tenant apps, etc). We've included some middleware which captures the exceptions & saves them to the db:

- Model (class) Name
- Error Message
- Stack trace
- Target URL
- Referrer URL
- Params
- User Agent (Browser Details)

This db allows you to track, read & repair errors in your application on the fly. We deploy this in our admin areas, to help us appreciate any issues on our client apps.

500 Server Error Pages

This functionality is disabled by default

To enable, you need to do the following:

- rails generate exception_handler:install #-> will install config initializer
- rails generate exception_handler:migration #-> will create migration (for `errors` table)
- rake db:migrate #-> creates `errors` table

- config/initializers/exception_handler.rb

Installation

Add this line to your application's Gemfile:

gem 'exception_handler'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exception_handler

Configuration

# General
$ rails generate exception_handler:install

# Migration
$ rails generate exception_handler:migrate

# Files
$ rails generate exception_handler:assets

Usage

Dev

#config/environments/development.rb
config.consider_all_requests_local = false # true

config.exceptions_app is used in Rails' production environment. Therefore, if you wish to test the gem in dev, you'll need to make your app process requests as production for now. This is a temporary step, and will be resolved in a new version

Production

No action required

Demo

Demo to be put onto Heroku etc


Support

If you need help, you may consider:

  1. Watching this video tutorial:

ExceptionHandler Update

  1. Read our tutorial
  2. Ask on StackOverflow
  3. Go on the gem support page

Contributing

  1. Fork it ( https://github.com/richpeck/exception_handler/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