Exception: Starling::Errors::ApiError

Inherits:
BaseError
  • Object
show all
Defined in:
lib/starling/errors/api_error.rb

Overview

An error raised when the Starling Bank API responds in a way indicating an error

Instance Method Summary collapse

Methods inherited from BaseError

#initialize

Constructor Details

This class inherits a constructor from Starling::Errors::BaseError

Instance Method Details

#errorString

Returns the error name returned by the Starling Bank API.

Returns:

  • (String)

    the error name returned by the Starling Bank API



20
21
22
23
# File 'lib/starling/errors/api_error.rb', line 20

def error
  return unless json?
  parsed_body['error']
end

#error_descriptionString

Returns the error description returned by the Starling Bank API.

Returns:

  • (String)

    the error description returned by the Starling Bank API



26
27
28
29
# File 'lib/starling/errors/api_error.rb', line 26

def error_description
  return unless json?
  parsed_body['error_description']
end

#messageString Also known as: to_s

Returns a helpful message explaining the error, incorporating the HTTP status code and the error message (either parsed from the JSON for a JSON response, or the whole body).

Returns:

  • (String)

    a helpful message explaining the error, incorporating the HTTP status code and the error message (either parsed from the JSON for a JSON response, or the whole body)



10
11
12
13
14
15
16
# File 'lib/starling/errors/api_error.rb', line 10

def message
  # If there response isn't JSON or either the Starling-provided error or error
  # description is missing, return a simpler error from BaseError
  return super unless error && error_description

  "#{status}: #{error_description} (#{error})"
end

#parsed_bodyHash?

Returns:

  • (Hash)

    the parsed JSON response, if there is a valid JSON body

  • (nil)

    if there is no body, or the body is not valid JSON



33
34
35
36
37
38
# File 'lib/starling/errors/api_error.rb', line 33

def parsed_body
  return unless body
  JSON.parse(body)
rescue JSON::ParserError
  nil
end