Class: RailsDynamicErrors::DynamicErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_dynamic_errors/middleware/dynamic_errors.rb

Overview

This is a Rack compatible middleware that implements the key functionality of the rails_dynamic_errors gem. It serves two roles:

  • intercept unhandled exceptions and error conditions from the Rail application it is inserted into

  • generate dynamic error pages for the exceptions and error conditions it is configured to

The middleware is inserted into the Rails application’s middleware stack automatically. As such, the end user should never need to use it directly.

In order to avoid disrupting standard Rails functionality, this middleware respects the Rails exception handling related configuration options. As such, it will only process exceptions and error conditions if:

  • the request is not local (action_dispatch.show_detailed_exceptions is false)

  • the environment is set to display exceptions (rather than raise them)

Configuration of which exceptions and error conditions to catch is done within the main Rails application’s configuration, using an option set namespaced to rails_dynamic_errors. Specifically:

Rails.application.config.rails_dynamic_errors.http_error_status_codes_to_handle

This option must be an array of integer HTTP error status codes (404, etc.)

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ DynamicErrors

Initialize the middleware (standard Rack method)



27
28
29
# File 'lib/rails_dynamic_errors/middleware/dynamic_errors.rb', line 27

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Call the middleware (standard Rack method)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_dynamic_errors/middleware/dynamic_errors.rb', line 32

def call(env)
  response = @app.call(env)
  # 404 errors for unmatched routes aren't actually raised until
  # ActionDispatch::DebugExceptions. If we're supposed to catch 404s and
  # the application indicates there was no matching route, throw and
  # handle a 404 generating exception
  if can_process_exceptions?(env) && catch_not_found_error? && request_unhandled?(response)
    raise ActionController::RoutingError.new("No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}")
  else
    response
  end
rescue Exception => exception
  if can_process_exceptions?(env)
    process_exception(env, exception)
  else
    raise exception
  end
end