Files
turso/serverless/javascript/src/error.ts
PThorpe92 9fe63ac8f0 SDK/drivers: make parameterized batches safe and consistent
Parameterized batches could produce different effects across embedded and
serverless drivers when a later bind value was invalid. Embedded Rust also
allowed cloned connections to interleave work with a batch, and managed
batches allowed transaction-control statements to escape their wrapper.
Rollback failures were hidden, while serverless Rust could retain a stale
last-insert rowid when COMMIT failed after a successful insert.

Validate every batch input before execution, reject transaction-control SQL
before opening a managed transaction, and hold an operation-wide gate for
embedded Rust batches. Preserve both the primary and rollback errors, and
carry successful statement rowids through serverless batch decoding even
when a synthetic transaction step fails. Reject malformed serverless batch
responses instead of treating incomplete transaction steps as success.
2026-08-28 10:40:26 -07:00

46 lines
1.8 KiB
TypeScript

export class DatabaseError extends Error {
/** Machine-readable error code (e.g., "SQLITE_CONSTRAINT") */
code?: string;
/** Raw numeric error code */
rawCode?: number;
/** Original error that caused this error */
declare cause?: Error;
/** For errors raised by `batch()`: the zero-based index of the failing
* statement, when a user statement (rather than the surrounding
* transaction control) failed. */
batchIndex?: number;
/** For errors raised by `batch()`: one entry per input statement, in
* order — the completed statement's `ResultSet`, or `null` for the
* failing statement and the statements that did not run. In a
* non-atomic batch the completed statements' effects are committed; in
* an atomic batch they were rolled back unless `rollbackError` is set.
* Empty when the batch failed client-side before anything was sent. */
batchResults?: Array<any | null>;
/** A rollback error that occurred while handling this error. */
rollbackError?: Error;
constructor(message: string, code?: string, rawCode?: number, cause?: Error) {
super(message);
this.name = 'DatabaseError';
this.code = code;
this.rawCode = rawCode;
this.cause = cause;
Object.setPrototypeOf(this, DatabaseError.prototype);
}
}
/**
* Error thrown when a query exceeds the configured timeout.
*
* This is a subclass of `DatabaseError` with `code` set to `"TIMEOUT"`.
* Catch this type to distinguish timeouts from other database errors
* and decide whether to retry or fail gracefully.
*/
export class TimeoutError extends DatabaseError {
constructor(message: string = 'Query timed out', cause?: Error) {
super(message, 'TIMEOUT', undefined, cause);
this.name = 'TimeoutError';
Object.setPrototypeOf(this, TimeoutError.prototype);
}
}