Exception: Yookassa::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/yookassa/errors.rb

Overview

Error returned by the YooKassa API with structured details.

Examples:

Handling an API error

begin
  Yookassa::Payment.find("nonexistent")
rescue Yookassa::ApiError => e
  puts e.code          # => "not_found"
  puts e.http_code     # => 404
  puts e.description   # => "Payment not found"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code: nil, description: nil, parameter: nil, response: {}) ⇒ ApiError

Returns a new instance of ApiError.

Parameters:

  • code (String, nil) (defaults to: nil)

    YooKassa error code

  • description (String, nil) (defaults to: nil)

    error description

  • parameter (String, nil) (defaults to: nil)

    invalid parameter name

  • response (Hash) (defaults to: {})

    raw HTTP response info



34
35
36
37
38
39
40
# File 'lib/yookassa/errors.rb', line 34

def initialize(code: nil, description: nil, parameter: nil, response: {})
  @code = code
  @description = description
  @parameter = parameter
  @response = response
  super(description || "API error (HTTP #{http_code})")
end

Instance Attribute Details

#codeString? (readonly)

Returns YooKassa error code (e.g. "invalid_request", "not_found").

Returns:

  • (String, nil)

    YooKassa error code (e.g. "invalid_request", "not_found")



19
20
21
# File 'lib/yookassa/errors.rb', line 19

def code
  @code
end

#descriptionString? (readonly)

Returns human-readable error description.

Returns:

  • (String, nil)

    human-readable error description



22
23
24
# File 'lib/yookassa/errors.rb', line 22

def description
  @description
end

#parameterString? (readonly)

Returns name of the invalid parameter, if applicable.

Returns:

  • (String, nil)

    name of the invalid parameter, if applicable



25
26
27
# File 'lib/yookassa/errors.rb', line 25

def parameter
  @parameter
end

#responseHash (readonly)

Returns raw response details (:http_code, :body, :headers).

Returns:

  • (Hash)

    raw response details (:http_code, :body, :headers)



28
29
30
# File 'lib/yookassa/errors.rb', line 28

def response
  @response
end

Instance Method Details

#http_codeInteger?

Returns HTTP status code.

Returns:

  • (Integer, nil)

    HTTP status code



43
44
45
# File 'lib/yookassa/errors.rb', line 43

def http_code
  @response[:http_code]
end

#response_bodyString?

Returns raw response body.

Returns:

  • (String, nil)

    raw response body



48
49
50
# File 'lib/yookassa/errors.rb', line 48

def response_body
  @response[:body]
end

#response_headersHash?

Returns response headers.

Returns:

  • (Hash, nil)

    response headers



53
54
55
# File 'lib/yookassa/errors.rb', line 53

def response_headers
  @response[:headers]
end