Error Handling Rails 3
Contents |
a GitHub account Sign in Create a gist now Instantly share code, notes, and snippets. Star 40 Fork 10 Sujimichi/application_controller.rb Created Apr
Rails Error Handling Best Practices
10, 2012 Embed What would you like to do? Embed Embed this rails api error handling gist in your website. Embed Share Copy sharable URL for this gist. Share Clone via HTTPS Clone
Rails Ajax Error Handling
with Git or checkout with SVN using the repository's web address. HTTPS Learn more about clone URLs Download ZIP Code Revisions 8 Stars 40 Forks 10 Rails 3 rescue_from ruby on rails error handling routing error, variation on solution from http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution Raw application_controller.rb class ApplicationController < ActionController::Base ... #Problem: #In rails 3.0.1+ it is no longer possible to do this anymore; # rescue_from ActionController::RoutingError, :with => :render_not_found # #The ActionController::RoutingError thrown is not caught by rescue_from. #The alternative is to to set a catch-all route to catch all unmatched routes and rails controller error handling send them to a method which renders an error #As in http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution #A slight variation on this is to add the route and method, but have the method raise an ActionController::RoutingError #This way you can keep the usage of rescue_from and treat all error handling in the same way. #continue to use rescue_from in the same way as before unless Rails.application.config.consider_all_requests_local rescue_from Exception, :with => :render_error rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found rescue_from ActionController::RoutingError, :with => :render_not_found end #called by last route matching unmatched routes. Raises RoutingError which will be rescued from in the same way as other exceptions. def raise_not_found! raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}") end #render 500 error def render_error(e) respond_to do |f| f.html{ render :template => "errors/500", :status => 500 } f.js{ render :partial => "errors/ajax_500", :status => 500 } end end #render 404 error def render_not_found(e) respond_to do |f| f.html{ render :template => "errors/404", :status => 404 } f.js{ render :partial => "errors/ajax_404", :status => 404 } end end ... end Raw routes.rb #make this your last
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Rails .find Error Handling
workings and policies of this site About Us Learn more about rails catch error Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Rails Exception Message
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. https://gist.github.com/2349565 Join them; it only takes a minute: Sign up Catch all exceptions in a rails controller up vote 58 down vote favorite 27 Is there a way to catch all uncatched exceptions in a rails controller, like this: def delete schedule_id = params[:scheduleId] begin Schedules.delete(schedule_id) rescue ActiveRecord::RecordNotFound render :json => "record not found" rescue ActiveRecord::CatchAll #Only comes in here http://stackoverflow.com/questions/3694153/catch-all-exceptions-in-a-rails-controller if nothing else catches the error end render :json => "ok" end Thank you ruby-on-rails share|improve this question edited Sep 12 '10 at 8:39 George Jempty 5,478948111 asked Sep 12 '10 at 8:01 Neigaard 1,06642244 add a comment| 5 Answers 5 active oldest votes up vote 55 down vote accepted begin # do something dodgy rescue ActiveRecord::RecordNotFound # handle not found error rescue ActiveRecord::ActiveRecordError # handle other ActiveRecord errors rescue # StandardError # handle most other errors rescue Exception # handle everything else end share|improve this answer answered Sep 12 '10 at 8:36 Chris Johnsen 99.4k19142155 12 Isn't the rule to NEVER catch Exception? –RonLugge Nov 18 '14 at 1:13 1 but how can I catch all type in rescue => e block only? –Matrix Feb 19 '15 at 15:03 1 @RonLugge it depends entirely on the situation at hand. applying "never" as a rule of thumb is a bad idea. –Justin Skiles Feb 25 '15 at 0:04 5 @JustinSkiles Catching Exception will catch syntax errors (and interrupt signals too). Give
an exception in production occurs. The problem with these pages is, that they do not fit well into any design and do not tell the user what really went wrong. http://geekmonkey.org/2012/10/exception-applications-in-rails-3-2/ In Rails 2.x it was possible to catch all thrown exceptions using the rescue_from http://api.rubyonrails.org/classes/ActiveModel/Errors.html 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 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 error handling 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 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 rails error handling 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 mentions that the a "is actually a parameter in the Rails 3 Route Globbing technique. For example, if your url was /this-url-does-not-exist, then params[:a] equals /this-url-does-not-exist". While this solution looks nice at first, it will create problems when your app uses engines. Exception Application The release notes of Rails 3.2 introduce a new solution to the workarounds above. Added config.exceptions_app to set the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to ActionDispatch::PublicExceptions.new(Rails.public_path). The best solution I have come across so far which utilizes the new config setting wa
helpers. A minimal implementation could be: class Person # Required dependency for ActiveModel::Errors extend ActiveModel::Naming def initialize @errors = ActiveModel::Errors.new(self) end attr_accessor :name attr_reader :errors def validate! errors.add(:name, :blank, message: "cannot be nil") if name.nil? end # The following methods are needed to be minimally implemented def read_attribute_for_validation(attr) send(attr) end def self.human_attribute_name(attr, options = {}) attr end def self.lookup_ancestors [self] end end The last three methods are required in your object for Errors to be able to generate error messages correctly and also handle multiple languages. Of course, if you extend your object with ActiveModel::Translation you will not need to implement the last two. Likewise, using ActiveModel::Validations will handle the validation related methods for you. The above allows you to do: person = Person.new person.validate! # => ["cannot be nil"] person.errors.full_messages # => ["name cannot be nil"] # etc.. Methods # [], []= A add, add_on_blank, add_on_empty, added?, as_json B blank? C clear, count D delete E each, empty? F full_message, full_messages, full_messages_for G generate_message, get H has_key? I include? K key?, keys M marshal_dump, marshal_load N new S set, size T to_a, to_hash, to_xml V values Included Modules Enumerable Constants CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict] MESSAGE_OPTIONS = [:message] Attributes [R] details [R] messages Class Public methods new(base) Link Pass in the instance of the object that is using the errors object. class Person def initialize @errors = ActiveModel::Errors.new(self) end end Source: show | on GitHub # File activemodel/lib/active_model/errors.rb, line 72 def initialize(base) @base = base @messages = apply_default_array({}) @details = apply_default_