Exception: ScalarRubyTest::Errors::APIError
- Defined in:
- lib/amritk-scalar-test/errors.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#problem_details ⇒ Object
(also: #problem)
readonly
Returns the value of attribute problem_details.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .error_message(status, body) ⇒ Object
- .from_response(response, body) ⇒ Object
- .parse_problem_details(body) ⇒ Object
Instance Method Summary collapse
-
#initialize(message, status: nil, body: nil, headers: nil, request_id: nil, raw_body: nil) ⇒ APIError
constructor
A new instance of APIError.
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(, status: nil, body: nil, headers: nil, request_id: nil, raw_body: nil) super() @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
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/amritk-scalar-test/errors.rb', line 13 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/amritk-scalar-test/errors.rb', line 13 def headers @headers end |
#problem_details ⇒ Object (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_body ⇒ Object (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_id ⇒ Object (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 |
#status ⇒ Object (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.(status, body) detail = Runtime.(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((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 |