Catch all exceptions in a rails controller

后端 未结 6 1067
春和景丽
春和景丽 2020-11-28 02:20

Is there a way to catch all uncatched exceptions in a rails controller, like this:

def delete
  schedule_id = params[:scheduleId]
  begin
    Schedules.delet         


        
6条回答
  •  清酒与你
    2020-11-28 03:09

    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 what your goal is, you may also want to consider NOT handling exceptions on a per-controller basis. Instead, use something like the exception_handler gem to manage responses to exceptions consistently. As a bonus, this approach will also handle exceptions that occur at the middleware layer, like request parsing or database connection errors that your application does not see. The exception_notifier gem might also be of interest.

提交回复
热议问题