Sha256: 647c455a6fa1b7f092b72339e75ef6a48946cfdfc50e62c63f46ac9530c9e643

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 KB

Contents

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

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

13 entries across 13 versions & 1 rubygems

Version Path
agilibox-1.10.0 lib/agilibox/errors_middleware.rb
agilibox-1.9.20 lib/agilibox/errors_middleware.rb
agilibox-1.9.19 lib/agilibox/errors_middleware.rb
agilibox-1.9.18 lib/agilibox/errors_middleware.rb
agilibox-1.9.17 lib/agilibox/errors_middleware.rb
agilibox-1.9.16 lib/agilibox/errors_middleware.rb
agilibox-1.9.15 lib/agilibox/errors_middleware.rb
agilibox-1.9.14 lib/agilibox/errors_middleware.rb
agilibox-1.9.13 lib/agilibox/errors_middleware.rb
agilibox-1.9.12 lib/agilibox/errors_middleware.rb
agilibox-1.9.11 lib/agilibox/errors_middleware.rb
agilibox-1.9.10 lib/agilibox/errors_middleware.rb
agilibox-1.9.9 lib/agilibox/errors_middleware.rb