Class: Yookassa::Middleware::ErrorHandler

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

Overview

Faraday middleware that maps HTTP error responses to typed exceptions.

Inspects the response status and raises the appropriate ApiError subclass. The error body is parsed to extract YooKassa error code, description, and parameter name when available.

See Also:

Constant Summary collapse

ERROR_MAP =

Maps HTTP status codes to error classes.

{
  400 => Yookassa::BadRequestError,
  401 => Yookassa::UnauthorizedError,
  403 => Yookassa::ForbiddenError,
  404 => Yookassa::NotFoundError,
  429 => Yookassa::TooManyRequestsError,
  500 => Yookassa::InternalServerError
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

Raises:

  • (error_class)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yookassa/middleware/error_handler.rb', line 25

def on_complete(env)
  return if env.success?

  status = env.status
  body = env.body
  error_class = ERROR_MAP[status] || Yookassa::ApiError
  error_data = parse_error_body(body)

  raise error_class.new(
    code: error_data[:code],
    description: error_data[:description],
    parameter: error_data[:parameter],
    response: {
      http_code: status,
      body: body,
      headers: env.response_headers
    }
  )
end