Module: Zotero::HTTPErrors
- Included in:
- Client
- Defined in:
- lib/zotero/http_errors.rb
Overview
HTTP error handling methods
Instance Method Summary collapse
- #error_detail(response) ⇒ Object
- #raise_client_error(response) ⇒ Object
- #raise_error_for_status(response) ⇒ Object
- #raise_rate_limit_error(response) ⇒ Object
- #raise_server_or_unknown_error(response) ⇒ Object
Instance Method Details
#error_detail(response) ⇒ Object
29 30 31 32 |
# File 'lib/zotero/http_errors.rb', line 29 def error_detail(response) body = response.body.to_s.strip body.empty? ? "(no details)" : body end |
#raise_client_error(response) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/zotero/http_errors.rb', line 15 def raise_client_error(response) code = response.code.to_i detail = error_detail(response) case code when 400, 413 then raise BadRequestError, "Bad request: #{detail}" when 401, 403 then raise AuthenticationError, "Authentication failed: #{detail}" when 404 then raise NotFoundError, "Resource not found: #{detail}" when 409 then raise ConflictError, "Conflict: #{detail}" when 412 then raise PreconditionFailedError, "Precondition failed: #{detail}" when 428 then raise PreconditionRequiredError, "Precondition required: #{detail}" end end |
#raise_error_for_status(response) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/zotero/http_errors.rb', line 6 def raise_error_for_status(response) code = response.code.to_i case code when 400..428 then raise_client_error(response) when 429 then raise_rate_limit_error(response) else raise_server_or_unknown_error(response) end end |
#raise_rate_limit_error(response) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/zotero/http_errors.rb', line 34 def raise_rate_limit_error(response) headers = response.to_hash.transform_values(&:first) backoff = headers["backoff"]&.to_i retry_after = headers["retry-after"]&.to_i = "Rate limited." += " Backoff: #{backoff}s" if backoff += " Retry after: #{retry_after}s" if retry_after raise RateLimitError.new(, retry_after: retry_after, backoff: backoff) end |
#raise_server_or_unknown_error(response) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/zotero/http_errors.rb', line 44 def raise_server_or_unknown_error(response) code = response.code.to_i case code when 500..599 raise ServerError, "Server error: HTTP #{code} - #{response.}" else raise Error, "Unexpected response: HTTP #{code} - #{response.}" end end |