Exception: KB::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/kb/errors/error.rb

Direct Known Subclasses

ClientError, ResourceNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code = nil, body = nil, error = nil) ⇒ Error

Returns a new instance of Error.



5
6
7
8
9
10
11
12
# File 'lib/kb/errors/error.rb', line 5

def initialize(status_code = nil, body = nil, error = nil)
  super(error)
  @status_code = status_code
  @body = body
  @message = "Received Status: #{status_code}\n#{body}"
  @message = error.message if error.present? && body.nil? && status_code.nil?
  set_backtrace error.backtrace if error
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/kb/errors/error.rb', line 3

def body
  @body
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/kb/errors/error.rb', line 3

def message
  @message
end

#status_codeObject

Returns the value of attribute status_code.



3
4
5
# File 'lib/kb/errors/error.rb', line 3

def status_code
  @status_code
end

Class Method Details

.from_faraday(error) ⇒ Object



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

def self.from_faraday(error)
  case error.response&.[](:status)
  when 404
    ResourceNotFound
  when 409
    ConflictError
  when 422
    UnprocessableEntityError
  else
    self
  end.new(error.response&.[](:status), error.response&.[](:body), error)
end