Helpful Error Messages in-direct from your App

Custom error pages directly from your app are far better than hand-made pages or those that rails gives you by default. But if they use your controllers/helpers they may send a user into a crash-loop (arriving on /500 and boom-> /500…)

So simple add an errors controller, build some templates(name is e500, since” def 500″ will not work), set them to page caching and ping them after each deploy.

#routes.rb as lat rule
  %w[500 404 422].each do |error|
    map.connect error, :controller=>'errors', :action=>"e#{error}"
  end

#errors_controller.rb
class ErrorsController < ActionController::Base
  helper :all
  layout 'blank' #new layout, without dynamic gimmicks
  caches_page 'e500', 'e404', 'e422'

  #no authentification, so just nil
  def current_user
    nil
  end
  helper_method :current_user
end

#e500.html.erb
<h1>BOOM!</h1>

#deploy.rb
task :ping_error_pages do
  %w[500 404 422].each do |error|
    run "wget -O /dev/null localhost/#{error} --quiet"
  end
end

after "deploy:restart", *%w(
  rs:ping_error_pages
  ...
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s