From eb0f5206afd442f8fc5a17b86a8f4d407f08fb92 Mon Sep 17 00:00:00 2001 From: Josh Schwartzman Date: Sun, 10 Dec 2023 23:22:44 -0700 Subject: [PATCH] When polling, raise if an error is returned (#142) Previously, if an error from the API was returned while polling for updates on a file, it would continue polling. I updated it so that it raises a RequestError if the API returns an error status. If not, it raises a RetryError, which is swallowed by the retry gem's `with_retries` block. Example of an error that led to continued polling: ``` {"status":"error","error":"File validation error: Uploading of these file types is not allowed.","error_code":"DownloadFileValidationFailedError"} ``` --- lib/uploadcare.rb | 1 + lib/uploadcare/client/uploader_client.rb | 16 +++++++++++++--- lib/uploadcare/exception/request_error.rb | 3 +-- lib/uploadcare/exception/retry_error.rb | 8 ++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 lib/uploadcare/exception/retry_error.rb 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