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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/smplkit/errors.rb', line 80 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) ApiErrorDetail.new( status: item["status"], title: item["title"], detail: item["detail"], source: item["source"] || {} ) 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/smplkit/errors.rb', line 101 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 |