Module: Smplkit::Errors

Defined in:
lib/smplkit/errors.rb

Class Method Summary collapse

Class Method Details

.parse_error_body(content) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/smplkit/errors.rb', line 76

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.

Raises:

  • (exc_cls)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/smplkit/errors.rb', line 97

def raise_for_status(status_code, content)
  return if (200..299).cover?(status_code)

  errors = parse_error_body(content)
  message = errors.empty? ? "HTTP #{status_code}" : Error.derive_message(errors)

  exc_cls =
    case status_code
    when 404 then NotFoundError
    when 409 then ConflictError
    when 400, 422 then ValidationError
    else Error
    end

  raise exc_cls.new(message, errors: errors, status_code: status_code)
end