Skip to content

Commit

Permalink
When polling, raise if an error is returned
Browse files Browse the repository at this point in the history
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"}
```
  • Loading branch information
geeosh committed Nov 28, 2023
1 parent d7f226c commit 1f479ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/uploadcare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Exceptions
require 'exception/throttle_error'
require 'exception/request_error'
require 'exception/retry_error'

# Entities
require 'entity/entity'
Expand Down
10 changes: 8 additions & 2 deletions lib/uploadcare/client/uploader_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/uploadcare/exception/request_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Uploadcare
module Exception
# Standard error for invalid API responses
class RequestError < StandardError
end
class RequestError < StandardError; end
end
end
8 changes: 8 additions & 0 deletions lib/uploadcare/exception/retry_error.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1f479ac

Please sign in to comment.