Skip to content

Commit

Permalink
When polling, raise if an error is returned (#142)
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 authored Dec 11, 2023
1 parent d7f226c commit eb0f520
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 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
16 changes: 13 additions & 3 deletions lib/uploadcare/client/uploader_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 eb0f520

Please sign in to comment.