Exception: TwelvedataRuby::ResponseError

Inherits:
Error
  • Object
show all
Defined in:
lib/twelvedata_ruby/error.rb

Overview

Base class for API response errors

Constant Summary collapse

API_ERROR_CODES =

Mapping of API error codes to specific error classes

{
  400 => "BadRequestResponseError",
  401 => "UnauthorizedResponseError",
  403 => "ForbiddenResponseError",
  404 => "NotFoundResponseError",
  414 => "ParameterTooLongResponseError",
  429 => "TooManyRequestsResponseError",
  500 => "InternalServerResponseError",
}.freeze
HTTP_ERROR_CODES =

Mapping of HTTP error codes to specific error classes

{
  404 => "PageNotFoundResponseError",
}.freeze

Constants inherited from Error

Error::DEFAULT_MESSAGES

Instance Attribute Summary collapse

Attributes inherited from Error

#attributes, #original_error

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Error

#format_default_message

Constructor Details

#initialize(json_data: {}, request: nil, status_code: nil, message: nil, **options) ⇒ ResponseError

Initialize response error

Parameters:

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

    JSON response data

  • request (Request) (defaults to: nil)

    Original request object

  • status_code (Integer) (defaults to: nil)

    HTTP/API status code

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

    Custom error message



117
118
119
120
121
122
123
124
# File 'lib/twelvedata_ruby/error.rb', line 117

def initialize(json_data: {}, request: nil, status_code: nil, message: nil, **options)
  @json_data = json_data.is_a?(Hash) ? json_data : {}
  @status_code = status_code || @json_data[:code]
  @request = request

  error_message = message || @json_data[:message] || "Response error occurred"
  super(message: error_message, **options)
end

Instance Attribute Details

#json_dataObject (readonly)

Returns the value of attribute json_data.



109
110
111
# File 'lib/twelvedata_ruby/error.rb', line 109

def json_data
  @json_data
end

#requestObject (readonly)

Returns the value of attribute request.



109
110
111
# File 'lib/twelvedata_ruby/error.rb', line 109

def request
  @request
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



109
110
111
# File 'lib/twelvedata_ruby/error.rb', line 109

def status_code
  @status_code
end

Class Method Details

.error_class_for_code(code, error_type = :api) ⇒ String?

Find appropriate error class for given code and type

Parameters:

  • code (Integer)

    Error code

  • error_type (Symbol) (defaults to: :api)

    Type of error (:api or :http)

Returns:

  • (String, nil)

    Error class name



99
100
101
102
103
104
105
106
# File 'lib/twelvedata_ruby/error.rb', line 99

def error_class_for_code(code, error_type = :api)
  case error_type
  when :api
    API_ERROR_CODES[code]
  when :http
    HTTP_ERROR_CODES[code]
  end
end