For error reporting, there is a convenient pearl called exception_notification , which sends you mail every time an exception occurs in your application.
If you also want to get detailed and fully customizable error reports on the page, you will need to code a bit. Here is what I use (I would give you a link to where I got this from, but I can no longer find it :()
Define ErrorsController as follows:
class ErrorsController < ApplicationController ERRORS = [ :internal_server_error, :not_found, :unprocessable_entity, :unauthorized ].freeze ERRORS.each do |e| define_method e do respond_to do |format| format.html { render e, :status => e } format.any { head e } end end end
end
Create an initializer to connect the controller to the Rails exception handling chain:
require 'action_dispatch/middleware/show_exceptions' module ActionDispatch class ShowExceptions private def render_exception_with_template(env, exception) env['exception'] = exception body = ErrorsController.action(rescue_responses[exception.class.name]).call(env) log_error(exception) env.delete 'exception' body rescue Exception => e log_error(e) render_exception_without_template(env, exception) end alias_method_chain :render_exception, :template end end
Create a view for each of the errors in ErrorsController::ERRORS (i.e. app / views / errors / not_found.html.erb, etc.).
I do not claim that this is the best method, but it still served me well (you can easily add pages for new errors, configure how and what you want to display for each error separately).
source share