Class: ActionController::Failsafe

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/failsafe.rb

Overview

The Failsafe middleware is usually the top-most middleware in the Rack middleware chain. It returns the underlying middleware’s response, but if the underlying middle raises an exception then Failsafe will log the exception into the Rails log file, and will attempt to return an error message response.

Failsafe is a last resort for logging errors and for telling the HTTP client that something went wrong. Do not confuse this with the ActionController::Rescue module, which is responsible for catching exceptions at deeper levels. Unlike Failsafe, which is as simple as possible, Rescue provides features that allow developers to hook into the error handling logic, and can customize the error message response based on the HTTP client’s IP.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Failsafe

Returns a new instance of Failsafe.



21
22
23
# File 'lib/action_controller/failsafe.rb', line 21

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/action_controller/failsafe.rb', line 25

def call(env)
  @app.call(env)
rescue Exception => exception
  # Reraise exception in test environment
  if defined?(Rails) && Rails.env.test?
    raise exception
  else
    failsafe_response(exception)
  end
end