Skip to content

Instantly share code, notes, and snippets.

@blackfalcon
Last active December 15, 2015 14:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save blackfalcon/5274114 to your computer and use it in GitHub Desktop.
Rails 3: Flexible exception handling

Because all good things need to be duplicated ...

originally posted @plataformatec.com by José Valim

When Rails 3.0 came out, one of the features that people suddenly missed was the ability to better handle exceptions. The issue was: since Rails 3 became a lot more Rack fluent, we had to move some features to the middleware stack and this forced us to move the whole exceptions handling as well.

Rails 3.2 attempts to bring some customization back to the game by allowing you to set your own exceptions rack application that is invoked when a failure happens. For instance, you could set the exceptions application to your own router in your config/application.rb:

config.exceptions_app = self.routes

Now, every time there is an exception, your router is going to be invoked. Therefore, to render custom 404 pages, you could simply add to your router:

match "/404", :to => "errors#not_found"

And implement the logic in the controller as you wish! However, there are a few things to keep in mind if you go down this road:

  • You need to use match in your routes and not get/post/put/delete because such exceptions can happen in any HTTP request;
  • You won’t be able to see your custom exceptions in development unless you set config.consider_all_requests_local to false in your config/environments/development.rb. The reason is, if the request is considered local, Rails will always favor to show the debug exceptions page;
  • You can always access the original exception in the controller at env["action_dispatch.exception"];
  • It is not possible to set cookies, the session nor the flash after an exception happens. They all were already serialized back to the client;
  • Finally, the default exceptions application used by Rails that simply renders a page in public/STATUS.html is available here: action_dispatch/middleware/public_exceptions.rb

Remember that whatever you do in the errors controller, it should not be anything “fancy”. Keep it simple because something already went wrong with your application!

Don't forget the controller

In this example I am using the following

errors_controller.rb

Inside that, I will have the following:

class ErrorsController < ApplicationController
  def render_404
  end

  def render_500
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment