Exception: Essential::APIError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/essential/errors/api_error.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_status: nil, body: nil, response: nil) ⇒ APIError

Returns a new instance of APIError.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/essential/errors/api_error.rb', line 26

def initialize(http_status: nil, body: nil, response: nil)
  @http_status = http_status
  @response = response

  if body
    begin
      @body = JSON.parse(body)
      if @body.key?('error')
        error    = @body['error']
        @type    = error['type']
        @message = error['message']
        @code    = error['code']
        @params  = error['params']
      end
    rescue JSON::ParserError
      @body = body
    end
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/essential/errors/api_error.rb', line 5

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/essential/errors/api_error.rb', line 6

def code
  @code
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



5
6
7
# File 'lib/essential/errors/api_error.rb', line 5

def http_status
  @http_status
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/essential/errors/api_error.rb', line 6

def message
  @message
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/essential/errors/api_error.rb', line 6

def params
  @params
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/essential/errors/api_error.rb', line 5

def response
  @response
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/essential/errors/api_error.rb', line 6

def type
  @type
end

Class Method Details

.from_exception(e) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/essential/errors/api_error.rb', line 8

def self.from_exception(e)
  case e
  # when SocketError
  # when NoMethodError
  when RestClient::ExceptionWithResponse
    new(
      http_status: e.http_code,
      body: e.http_body,
      response: e.response
    )
  when RestClient::Exception
    new(http_status: e.http_code)
  # when Errno::ECONNREFUSED
  else
    raise e
  end
end

Instance Method Details

#to_sObject



46
47
48
49
50
51
52
53
# File 'lib/essential/errors/api_error.rb', line 46

def to_s
  parts = []
  parts << format('<%s>', self.http_status)
  parts << self.type
  parts << self.message if self.message
  parts << self.params.to_json if self.params
  parts.join(' - ')
end