Module: Uploadcare::Concerns::ErrorHandler

Includes:
Exception
Included in:
Uploadcare::Client::RestClient, Uploadcare::Client::UploadClient
Defined in:
lib/uploadcare/concern/error_handler.rb

Overview

Wrapper for responses raises errors instead of returning monads

Instance Method Summary collapse

Instance Method Details

#failure(response) ⇒ Object

Extension of ApiStruct's failure method

Raises errors instead of returning falsey objects



14
15
16
17
18
19
20
# 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'] || parsed_response.map { |k, v| "#{k}: #{v}" }.join('; ')
rescue JSON::ParserError
  raise RequestError, response.body.to_s
end

#wrap(response) ⇒ Object

Extension of ApiStruct's wrap method

Catches throttling errors and Upload API errors



27
28
29
30
31
32
33
# 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