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.
# File actionpack/lib/action_controller/failsafe.rb, line 21 21: def initialize(app) 22: @app = app 23: end
# File actionpack/lib/action_controller/failsafe.rb, line 25 25: def call(env) 26: @app.call(env) 27: rescue Exception => exception 28: # Reraise exception in test environment 29: if defined?(Rails) && Rails.env.test? 30: raise exception 31: else 32: failsafe_response(exception) 33: end 34: end