Exception: Lara::LaraApiError

Inherits:
LaraError
  • Object
show all
Defined in:
lib/lara/errors.rb

Overview

API error with HTTP status, type and message.

Instance Attribute Summary collapse

Attributes inherited from LaraError

#status_code

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, type, message) ⇒ LaraApiError

Returns a new instance of LaraApiError.

Parameters:

  • status_code (Integer)
  • type (String)
  • message (String)


38
39
40
41
42
# File 'lib/lara/errors.rb', line 38

def initialize(status_code, type, message)
  super("(HTTP #{status_code}) #{type}: #{message}", status_code)
  @type = type
  @message = message
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/lara/errors.rb', line 18

def type
  @type
end

Class Method Details

.from_response(response) ⇒ Object

Builds an error from an HTTP response with JSON body. Supports both { “error”: { “type”: “…”, “message”: “…” } } and { “type”: “…”, “message”: “…” } response formats.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lara/errors.rb', line 23

def self.from_response(response)
  data = begin
    JSON.parse(response.body)
  rescue StandardError
    {}
  end
  error = data["error"] || data
  error_type = error["type"] || "UnknownError"
  error_message = error["message"] || "An unknown error occurred"
  new(response.status, error_type, error_message)
end