From 1f479ace0c33cf3b7aa8d34f4700465803630222 Mon Sep 17 00:00:00 2001 From: Josh Schwartzman Date: Tue, 28 Nov 2023 06:10:02 -0700 Subject: [PATCH] When polling, raise if an error is returned 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 | 10 ++++++++-- lib/uploadcare/exception/request_error.rb | 3 +-- lib/uploadcare/exception/retry_error.rb | 8 ++++++++ 4 files changed, 18 insertions(+), 4 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..dbff2014 100644 --- a/lib/uploadcare/client/uploader_client.rb +++ b/lib/uploadcare/client/uploader_client.rb @@ -75,9 +75,15 @@ 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]) + + if response.success[:status] == 'error' + raise RequestError, response.success[:error] + elsif %w[progress waiting unknown].include?(response.success[:status]) + raise RetryError, response.success[:error] + end response end 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