module Uploadcare::Concerns::ErrorHandler
Wrapper for responses raises errors instead of returning monads
Public Instance Methods
failure(response)
click to toggle source
Extension of ApiStruct's failure method
Raises errors instead of returning falsey objects @see github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L55
# File lib/uploadcare/concern/error_handler.rb, line 14 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
wrap(response)
click to toggle source
Extension of ApiStruct's wrap method
Catches throttling errors and Upload API errors
@see github.com/rubygarage/api_struct/blob/master/lib/api_struct/client.rb#L45
# File lib/uploadcare/concern/error_handler.rb, line 27 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 Instance Methods
catch_upload_errors(response)
click to toggle source
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
# File lib/uploadcare/concern/error_handler.rb, line 45 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
raise_throttling_error(response)
click to toggle source
Raise ThrottleError. Also, tells in error when server will be ready for next request
# File lib/uploadcare/concern/error_handler.rb, line 38 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