Alexander Trauzzi
Alexander Trauzzi

Reputation: 7396

Custom global exception & error handler in Rails 4

What is the correct way to assign a controller action or middleware to handle all exceptions (globally) and strict parameter errors in Rails 4?

Keep in mind, I don't want to do it just for one controller.

Upvotes: 1

Views: 832

Answers (2)

Yarin
Yarin

Reputation: 183549

Use exceptions_app.

application.rb

class Application < Rails::Application
   ...
   config.exceptions_app = lambda do |env|
        ExceptionController.action(:render_error).call(env)
   end
   ...
end

exception_controller.rb

class ExceptionController < ActionController::Base
    layout 'application'

    def render_error
        @exception = env["action_dispatch.exception"]
        @status_code = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
        render :error_page, status: @status_code, layout: true
    end
end

Taken from Rails 3.2 error handling with exceptions_app (Example)

Upvotes: 0

Sp&#228;tzle
Sp&#228;tzle

Reputation: 747

Well, the obvious solution is the application controller (/app/controllers/application_controller.rb), should work fine if your controllers have the 7 INCSEUD actions.

Upvotes: 1

Related Questions