Sha256: 654240daae2231a3b91c8ac00e8332330d1f8dbe53a1ced585b8addc09283a15

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

class Agilibox::ErrorsMiddleware
  MAINTENANCE_ERRORS = [
    "ActiveRecord::ConnectionTimeoutError",
    "connections on port 5432",
    "PG::UnableToSend",
  ]

  NOT_ACCEPTABLE_ERRORS = [
    "ActionController::BadRequest",
    "ActionController::UnknownFormat",
    "ActionController::UnknownHttpMethod",
    "ActionDispatch::Cookies::CookieOverflow",
    "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 = Agilibox::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

3 entries across 3 versions & 1 rubygems

Version Path
agilibox-1.10.5 lib/agilibox/errors_middleware.rb
agilibox-1.10.2 lib/agilibox/errors_middleware.rb
agilibox-1.10.1 lib/agilibox/errors_middleware.rb