diff --git a/lib/uploadcare.rb b/lib/uploadcare.rb index e063613d..ad86a41c 100644 --- a/lib/uploadcare.rb +++ b/lib/uploadcare.rb @@ -6,6 +6,7 @@ # Exceptions require 'exception/throttle_error' require 'exception/request_error' +require 'exception/retry_error' # Entities require 'entity/entity' diff --git a/lib/uploadcare/client/uploader_client.rb b/lib/uploadcare/client/uploader_client.rb index 76f5e5d1..ecc0f25b 100644 --- a/lib/uploadcare/client/uploader_client.rb +++ b/lib/uploadcare/client/uploader_client.rb @@ -75,12 +75,22 @@ def post(args = {}) def poll_upload_response(token) with_retries(max_tries: Uploadcare.config.max_request_tries, base_sleep_seconds: Uploadcare.config.base_request_sleep, - max_sleep_seconds: Uploadcare.config.max_request_sleep) do + max_sleep_seconds: Uploadcare.config.max_request_sleep, + rescue: RetryError) do response = get_upload_from_url_status(token) - raise RequestError if %w[progress waiting unknown].include?(response.success[:status]) + handle_polling_response(response) + end + end - response + def handle_polling_response(response) + case response.success[:status] + when 'error' + raise RequestError, response.success[:error] + when 'progress', 'waiting', 'unknown' + raise RetryError, response.success[:error] end + + response end # Prepares body for upload_many method diff --git a/lib/uploadcare/exception/request_error.rb b/lib/uploadcare/exception/request_error.rb index a949e44a..ca23bfc3 100644 --- a/lib/uploadcare/exception/request_error.rb +++ b/lib/uploadcare/exception/request_error.rb @@ -3,7 +3,6 @@ module Uploadcare module Exception # Standard error for invalid API responses - class RequestError < StandardError - end + class RequestError < StandardError; end end end diff --git a/lib/uploadcare/exception/retry_error.rb b/lib/uploadcare/exception/retry_error.rb new file mode 100644 index 00000000..ac05439b --- /dev/null +++ b/lib/uploadcare/exception/retry_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Uploadcare + module Exception + # Standard error to raise when needing to retry a request + class RetryError < StandardError; end + end +end