Exception: TwelvedataRuby::Error

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

Overview

Base error class for all TwelvedataRuby errors

Constant Summary collapse

DEFAULT_MESSAGES =

Default error messages for different error types

{
  "EndpointError" => "Endpoint is not valid: %{invalid}",
  "EndpointNameError" => "`%{invalid}` is not a valid endpoint. Valid endpoints: %{valid_names}",
  "EndpointParametersKeysError" => "Invalid parameters: %{invalid}. Valid parameters for `%{name}`: %{parameters}",
  "EndpointRequiredParametersError" => "Missing required parameters: %{invalid}.\
                                        Required for `%{name}`: %{required}",
  "ResponseError" => "Response error occurred",
  "ConfigurationError" => "Configuration error: %{message}",
  "NetworkError" => "Network error: %{message}",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, attributes: {}, original_error: nil) ⇒ Error

Initialize error with message and attributes

Parameters:

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

    Custom error message

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

    Error attributes for interpolation

  • original_error (Exception, nil) (defaults to: nil)

    Original exception that caused this error



25
26
27
28
29
30
31
# File 'lib/twelvedata_ruby/error.rb', line 25

def initialize(message: nil, attributes: {}, original_error: nil)
  @attributes = attributes
  @original_error = original_error

  error_message = message || format_default_message
  super(error_message)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



18
19
20
# File 'lib/twelvedata_ruby/error.rb', line 18

def attributes
  @attributes
end

#original_errorObject (readonly)

Returns the value of attribute original_error.



18
19
20
# File 'lib/twelvedata_ruby/error.rb', line 18

def original_error
  @original_error
end

Instance Method Details

#format_default_messageObject (private)



35
36
37
38
39
40
41
42
# File 'lib/twelvedata_ruby/error.rb', line 35

def format_default_message
  template = DEFAULT_MESSAGES[Utils.demodulize(self.class.name)]
  return "An error occurred" unless template

  format(template, **attributes)
rescue KeyError => e
  "Error message template missing key: #{e.key}"
end