Errors and retries in the TypeScript SDK
The error classes, what the SDK retries, and how to send a write again without running it twice.
Errors
Every error the SDK throws is a ByteRoverError, except an abort you asked for, which throws your signal's reason. An error your own code throws, such as a callback (onUploadCreated, onUploadCompleted, ignore) or a range source's read, comes back as it is. An error response from the API is an APIStatusError, with:
| Field | Meaning |
|---|---|
status | The HTTP status. |
code | The API's error code, such as NOT_FOUND. Always a string. See Errors and retries. |
message | A sentence that says what went wrong. |
trace_id | The request's trace ID. Quote it when you contact support. |
body, headers, response | The parsed body, the headers and the response. |
from_gateway | On a 5xx, true when a proxy in front of ByteRover answered, not ByteRover. Whether the request ran is unknown. |
Each status has its own class:
| Class | Status |
|---|---|
BadRequestError | 400 |
AuthenticationError | 401 |
InsufficientBalanceError | 402 |
PermissionDeniedError | 403 |
NotFoundError | 404 |
ConflictError | 409. code says which conflict, such as VERSION_MISMATCH or UPLOAD_CLOSED. |
RateLimitError | 429 |
InternalServerError | 500 and any other 5xx |
ServiceUnavailableError | 503 and 504 |
Any other error status is a plain APIStatusError.
When no response came at all, the error is an APIConnectionError, or an APITimeoutError when the timeout ran out. Its sent is "no" when the connection failed before the request went out, so nothing ran, and "unknown" when the request may have reached the server.
import { APIConnectionError, ByteRover, ConflictError, NotFoundError } from "@byterover/sdk";
const client = new ByteRover();
try {
await client.documents.update("doc_7n4p1s8v2x5z9b3d6f0h", { tags: ["policy"], expected_version: 4 });
} catch (error) {
if (error instanceof ConflictError && error.code === "VERSION_MISMATCH") console.log("Changed since you read it.");
else if (error instanceof NotFoundError) console.log("No such document, trace", error.trace_id);
else if (error instanceof APIConnectionError) console.log("No response; sent:", error.sent);
else throw error;
}instanceof works even when your program loads both the ESM and the CommonJS build of the SDK. A redirect is refused, not followed, so the key is only ever sent to baseUrl. A 2xx response whose body isn't JSON, such as a sign-in page of a captive portal, is an APIStatusError with the code INVALID_RESPONSE, never a result. So is an empty 2xx body from an operation that answers one; folders.create, folders.delete and request() may answer nothing.
Four errors come from the SDK's own work: OperationFailedError and WaitTimeoutError from operations.wait, UploadWaitError when an upload completed but waiting for it failed, and UploadIncompleteError, with the upload_id, when an upload was opened but didn't finish. See Upload files.
What the SDK retries
A call is retried, up to maxRetries times (2 by default), only when that is safe:
- any call, when the connection failed before the request went out, and on a 429 from ByteRover;
- a read, also after a timeout, a lost connection, a 5xx or a 429 from a proxy in front of ByteRover;
- never a write or a search whose outcome is unknown: a timeout, a lost connection or a 5xx. The write may have run, and the search may have been charged.
Between attempts the SDK waits a random time that grows with each attempt, up to 8 seconds, or the time the API asks for with Retry-After. When the API asks for more than a minute, you get the error at once. Set maxRetries: 0, on the client or on one call, to retry nothing.
Send a write again
Every write sends an Idempotency-Key: a new one for each call, kept across that call's retries. A search sends one only when you pass idempotencyKey, since the API keeps a keyed reply, passages included, and a search that may have run is never sent again. The API keeps the reply to a key only if the write succeeded. A copy sent while the first is still running waits on the server for its reply, for up to 150 seconds. The SDK's default timeouts end sooner (60 seconds for a write, 105 or 135 seconds for a search), so such a copy usually ends with an APITimeoutError whose sent is "unknown"; send it again with the same idempotencyKey. You get a ConflictError (CONFLICT) only when the call's timeout is longer than that wait.
So after an unknown outcome, send the write again with the same idempotencyKey you passed: if the first try succeeded you get its reply, if it is still running the copy waits for it, and the write runs again only if it failed. If you didn't pass a key, the SDK's key is gone, so check the state first, with documents.retrieve, documents.list or operations.retrieve, and only then send the write again, with a key of your own:
const driveId = "drv_4h7k2m9q8r1t5v6w3x0y";
const text = "# Standup\n\nShipped the refund page.";
await client.documents.putText(driveId, { path: "/notes/2026-11-15.md", text }, { idempotencyKey: "notes-2026-11-15" });A key the SDK made is lost if your process stops. Pass your own when a write must survive a crash. The API keeps keys for 24 hours. See Idempotency.