Catch all exceptions in the rail controller

Is there a way to catch all thrown exceptions in the rails controller, for example:

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 if nothing else catches the error end render :json => "ok" end 

thank

+73
ruby-on-rails
Sep 12 '10 at 8:01
source share
5 answers
 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 raise end 
+74
Sep 12 2018-10-12T00:
source share

You can also define a rescue_from method.

 class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, :with => :error_render_method def error_render_method respond_to do |type| type.xml { render :template => "errors/error_404", :status => 404 } type.all { render :nothing => true, :status => 404 } end true end end 

Depending on your purpose, you may also consider the possibility of handling exceptions for each controller. Instead, use something like exception_handler to sequentially manage exception responses. As a bonus, this approach will also handle exceptions that occur at the middleware level, such as parsing queries or database connection errors that your application does not see. The stone exception_notifier may also be of interest.

+178
Jul 21 2018-11-11T00:
source share

You can catch exceptions by type:

 rescue_from ::ActiveRecord::RecordNotFound, with: :record_not_found rescue_from ::NameError, with: :error_occurred rescue_from ::ActionController::RoutingError, with: :error_occurred # Don't resuce from Exception as it will resuce from everything as mentioned here "http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby" Thanks for @Thibaut Barrère for mention that # rescue_from ::Exception, with: :error_occurred protected def record_not_found(exception) render json: {error: exception.message}.to_json, status: 404 return end def error_occurred(exception) render json: {error: exception.message}.to_json, status: 500 return end 
+19
May 18 '15 at
source share

rescue without arguments will fix any error.

So you need to:

 def delete schedule_id = params[:scheduleId] begin Schedules.delete(schedule_id) rescue ActiveRecord::RecordNotFound render :json => "record not found" rescue #Only comes in here if nothing else catches the error end render :json => "ok" end 
+9
Sep 12 '10 at 8:32
source share

In fact, if you really want to catch everything, you simply create your own exception application that allows you to customize the behavior that is usually handled by the PublicExceptions middleware: https://github.com/rails/rails/blob/4-2- stable / actionpack / lib / action_dispatch / middleware / public_exceptions.rb

A bunch of other answers contain gems that do this for you, but there is no reason why you cannot just look at them and do it yourself.

Caution: make sure you never throw an exception in your exception handler. Otherwise you will get the ugly FAILSAFE_RESPONSE https://github.com/rails/rails/blob/4-2-stable/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L4-L22

BTW, the behavior in the controller comes from the savior: https://github.com/rails/rails/blob/4-2-stable/activesupport/lib/active_support/rescuable.rb#L32-L51

0
Feb 26 '16 at 4:38
source share



All Articles