Sha256: 8e54c0ba8b6261c0154fb7e855cc72e65cb7dcf988d398371920050124aa555f
Contents?: true
Size: 1.85 KB
Versions: 2
Compression:
Stored size: 1.85 KB
Contents
# frozen_string_literal: true module Uploadcare module Concerns # Wrapper for responses # raises errors instead of returning monads module ErrorHandler include Exception # Extension of ApiStruct's failure method # # Raises errors instead of returning falsey objects # @see https://github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L55 def failure(response) catch_upload_errors(response) parsed_response = JSON.parse(response.body.to_s) raise RequestError, parsed_response['detail'] rescue JSON::ParserError raise RequestError, response.status end # Extension of ApiStruct's wrap method # # Catches throttling errors and Upload API errors # # @see https://github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L45 def wrap(response) raise_throttling_error(response) if response.status == 429 return failure(response) if response.status >= 300 catch_upload_errors(response) success(response) end private # Raise ThrottleError. Also, tells in error when server will be ready for next request def raise_throttling_error(response) retry_after = response.headers['Retry-After'].to_i + 1 || 11 raise ThrottleError.new(retry_after), "Response throttled, retry #{retry_after} seconds later" end # Upload API returns its errors with code 200, and stores its actual code and details within response message # This methods detects that and raises apropriate error def catch_upload_errors(response) return unless response.code == 200 parsed_response = JSON.parse(response.body.to_s) error = parsed_response['error'] if parsed_response.is_a?(Hash) raise RequestError, error if error end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
uploadcare-ruby-3.0.5 | lib/uploadcare/concern/error_handler.rb |
uploadcare-ruby-3.0.3 | lib/uploadcare/concern/error_handler.rb |