Exception: Webceo::Api::Error

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

Overview

Class Error provides customized error message based on the response sent by Webceo API server

Author:

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Error.



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
# File 'lib/webceo/api/error.rb', line 13

def initialize(http_status, response_body, error_info = nil)
  self.response_body = (response_body) ? response_body.strip : ''
  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).first if response_body
      rescue
      end
      error_info ||= {}
    end

    error_info = error_info.with_indifferent_access

    self.code = error_info['result']
    self.message = error_info['errormsg']
    self.api_method = error_info['method']
    self.request_id = error_info['id']

    error_array = []
    %i(code message api_method request_id).each do |key|
      error_array << "#{key}: #{self.send(key)}" if self.send(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

#api_methodObject

Returns the value of attribute api_method.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def api_method
  @api_method
end

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def code
  @code
end

#http_statusObject

Returns the value of attribute http_status.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def http_status
  @http_status
end

#messageObject

Returns the value of attribute message.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def message
  @message
end

#request_idObject

Returns the value of attribute request_id.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def request_id
  @request_id
end

#response_bodyObject

Returns the value of attribute response_body.



11
12
13
# File 'lib/webceo/api/error.rb', line 11

def response_body
  @response_body
end