Class: HighLevel::Middleware::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/high_level/middleware/error_handler.rb

Overview

Maps non-2xx responses to typed Error subclasses, raising one with the response’s status, body, and request id.

Constant Summary collapse

STATUS_MAP =

HTTP status code to exception class. Unmapped 5xx codes raise ServerError; any other unmapped non-2xx raises the base Error.

{
  400 => HighLevel::BadRequestError,
  401 => HighLevel::UnauthorizedError,
  403 => HighLevel::ForbiddenError,
  404 => HighLevel::NotFoundError,
  422 => HighLevel::UnprocessableEntityError,
  429 => HighLevel::RateLimitError
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

Faraday response callback. Raises the mapped error for any non-2xx status; a no-op otherwise.

Raises:

  • (klass)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/high_level/middleware/error_handler.rb', line 22

def on_complete(env)
  return if env.status.between?(200, 299)

  klass = STATUS_MAP[env.status] ||
          (env.status >= 500 ? HighLevel::ServerError : HighLevel::Error)

  raise klass.new(
    "HTTP #{env.status} on #{env.method.to_s.upcase} #{env.url&.path}",
    status: env.status,
    response_body: env.body,
    request_id: request_id_from(env)
  )
end