Module: Smplkit::Errors
- Defined in:
- lib/smplkit/errors.rb
Class Method Summary collapse
- .parse_error_body(content) ⇒ Object
-
.raise_for_status(status_code, content) ⇒ Object
Parse a non-2xx response and raise the appropriate SDK exception.
Class Method Details
.parse_error_body(content) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/smplkit/errors.rb', line 98 def parse_error_body(content) body = JSON.parse(content) raw_errors = body.is_a?(Hash) ? body["errors"] : nil return [] unless raw_errors.is_a?(Array) raw_errors.filter_map do |item| next unless item.is_a?(Hash) source = item["source"] = item["meta"] ApiErrorDetail.new( status: item["status"], code: item["code"], title: item["title"], detail: item["detail"], source: source.is_a?(Hash) ? source : {}, meta: .is_a?(Hash) ? : {} ) end rescue JSON::ParserError, EncodingError [] end |
.raise_for_status(status_code, content) ⇒ Object
Parse a non-2xx response and raise the appropriate SDK exception. Raises nothing if status is 2xx.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/smplkit/errors.rb', line 123 def raise_for_status(status_code, content) return if (200..299).cover?(status_code) errors = parse_error_body(content) = errors.empty? ? "HTTP #{status_code}" : Error.(errors) exc_cls = case status_code when 402 then PaymentRequiredError when 404 then NotFoundError when 409 then ConflictError when 400, 422 then ValidationError else Error end raise exc_cls.new(, errors: errors, status_code: status_code) end |