Exception: ScalarRubyTest::Errors::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/amritk-scalar-test/errors.rb

Direct Known Subclasses

APIConnectionError, APIStatusError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status: nil, body: nil, headers: nil, request_id: nil, raw_body: nil) ⇒ APIError

Returns a new instance of APIError.



15
16
17
18
19
20
21
22
23
# File 'lib/amritk-scalar-test/errors.rb', line 15

def initialize(message, status: nil, body: nil, headers: nil, request_id: nil, raw_body: nil)
  super(message)
  @status = status
  @body = body
  @headers = headers
  @request_id = request_id
  @raw_body = raw_body
  @problem_details = self.class.parse_problem_details(body)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def headers
  @headers
end

#problem_detailsObject (readonly) Also known as: problem

Returns the value of attribute problem_details.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def problem_details
  @problem_details
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def raw_body
  @raw_body
end

#request_idObject (readonly)

Returns the value of attribute request_id.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def request_id
  @request_id
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/amritk-scalar-test/errors.rb', line 13

def status
  @status
end

Class Method Details

.error_message(status, body) ⇒ Object



43
44
45
46
# File 'lib/amritk-scalar-test/errors.rb', line 43

def self.error_message(status, body)
  detail = Runtime.extract_error_message(body)
  detail.nil? || detail.empty? ? "HTTP #{status}" : "#{status} #{detail}"
end

.from_response(response, body) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amritk-scalar-test/errors.rb', line 27

def self.from_response(response, body)
  klass = case response.code.to_i
          when 400 then BadRequestError
          when 401 then AuthenticationError
          when 403 then PermissionDeniedError
          when 404 then NotFoundError
          when 409 then ConflictError
          when 422 then UnprocessableEntityError
          when 429 then RateLimitError
          else response.code.to_i >= 500 ? InternalServerError : APIStatusError
          end
  headers = response.to_hash
  request_id = headers["x-request-id"]&.first || headers["request-id"]&.first
  klass.new(error_message(response.code.to_i, body), status: response.code.to_i, body: body, headers: headers, request_id: request_id, raw_body: response.body)
end

.parse_problem_details(body) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/amritk-scalar-test/errors.rb', line 48

def self.parse_problem_details(body)
  return nil unless body.is_a?(Hash)
  known = %w[type title status detail instance]
  return nil unless known.any? { |key| body.key?(key) }
  return nil if %w[type title detail instance].any? { |key| body.key?(key) && !body[key].is_a?(String) }
  return nil if body.key?("status") && !body["status"].is_a?(Integer)
  ProblemDetails.new(type: body["type"], title: body["title"], status: body["status"], detail: body["detail"], instance: body["instance"], extensions: body.reject { |key, _| known.include?(key) })
end