Errors, timeouts, and retries
These examples use the latest stable release of the Python SDK.
Every API failure raises a FlashdriveError or one of its subclasses. Invalid arguments are caught before anything is sent and raise ValueError.
Inspect an error
from flashdrive import Flashdrive, FlashdriveError
with Flashdrive(drive_id="DRIVE_UUID") as client:
try:
client.files.edit_text(
"DOCUMENT_UUID",
[("draft", "final")],
revision="7",
idempotency_key="finalize-1",
)
except FlashdriveError as exc:
print(exc.status, exc.code, exc.request_id, exc.details)
print(exc.error_type, exc.retryable, exc.retry_key, exc.retry_after)
Every error has:
| Attribute | Meaning |
|---|---|
status | The HTTP status, or None for a failure inside the SDK. |
code | A stable code such as stale_version or drive_limit_exceeded. Decide on this, not on the message, whose wording can change. |
request_id | Quote it when you contact support. |
details | More about the failure, such as the field or the index of a failed edit. |
retry_after | Seconds to wait before retrying, when the service says. |
error_type | The kind of problem, an ErrorType. |
retryable | A Retryable: never, after_backoff, after_retry_after or after_change. |
retry_key | A RetryKey: same to retry with the same idempotency key, new for a new one. |
Three properties sum these up: is_retryable (sending the request again can ever succeed), retry_unchanged (the same request can simply be sent again after a wait) and retry_with_same_key. The advice comes from the service's answer, then from the SDK's own list of codes, then from the HTTP status, so even a code newer than your SDK carries advice. Values the SDK does not know stay as the strings the service sent. See the error reference for codes and messages.
Handle a kind of problem
Each kind of error has its own class, so you can handle a kind of problem without knowing every code:
| Class | error_type | Typical cause |
|---|---|---|
AuthenticationError | auth | A missing, wrong or revoked key (invalid_credentials). |
PermissionDeniedError | permission | The key or token may not do this. |
NotFoundError | not_found | The drive, folder or file does not exist, or is outside your access. |
InvalidRequestError | invalid | A value the service refused. |
ConflictError | conflict | A name taken, an edit that does not apply, or work still in progress. |
PreconditionFailedError | precondition | The file changed since you read it (stale_version). |
RateLimitError | rate_limit | Too many requests, or the drive's upload capacity is full. |
PaymentRequiredError | payment_required | The workspace's plan or balance needs attention. |
QuotaExceededError | quota | A drive or storage limit (drive_limit_exceeded, storage_capacity_exceeded). |
RequestTooLargeError | too_large | A file or request over its size limit. |
ServiceUnavailableError | unavailable | The service is busy or briefly unavailable. |
UpgradeRequiredError | upgrade | This SDK version or API version is not supported. |
import time
from flashdrive import ConflictError, PreconditionFailedError, RateLimitError
try:
client.files.write_bytes(document_id, data, revision=revision, offset=0)
except PreconditionFailedError:
... # the file changed: read it again, then decide
except ConflictError as exc:
... # look at exc.code
except RateLimitError as exc:
time.sleep(exc.retry_after or 1)
A throttled request raises RateLimitError with the message Too many requests. Try again after HH:MM UTC.
Other errors the SDK raises itself:
| Error | When |
|---|---|
SDKTimeoutError (code sdk_timeout) | A call ran out of its timeout. |
UploadError | An upload failed after the service accepted it; receipt holds what to resume. |
UploadTimeoutError (code upload_timeout) | An upload's local wait ran out; it is both an UploadError and an SDKTimeoutError. |
UploadBatchError (code upload_batch_failed) | Some files of an upload failed; see upload recovery. |
FileEditError | A files.write_bytes() call stopped. idempotency_key, drive_id, document_id, base_revision and receipt say how to resume: send the same data, options and revision with that key. A pending receipt does not prove the edit was saved. |
FileEditTimeoutError | A files.write_bytes() time limit ran out and the outcome is unknown; it is both a FileEditError and an SDKTimeoutError. Resume as above. |
BulkImportError | A bulk import stopped. import_id, drive_id and journal say how to resume, and import_status holds the last status, if known. |
SDKCompatibilityError | This SDK cannot work with the service's API. |
Keep the SDK up to date
Two errors ask you to change your installed client or your code:
| Error | HTTP status and code | What to do |
|---|---|---|
ClientUpgradeRequiredError | 426 client_upgrade_required | This SDK version is below the oldest one the service supports. Check the available matching release in SDK setup before upgrading. exc.minimum_version and exc.latest_version name the oldest supported and newest releases. It is an SDKCompatibilityError. |
APIVersionRetiredError | 410 api_version_retired | The endpoint was retired on exc.sunset_at. Switch to exc.replacement, which can be None, and read the guide at exc.docs_url. |
Before that happens, the SDK warns you once per notice and process: SDKUpgradeWarning when a newer release is available and SDKDeprecationWarning when something you use is deprecated, with the replacement, retirement date and upgrade command. Control them with Python's warning filters, or pass version_warnings=False to the client. The notices stay available in client.notices. The SDK never upgrades itself.
Choose a time budget
Every call takes timeout, in seconds, for the whole call including retries:
| Call | Default timeout |
|---|---|
| Most calls | 30 |
upload() | 600 for the transfers, plus processing_timeout 600 for processing |
documents.replace_file(), uploads.resume(), recursive folders.delete() | 600 |
files.edit_text() | 120 |
files.write_bytes() | 120 per request, processing_timeout 3600 overall |
files.download() | 120 for each range request |
files.read_bytes() | 60 |
folders.ensure() | 60 |
Use wait=False on upload() and replace_file() to return once the service has accepted the work. Closing network connections can take a moment longer than the time limit. After a local timeout, use the original key or upload receipt to recover the result: work the service accepted continues.
Automatic retries
The SDK retries a request only when repeating it is safe: reads, and changes that carry an idempotency key. It makes up to three attempts within the call's timeout for HTTP 429, 502, 503 and 504, for request_in_progress, and for dropped connections, waiting at least the Retry-After the service sends. If the wait a 429 asks for would outlast the deadline, the SDK raises the RateLimitError at once. After the last attempt, a dropped connection raises an error with code transport_error.
Calls that change files make their own idempotency key when you do not pass one, so the SDK's retries never apply a change twice. Pass your own idempotency_key when you want to retry the same change yourself, for example after your process restarts. A key is 1 to 128 visible ASCII characters.
Wait for upload capacity
When a drive's upload capacity is full, the service answers 429 upload_limit or 409 request_in_progress. For uploads, the SDK waits and retries with the same key until the transfer deadline, following Retry-After and adding a small random delay to spread out clients. These waits do not use up the three attempts. Files waiting for capacity do not stop the rest of a batch.
For storage_capacity_exceeded, free space or increase your workspace's storage before retrying; see storage and upgrades.
Decide whether to reuse the key
| Outcome | Next step |
|---|---|
| The connection dropped before a reply arrived | Retry the unchanged request with the same key to get the original result. |
The request is still running (request_in_progress) | Wait, then retry with the same key. |
| Rate limited | Wait for retry_after and retry with the same key. |
The file changed (stale_version) | Read it again, redo the change if it still applies, and use a new key. |
| The service refused the input | Correct it and use a new key for the changed request. |
The key was used with different details (idempotency_conflict) | Send the original details, or use a new key. |
exc.retry_key gives the same advice for any error: same or new.