Class: Agilibox::ErrorsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/agilibox/errors_middleware.rb

Constant Summary collapse

MAINTENANCE_ERRORS =
[
  "ActiveRecord::ConnectionTimeoutError",
  "connections on port 5432",
  "PG::UnableToSend",
]
NOT_ACCEPTABLE_ERRORS =
[
  "ActionController::BadRequest",
  "ActionController::UnknownFormat",
  "ActionController::UnknownHttpMethod",
  "ActionView::MissingTemplate",
  "Mime::Type::InvalidMimeType",
]

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorsMiddleware

Returns a new instance of ErrorsMiddleware.



16
17
18
# File 'lib/agilibox/errors_middleware.rb', line 16

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/agilibox/errors_middleware.rb', line 20

def call(env)
  @app.call(env)
rescue StandardError => e
  error = "#{e.class} : #{e.message}"

  if MAINTENANCE_ERRORS.any? { |pattern| error.match?(pattern) }
    return respond_with 503, "Maintenance en cours."
  end

  if NOT_ACCEPTABLE_ERRORS.any? { |pattern| error.match?(pattern) }
    return respond_with 406, "Not acceptable."
  end

  raise e
end