Error handling
A failed API request returns an HTTP error. A successfully queued upload can separately end with classification.status failed if processing fails. Handle both paths.
Request errors
HTTP errors return:
{
"code": "validation/invalid-request",
"message": "Too big: expected number to be <=100",
"requestId": "k3v9q2x7m1p4r8t6w5z0",
"details": {
"issues": [{ "path": "pageSize", "message": "Too big: expected number to be <=100", "code": "validation/too-big" }]
}
}| Field | Meaning |
|---|---|
code | A stable code to branch on, such as 404 asset/not-found. CalmLens can add codes to any endpoint, so handle an unknown code by its HTTP status. |
message | An explanation for developers. Its wording can change, so don't match on it. |
requestId | The request's ID, the same as the X-Request-Id header. |
details | Structured information that depends on the code, such as issues for a validation error or retryAfterSeconds. It is an empty object when there is none. |
Invalid input returns 400 validation/invalid-request, however many problems the request has. details.issues lists each one with its path, message and an issue code: validation/invalid-type, validation/too-small, validation/too-big, validation/invalid-format, validation/invalid-value or validation/unknown-field.
The SDKs return structured API errors. Handle errors by their code; log the message and details when debugging.
import { CalmLensError } from "calmlens";
try {
await client.assets.retrieve({ assetId });
} catch (error) {
if (error instanceof CalmLensError && error.code === "asset/not-found") {
return undefined;
}
throw error;
}
Every response, successful or not, carries an X-Request-Id header that identifies the request in CalmLens logs. Log it with failed requests and include it when you contact support.
Retry network failures and server errors with capped exponential backoff. Do not automatically retry validation errors. For multipart part errors, see Retrying parts.
Retrying requests safely
A request that timed out may have succeeded. Retrying a create or moderate request could then create a second asset that counts toward your usage. To prevent this, send an Idempotency-Key header on asset uploads (POST JSON uploads, POST raw file uploads and POST multipart starts) and on POST /, and send the same key with every retry of that request:
Idempotency-Key: 4f9a1c2e-8b7d-4e61-9a35-0c2b7f1d6e80- Use a new key for each request. A UUID works well. Keys can be up to 255 printable ASCII characters; anything else returns 400 idempotency/invalid-key.
- A retry returns the saved response. When the first request succeeded, a retry with the same key gets its response for 24 hours, with the header
Idempotent-Replayed: true, and nothing runs again. - The key belongs to one request. Reusing a key for a different request (another path, other query parameters or another body) returns 409 idempotency/key-reused. Raw uploads are compared by their query parameters,
Content-TypeandContent-Lengthrather than by their bytes. - Concurrent retries wait their turn. While the first request is still running, a retry returns 409 idempotency/request-in-progress with a
Retry-Afterheader. Wait that many seconds and retry with the same key to receive the result. - Failures are not saved. If the first request failed, the key is freed and a retry runs the request again, so you can fix the problem and retry with the same key.
- Keys are scoped to your API key. A key sent with one API key never matches a request sent with another.
A unique externalId also protects against duplicates, but unlike an idempotency key it doesn't return the first response, so retrieve the existing asset instead.
Rate limits
CalmLens does not publish a general requests-per-second limit. If a request is throttled, it returns 429 with a Retry-After header giving the number of seconds to wait before retrying. Uploads have their own limits:
| Error | Returned when |
|---|---|
| 403 usage/free-uploads-exhausted | The organisation has used all its free uploads without setting up billing. |
| 402 usage/uploads-paused | The organisation has reached one of its usage limits. details.reason says which. |
| 429 upload/website-rate-limit-reached | Websites are submitted faster than the project's per-minute limit. Retry-After says when to retry. |
| 403 remote-url/not-allowed | The URL is on a domain the project doesn't accept. |
File-size and multipart constraints return their documented upload/* errors.
Choose a recovery action
| Failure | Recovery |
|---|---|
| Missing or invalid credentials, or insufficient access | Fix credentials and permissions before retrying. |
| Validation or unsupported input | Correct the request or file. |
| Network timeout or server failure | Retry with capped backoff and the same Idempotency-Key; without one, check whether a write succeeded before repeating it. |
Throttled (429) or request still in progress | Wait for the Retry-After delay, then retry. |
| Idempotency key reused for a different request | Send the new request with a new key. |
| Free uploads used up | Set up billing for the project's organisation before uploading again. |
| Multipart conflict | Check the expected part number or restart the upload. |
Processing failures
A queued request can succeed while classification later fails. Watch for classification.status failed through retrieval or the asset.failed event; do not publish that content. Inspect its classification.failureReason (see Failure reasons), correct the input when needed, then retry an eligible asset. Follow Recover from processing failure for retry eligibility, the API operation and when to submit a new copy.