Module: Zotero::HTTPErrors
- Included in:
- Client
- Defined in:
- lib/zotero/http_errors.rb
Overview
HTTP error handling methods
Instance Method Summary collapse
- #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
#raise_client_error(response) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/zotero/http_errors.rb', line 15 def raise_client_error(response) code = response.code.to_i case code when 400, 413 then raise BadRequestError, "Bad request: #{response.body}" when 401, 403 then raise AuthenticationError, "Authentication failed - check your API key" when 404 then raise NotFoundError, "Resource not found" when 409 then raise ConflictError, "Conflict: #{response.body}" when 412 then raise PreconditionFailedError, "Precondition failed: #{response.body}" when 428 then raise PreconditionRequiredError, "Precondition required: #{response.body}" 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
27 28 29 30 31 32 33 34 35 |
# File 'lib/zotero/http_errors.rb', line 27 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, end |
#raise_server_or_unknown_error(response) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/zotero/http_errors.rb', line 37 def raise_server_or_unknown_error(response) code = response.code.to_i case code when 500..599 raise ServerError, "Server error: HTTP #{code} - #{response.message}" else raise Error, "Unexpected response: HTTP #{code} - #{response.message}" end end |