ExceptionHandler helps you can create custom 404 & 500 production error pages (adapted from 1 & 2).
ExceptionHandler is the only production-ready custom error page solution for Rails 4 & 5.
Customize 404 and 500 error pages to be branded to your application in production:
Responsive Branded Error Pages Middleware Exception Handling Fully Customizable
The most feature-rich, stable & effective solution for custom error pages in Rails 4 & 5.
New Controller New Middleware Full Test Suite FULLY Rails 4 & 5 Compatible
Brand new controller
, middleware
& options have made ExceptionHandler
even more powerful & efficient. Now you can use ExceptionHandler
directly with a single click -- plug and play custom exception pages:
ExceptionHandler uses config.exceptions_app
to catch & send errors to the exceptions
controller.
It uses data stored in Rack
(message
, details
, user agent
) to populate its custom view
. This gives you the ability to maintain your branding even when your app experiences an exception.
The power of ExceptionHandler
lies in its capacity to access the ActiveDispatch::ShowExceptions
middleware:
Rails invokes config.exceptions_app
whenever an exception is raised.
ExceptionHandler injects our ExceptionController
into this hook to provide the most efficient response:
config.exceptions_app
sets the exceptions application invoked by theShowException
middleware when an exception happens. Defaults toActionDispatch::PublicExceptions.new(Rails.public_path)
.
As opposed to other exception suites (which use the routes
), this gives you DIRECT access to the exception through the middleware stack, straight to the ExceptionController
:
ExceptionHandler uses custom middleware to extract all the exception data for the request. Not only is this the most succinct, efficient way to do this, it also allows you to customize the entire fault-recovery process.
It's completely unique - the only professional solution to catch, process & handle exceptions in Rails.
You Don't Need Any Configuration To Run ExceptionHandler
gem install "exception_handler"
or
gem 'exception_handler', '~> 0.5.0' #-> Gemfile
ExceptionHandler
's new config system (introduced in 0.4.7
) stores all the defaults - you just need to install the gem & let it run.
If you want to change any settings (detailed below), you simply need to change config/application.rb
/ config/environments/your_env.rb
. The POWER of this new config system means you're able to deploy ExceptionHandler
in the most unobtrusive, versatile way possible.
ExceptionHandler 0.5
has the SIMPLEST config possible...
The MAGIC is that it uses environment files
.
You can apply the above options to any of Rails' environment files, allowing you to customize the ExceptionHandler
gem without inducing ANY overhead. Initializer
files just get in the way.
IMPORTANT
If you're upgrading from >=
0.4.7
, you need to remove yourexception_handler
initializer.We've changed the load process to use Rails app config - you don't need the
exception_handler
initializer any more
As can be seen in the config class
, the following are all the options exception_handler
accepts:
# config.rb
DEFAULTS = {
dev: false, #-> defaults to "false" for dev mode
db: false, #-> defaults to :errors if true, else input "table_name" as string
email: false, #-> need to integrate
social: {
:facebook => 'frontline.utilities', #-> Facebook handle
:twitter => 'frontlineutils', #-> Twitter handle
:youtube => 'frontlineutils', #-> YouTube handle
:linkedin => 'frontline-utilities', #-> LinkedIn handle
:fusion => 'frontlineutils', #-> Fusion handle
:url => {
:facebook => 'https://facebook.com', #-> no need to edit
:twitter => 'http://twitter.com', #-> no need to edit
:youtube => 'https://youtube.com/user', #-> no need to edit
:linkedin => 'https://linkedin.com/company', #-> no need to edit
:fusion => 'https://frontlinefusion.com', #-> no need to edit
},
},
layouts: {
'404' => nil, #-> 404 Callback (needs improving big time) Use the following: '404' => <<-EOF redirect_to root_url, notice: "Hello" EOF
'400' => nil, #-> layout for 400 error code (404 should only be used as response)
'500' => 'exception'
},
}
The above are defaults.
You can change the options for each environment file
:
config/application.rb
config/environments/development.rb
config/environments/production.rb
config/environments/staging.rb
The benefit of this is that it gives you the ability to customize ExceptionHandler
for any of your environments. Contrary to a shitty initializer
, you're able to sculpt the gem to work FOR YOU.
Want to test in the dev
environment?
#config/application.rb
config.exception_handler = {
dev: true # -> Runs in development mode WITHOUT changing the app environment files
}
config.exceptions_app
only works when you have config.consider_all_requests_local = true
, which is true
in development
:
config.consider_all_requests_local
is a flag. If true then any error will cause detailed debugging information to be dumped in theHTTP
response, and theRails::Info controller
will show the application runtime context in/rails/info/properties
.True
by default in development and test environments, and false in production mode. For finer-grained control, set this to false and implementlocal_request?
in controllers to specify which requests should provide debugging information on errors.
If you wish to test ExceptionHandler
in development
, you'll have to use the dev: true
option in your exception_handler
config, or change config.consider_all_requests_local = true
in config/development.rb
.
One of the most critial aspects of ExceptionHandler
is the custom Exception
layout.
If you want to change the layout (for 500
error pages), you need to use the following:
You'll need to generate it into your application before being able to edit:
ExceptionHandler
comes with its own inbuilt views & controller.
If you want to change it, you need to add the views into your app with the generator
$ rails generate exception_handler:views #-> controller, models, helpers, views & assets
$ rails generate exception_handler:views -v views controllers models helpers assets #-> remove as appropriate to install individual assets
--
ExceptionHandler
uses exceptions#show
-- 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)
If you want to store exceptions in your db
, you will need to set up a migration:
$ rails generate exception_handler:migration
$ rake db:migrate
You will also need to ensure your config db option is either true
or "table_name"
:
IMPORTANT
ExceptionHandler
's new config system isenvironment
agnostic.Where you declare your
db
setting will change depending on your environmentIf you declare
db
inapplication.rb
, it will be applicable for allenvironments
; only declaring inproduction.rb
will set it for production only.
Github Issue (1hr) | Stackoverflow (24hrs) |
---|---|
We use ExceptionHandler
in production, so we have a vested interest in keeping it running smoothly.
Asking in either of the above services will give you a response. Don't be shy, we want to make this the best exception handling
middleware for Rails.
--
- Next release
- Added locales
- Email notifications
- Full test suite (finally)
- Rails 4.2 & Rails 5.0 native (
request.env
fix) - Controller fixed
DB
fixed- Legacy initializer support (more)
- Rails asset management improvement
- Cut out most dev files from gem (drastic reduction in gem size)
- New config system (more)
- Fixed controller layout issues
- Streamlined middleware
- New layout & interface implementation