Class: BMC::ErrorsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/bmc/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",
  "ActionDispatch::Cookies::CookieOverflow",
  "ActionDispatch::Http::MimeNegotiation::InvalidType",
  "ActionView::MissingTemplate",
  "Mime::Type::InvalidMimeType",
]

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorsMiddleware

Returns a new instance of ErrorsMiddleware.



18
19
20
# File 'lib/bmc/errors_middleware.rb', line 18

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bmc/errors_middleware.rb', line 22

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