« back to all cards in this deck
Posted about 2 years ago. Visible to the public. Repeats.

Custom error pages in Rails 3.2

Starting with Rails 3.2, there is a nicer way to display custom error pages (i.e. “page not found”) that does not interfere with ExceptionNotifier. (I’m not sure about Airbrake et al.)

José Valim has posted this on his blog, here is a slightly more verbose rundown:

  1. Register your own app as the application responsible for showing error pages by adding

    config.exceptions_app = self.routes

    to your config/application.rb.

  2. Add routes for the error pages, by appending this to your config/routes.rb:

    match '/404' => 'errors#not_found' match '/422' => 'errors#server_error' match '/500' => 'errors#server_error'
  3. Add an ErrorsController like this one:

    class ErrorsController < ApplicationController def not_found render :status => 404, :formats => [:html] end def server_error render :status => 500, :formats => [:html] end end
  4. Add the corresponding views.

  5. Add some tests, for example a cucumber feature:

    @allow-rescue Scenario: 404 page When I go to the path "/foobar" Then the page should be titled "Page not found" And I should see "We're sorry, but couldn't find the page you requested" And the response status should be "404"
  6. (Optional) In a request spec, you’ll need to stub a few config variables (in the cached Rails environment config):

    # spec/requests/error_pages_spec.rb describe "Error pages" do def handle_errors env_config = Rails.application.env_config show_exceptions = env_config['action_dispatch.show_exceptions'] local_requests = env_config['action_dispatch.show_detailed_exceptions'] # Disables Rails built-in error reports, so our custom error application # can handle them and render it's own templates. This overrides the cached # setting in Rails.application.config.consider_all_requests_local env_config['action_dispatch.show_detailed_exceptions'] = false # Render exception templates instead of raising exceptions. # This is the cached setting for # Rails.application.config.action_dispatch.show_exceptions env_config['action_dispatch.show_exceptions'] = true yield env_config['action_dispatch.show_exceptions'] = show_exceptions env_config['action_dispatch.show_detailed_exceptions'] = local_requests end around(:each) do |example| handle_errors(&example) end # ... end

Additional notes

  • You cannot modify headers, so you can’t modify the session, cookies etc.
  • You won’t see this in development, except if you set config.consider_all_requests_local = false in your config/development.rb.
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Author of this card:

Avatar
Tobias Kraze
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code

License for source code

All source code included in the card Custom error pages in Rails 3.2 is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source.

The MIT License (MIT)

Copyright (c) 2012-2014 makandra GmbH

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
Posted by Tobias Kraze to makandropedia