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
ExceptionHandler
deploys 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 custom 404
error uses your own layout
. The 500 & other errors
are server
issues, and so we have included an errors
layout (/views/layouts/errors.html.erb
).
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 |
---|---|
Uses errors layout | Uses standard layout |
####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.
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
- ExceptionHandler.setup do |config|
- config.db = true
- end
###Step1
You need to reference the exception_handler
gem. Once you have downloaded the gem, you'll be able to deploy it in your application.
Add this line to your application's Gemfile:
gem 'exception_handler'
And then execute:
$ bundle
Or install it yourself as:
$ gem install exception_handler
--
###Step2
Run:
rails generate exception_handler:install
This will create config/initializers/exception_handler.rb
. Whilst not vital, it will give you
access to the configuration options which can change the gem's behavior:
--
###Database
If you want to set up the database, you will need to use the migration installer
:
$ rails generate exception_handler:migration
This creates:
$ rake db:migrate
This will migrate the datatable for you. Now you need to change config.db = true
###Configuration
Exception handler comes with 3 installers
You only need to use rails generate exception_handler:install
. The others allow you to include files on your system, or create the errors
table.
# General
$ rails generate exception_handler:install #-> installs "config" file (initializer)
# Migration
$ rails generate exception_handler:migration #-> generates migration for "errors" table
# Files
$ rails generate exception_handler:views #-> controller, models, views & assets
$ rails generate exception_handler:views -v views controllers models assets #-> remove as appropriate to install individual assets
###Development Environment
config.exceptions_app
is only 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:
#config/environments/development.rb
config.consider_all_requests_local = false # true
You should change this setting if you wish to test your styling in development mode. Please note it should be temporary
--
###Production Environment
No action required
###Demo
404 Error | 500 Error |
---|---|
Link | Link |
If you need help, you may consider:
-
Watching this video tutorial:
-
Read our tutorial
-
Ask on StackOverflow
-
Go on the gem support page
- Fork it ( https://github.com/richpeck/exception_handler/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request