Sick of the standard Rails error pages?
Need a custom layout to show professional-looking errors in production?
exception_handler
is exactly what you need!
ExceptionHandler provides error handling for Rails 4+ apps (adapted from 1 & 2).
It hooks into the config.exceptions_app
middleware:
With ExceptionHandler, you can create custom 404 & 500 production error pages:
ExceptionHandler catches errors in the middleware hook (config.exceptions_app
), channeling the users to our custom exceptions
controller. It then dissects data such as the message
, details
, user agent
, etc, redirect the user to a custom view
, storing it in the db:
40x Errors | 50x Errors |
---|---|
layouts/application.html.erb | layouts/errors.html.erb |
All exceptions in Rails are handled by the ActiveDispatch::ShowExceptions
middleware.
This is invoked through a hook called config.exceptions_app
, accessed through the environment
files of rails application.rb
, environments/development.rb
, environments/production.rb
etc.
Whilst it's common practice to use config.exceptions_app = self.routes
to send exceptions to your routes, ExceptionHandler
hooks directly into the middleware, giving us access to all the data:
Installation
gem install 'exception_handler'
or
gem 'exception_handler', '~> 0.4' # Gemfile
$ bundle
You can see ExceptionHandler
on RubyGems
--
Config
Optional
If you wish to change any of the default settings (listed below), run the following command:
$ rails g exception_handler:install
This will deploy a file to config/initializers/exception_handler.rb
, which you'll be able to edit as required.
You can run ExceptionHandler
just from the gem
if you wish.
--
DB
Optional
If you want to set up database support (IE have exceptions saved to a table), you need to create a migration:
$ rails generate exception_handler:migration
$ rake db:migrate
This is an optional step. If you want to save the data, you will also need to ensure your config.db
option is correct:
--
Views
Optional
If you want to change the views, you can have them put into your app:
$ 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
The views will be appended to your app, and you'll be able to edit them as required.
Show
The view is located in app/views/exception_handler/show.html.erb
designed to work for all exceptions.
By default, it is split for use with different layouts, data being available to both.
This works exactly the same as the other views in your app (IE the exceptions
controller invokes the show
action)
Layout
One of the most critial aspects of ExceptionHandler is the custom Error
layout.
If you want to change the layout (for 500 error pages), you need to use the following:
Development
config.exceptions_app
is only used in Rails' production environment.
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:
You should change this setting if you wish to test your styling in development mode. It should be temporary (you need to change it back once you have it looking how you like)
Usable_Type & Usable_ID
Bugs
ApplicationController
A significant issue exists in respect to the inheritance of exceptions_controller
.
Our current version (0.4
) inherits from the ApplicationController
. This causes a problem for many applications.
If you use any sort of before_action
callback in your controller, especially to populate variables, this
will cause untold problems in your exceptions_controller
.
500
errors are server faults; trying to get the server to call more data will simply not work. It causes
an exception loop, leading to catastrophic failure.
Support
- Watch the video tutorial (soon)
- Create an "issue"
- Contact support (soon)
- Ask on StackOverflow
Contribution
- 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