Exception: Cartowrap::APIError

Inherits:
CartowrapError show all
Defined in:
lib/cartowrap/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_status, response_body, error_info = nil) ⇒ APIError

Returns a new instance of APIError.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cartowrap/errors.rb', line 13

def initialize(http_status, response_body, error_info = nil)
  if response_body
    self.response_body = response_body.strip
  else
    self.response_body = ''
  end
  self.http_status = http_status

  if error_info && error_info.is_a?(String)
    message = error_info
  else
    unless error_info
      begin
        error_info = MultiJson.load(response_body)['error'] if response_body
      rescue
      end
      error_info ||= {}
    end

    self.cdb_error_type = error_info["type"]
    self.cdb_error_code = error_info["code"]
    self.cdb_error_subcode = error_info["error_subcode"]
    self.cdb_error_message = error_info["message"]
    self.cdb_error_user_msg = error_info["error_user_msg"]
    self.cdb_error_user_title = error_info["error_user_title"]

    error_array = []
    %w(type code error_subcode message error_user_title error_user_msg).each do |key|
      error_array << "#{key}: #{error_info[key]}" if error_info[key]
    end

    if error_array.empty?
      message = self.response_body
    else
      message = error_array.join(', ')
    end
  end
  message += " [HTTP #{http_status}]" if http_status

  super(message)
end

Instance Attribute Details

#cdb_error_codeObject

Returns the value of attribute cdb_error_code.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_code
  @cdb_error_code
end

#cdb_error_messageObject

Returns the value of attribute cdb_error_message.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_message
  @cdb_error_message
end

#cdb_error_subcodeObject

Returns the value of attribute cdb_error_subcode.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_subcode
  @cdb_error_subcode
end

#cdb_error_typeObject

Returns the value of attribute cdb_error_type.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_type
  @cdb_error_type
end

#cdb_error_user_msgObject

Returns the value of attribute cdb_error_user_msg.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_user_msg
  @cdb_error_user_msg
end

#cdb_error_user_titleObject

Returns the value of attribute cdb_error_user_title.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def cdb_error_user_title
  @cdb_error_user_title
end

#http_statusObject

Returns the value of attribute http_status.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def http_status
  @http_status
end

#response_bodyObject

Returns the value of attribute response_body.



9
10
11
# File 'lib/cartowrap/errors.rb', line 9

def response_body
  @response_body
end