Exception: Peddler::Error

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil, response = nil) ⇒ Error

Returns a new instance of Error.



38
39
40
41
# File 'lib/peddler/error.rb', line 38

def initialize(msg = nil, response = nil)
  @response = response
  super(msg)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/peddler/error.rb', line 5

def response
  @response
end

Class Method Details

.build(response) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/peddler/error.rb', line 9

def build(response)
  error = JSON.parse(response).dig("errors").first
  class_name = normalize_class_name(error.dig("code"))
  message = error.dig("message")
  klass = if Errors.const_defined?(class_name)
    Errors.const_get(class_name)
  else
    Errors.const_set(
      class_name,
      Class.new(Error),
    )
  end

  klass.new(message, response)
rescue NameError
  # Do nothing if code cannot be converted to a class name
end