Error Handling In Rails 3.1
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ruby on rails 3.1 global exception handler up vote 1 down vote favorite I'm developing an app with Rails 3.1.2 but I can't find some documentation that works with errors / exception (like 404) on this version of rails. i have tried things like: In application controller rescue_from ActiveRecord::RecordNotFound,ActionController::RoutingError, ActionController::UnknownController, ActionController::UnknownAction, :NoMethodError, :with => :handle_exception def handle_exception render :template => 'error_pages/error' end environment/development.rb config.consider_all_requests_local = false Where can I find a solution? Thanks in advance... ruby exception-handling ruby-on-rails-3.1 share|improve this question edited Dec 13 '11 at 20:00 Kevin 30.3k84986 asked Dec 13 '11 at 19:42 Carlos Raul Aparicio Hernandez 1714 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted This should work: In application controller class NotFound < StandardError; end rescue_from NotFound, :with => :handle_exception def handle_exception render :template => 'error_pages/error' end share|improve this answer edited Jul 25 '13 at 21:13 answered Jan 9 '12 at 2:08 Alain Beauvois 4,23913023 I confirm: this works. –Alain Beauvois Feb 7 '12 at 11:14 Just want to add: It seem to be invalid to use "End" instead of "end". –Sebastian vom Meer Jul 23 '13 at 9:00 add a comment| up vote 0 down vote Look at action_dispatch/middleware/show_exceptions. From the documentation in the source: # This middleware rescues any exception returned by the application # and wraps them in a format for the end user. Short story short, it renders ActionDispatch::ShowExceptions.render_exception when the wrapped application (Rails, in your case), encounters an unrescued exception. If you look through the default implementation, it ends up rendering something like public/500.html, which is what you see in the production environment. Overwrite the method or method chain it as you see fit to add your own implementation. share|improve this answer answered Dec 13 '11 at 20:11 Steven Xu 10.6k94395 add a comment| Your Answer draft saved draft discar
a GitHub account Sign in Create a gist now Instantly share code, notes, and snippets. Star 36 Fork 15 gonzedge/application_controller.rb Created Jan 5, 2012 Embed What would you like to do? Embed Embed this gist in your website. Embed Share Copy sharable URL for this gist. Share Clone via HTTPS Clone with Git or checkout with SVN using the repository's web address. HTTPS Learn more about clone URLs Download ZIP Code Revisions 5 Stars 36 Forks 15 http://stackoverflow.com/questions/8495151/ruby-on-rails-3-1-global-exception-handler Rails 3.1 - Adding custom 404 and 500 error pages Raw application_controller.rb class ApplicationController < ActionController::Base # ... unless Rails.application.config.consider_all_requests_local rescue_from Exception, with: lambda { |exception| render_error 500, exception } rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception } end private def render_error(status, exception) respond_to do |format| format.html { render template: "errors/error_#{status}", https://gist.github.com/1563416 layout: 'layouts/application', status: status } format.all { render nothing: true, status: status } end end # ... end Raw error_404.html.haml %h2 404 %div %h3 We're sorry %p The content that you requested could not be found. %p You tried to access '#{@not_found_path}', which is not a valid page. %p Want to %a{href: root_path} go back to our home page and try again? Raw errors_controller.rb class ErrorsController < ApplicationController def error_404 @not_found_path = params[:not_found] end def error_500 end end Raw generate.sh rails generate controller errors error_404 error_500 Raw remove_routes.rb get "errors/error_404" get "errors/error_500" Raw routes.rb unless Rails.application.config.consider_all_requests_local match '*not_found', to: 'errors#error_404' end karellm commented Jan 16, 2012 I think the application_controller.rb should declare the error methods as follow: def render_404(exception) def render_500(exception) instead of: def render_404(error) def render_500(error) Owner gonzedge commented Jan 16, 2012 Thanks @karellm for the correction! RudthMael commented Apr 2, 2012 How do you make sure the exception is logged in the log/RAILS_ENV.log file? Owner gonzedge commented Apr 5, 2012 @RudthMael, I think that adding
an exception in production occurs. The problem with these pages is, that they do http://geekmonkey.org/2012/10/exception-applications-in-rails-3-2/ not fit well into any design and do not tell the user what really went wrong. In Rails 2.x it was possible to catch all thrown exceptions using http://apidock.com/rails/ActionController/Rescue/ClassMethods/rescue_from the rescue_from method in the ApplicationController. class ApplicationController < ActionController::Base rescue_from(ActionController::RoutingError) { render :template => 'errors/404' } end In Rails 3 however routing was extracted into its error handling own middleware called ActionDispatch. While this is a good thing, it has created a lot of confusion on how to handle ActionController::RoutingError. Having routing done by a middleware means that in case of a routing error the ApplicationController will not get executed, and therefore rescue_from can not handle any routing errors. Instead ActionDispatch provides error handling in a class ActionDispatch::ShowException to handle exceptions that happen in the middleware. Over time the community has proposed several solutions to solve this problem. Override ShowException Matthew Gibbons proposed in his article Rails 3.0 Exception Handling to override the ShowException class in an initializer. The overriden method render_exception will call a method on a custom ErrorsController and render out the exception in the layout of the page. The advantage of this approach is that the user will see the error message rendered in the layout of the page and it is up to you on how to properly present the error message to the user. While this was a viable solution for Rails 3.0 and 3.1, in Rails 3.2 it will not work anymore. Catch all route Tian from TechOctave proposed in his article Rails 3.0 rescue from Routing Error Solution to introduce a catch all route in config/routes.rb. Yourapp::Application.routes.draw do #Last route in routes.rb match '*a', :to => 'errors#routing' end He me
1.1.1 1.1.6 1.2.0 1.2.6 2.0.0 (0) 2.0.3 (0) 2.1.0 (2) 2.2.1 2.3.2 2.3.8 3.0.0 3.0.5 3.0.9 3.1.0 3.2.1 3.2.3 3.2.8 3.2.13 4.0.2 4.1.8 4.2.1 4.2.7 What's this? Related methods Instance methods (3) call_with_exception (<= v2.3.8) process_with_exception (<= v2.2.1) rescue_from (<= v2.1.0) = private = protected Method deprecated or moved This method is deprecated or moved on the latest stable version. The last existing version (v2.1.0) is shown here. These similar methods exist in v4.2.7: ActiveSupport::Rescuable::ClassMethods#rescue_from rescue_from(*klasses, &block) public Rescue exceptions raised in controller actions. rescue_from receives a series of exception classes or class names, and a trailing :with option with the name of a method or a Proc object to be called to handle them. Alternatively a block can be given. Handlers that take one argument will be called with the exception, so that the exception can be inspected when dealing with it. Handlers are inherited. They are searched from right to left, from bottom to top, and up the hierarchy. The handler of the first class for which exception.is_a?(klass) holds true is the one invoked, if any. class ApplicationController < ActionController::Base rescue_from User::NotAuthorized, :with => :deny_access # self defined exception rescue_from ActiveRecord::RecordInvalid, :with => :show_errors rescue_from 'MyAppError::Base' do |exception| render :xml => exception, :status => 500 end protected def deny_access ... end def show_errors(exception) exception.record.new_record? ? ... end end Show source # File actionpack/lib/action_controller/rescue.rb, line 90 def rescue_from(*klasses, &block) options = klasses.extract_options! unless options.has_key?(:with) block_given? ? options[:with] = block : raise(ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument.") end klasses.each do |klass| key = if klass.is_a?(Class) && klass <= Exception klass.name elsif klass.is_a?(String) klass else raise(ArgumentError, "#{klass} is neither an Exception nor a String") end # Order is important, we put the pair at the end. When dealing with an # exception we will follow the documented order going from right to left. rescue_handlers << [key, options[:with]] end end Register or log in to add new notes. tadman - April 9, 2009 4 thanks Method has moved to ActionController::Rescue::ClassMethods module