Exception: EDH::Passport::APIError

Inherits:
EDHError
  • Object
show all
Defined in:
lib/edh/errors.rb

Direct Known Subclasses

BadPassportResponse, ClientError, ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new API Error

Parameters:

  • http_status (Integer)

    The HTTP status code of the response

  • response_body (String)

    The response body

  • error_info (defaults to: nil)

    One of the following:

    Hash

    The error information extracted from the request (“type”, “code”, “error_subcode”, “message”)

    String

    The error description

    If error_info is nil or not provided, the method will attempt to extract the error info from the response_body



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
54
55
56
57
58
59
60
61
# File 'lib/edh/errors.rb', line 23

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.pp_error_type = error_info["type"]
    self.pp_error_code = error_info["code"]
    self.pp_error_subcode = error_info["error_subcode"]
    self.pp_error_message = error_info["message"]

    error_array = []
    %w(type code error_subcode message).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

#http_statusObject

Returns the value of attribute http_status.



8
9
10
# File 'lib/edh/errors.rb', line 8

def http_status
  @http_status
end

#pp_error_codeObject

Returns the value of attribute pp_error_code.



8
9
10
# File 'lib/edh/errors.rb', line 8

def pp_error_code
  @pp_error_code
end

#pp_error_messageObject

Returns the value of attribute pp_error_message.



8
9
10
# File 'lib/edh/errors.rb', line 8

def pp_error_message
  @pp_error_message
end

#pp_error_subcodeObject

Returns the value of attribute pp_error_subcode.



8
9
10
# File 'lib/edh/errors.rb', line 8

def pp_error_subcode
  @pp_error_subcode
end

#pp_error_typeObject

Returns the value of attribute pp_error_type.



8
9
10
# File 'lib/edh/errors.rb', line 8

def pp_error_type
  @pp_error_type
end

#response_bodyObject

Returns the value of attribute response_body.



8
9
10
# File 'lib/edh/errors.rb', line 8

def response_body
  @response_body
end