Exception: Increase::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/increase/errors.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, response: nil, detail: nil, status: nil, title: nil, type: nil) ⇒ ApiError

Returns a new instance of ApiError.



14
15
16
17
18
19
20
21
22
23
# File 'lib/increase/errors.rb', line 14

def initialize(message, response: nil, detail: nil, status: nil, title: nil, type: nil)
  @response = response

  @detail = detail || response.body["detail"]
  @status = status || response.body["status"]
  @title = title || response.body["title"]
  @type = type || response.body["type"]

  super(message)
end

Instance Attribute Details

#detailObject (readonly)

Returns the value of attribute detail.



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

def detail
  @detail
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/increase/errors.rb', line 7

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/increase/errors.rb', line 10

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/increase/errors.rb', line 11

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/increase/errors.rb', line 12

def type
  @type
end

Class Method Details

.from_response(response) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/increase/errors.rb', line 26

def from_response(response)
  type = response.body["type"]
  klass = ERROR_TYPES[type]

  # Fallback in case of really bad 5xx error
  klass ||= InternalServerError if (500..599).cover?(response.status)

  # Handle case of an unknown error
  klass ||= ApiError

  code = [response.body["status"] || response.status, type].compact.join(": ") || "Error"
  message = [response.body["title"], response.body["detail"]].compact.join(" ") || "Increase API Error"

  klass.new("[#{code}] #{message}", response: response)
end