Sha256: 87d7c07fabdb24c7c931574de4030e9aaae80f29c60d8f00ce626a1dacca652f

Contents?: true

Size: 1.27 KB

Versions: 19

Compression:

Stored size: 1.27 KB

Contents

class BMC::ErrorsMiddleware
  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",
  ]

  def initialize(app)
    @app = app
  end

  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

  private

  def respond_with(status, body)
    [status, {"Content-Type" => "text/plain; charset=UTF-8"}, [body]]
  end
end

stack = Rails.configuration.middleware
mw = BMC::ErrorsMiddleware
stack.unshift(mw)
stack.insert_after(ActionDispatch::DebugExceptions, mw) if defined?(ActionDispatch::DebugExceptions)
stack.insert_after(Bugsnag::Rack, mw) if defined?(Bugsnag::Rack)
stack.use(mw)

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
bmc-1.6.1 lib/bmc/errors_middleware.rb
bmc-1.6.0 lib/bmc/errors_middleware.rb
bmc-1.5.1 lib/bmc/errors_middleware.rb
bmc-1.5.0 lib/bmc/errors_middleware.rb
bmc-1.4.3 lib/bmc/errors_middleware.rb
bmc-1.4.2 lib/bmc/errors_middleware.rb
bmc-1.4.1 lib/bmc/errors_middleware.rb
bmc-1.4.0 lib/bmc/errors_middleware.rb
bmc-1.3.5 lib/bmc/errors_middleware.rb
bmc-1.3.4 lib/bmc/errors_middleware.rb
bmc-1.3.3 lib/bmc/errors_middleware.rb
bmc-1.3.2 lib/bmc/errors_middleware.rb
bmc-1.3.1 lib/bmc/errors_middleware.rb
bmc-1.3.0 lib/bmc/errors_middleware.rb
bmc-1.2.1 lib/bmc/errors_middleware.rb
bmc-1.2.0 lib/bmc/errors_middleware.rb
bmc-1.1.0 lib/bmc/errors_middleware.rb
bmc-1.0.1 lib/bmc/errors_middleware.rb
bmc-1.0.0 lib/bmc/errors_middleware.rb