Merge 'serverless: Add DatabasError type' from Pekka Enberg

Add an explicit error type so application can actually do error
handling...

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #2256
This commit is contained in:
Pekka Enberg
2025-07-24 19:12:13 +03:00
6 changed files with 20 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import { Connection, connect, type Config as TursoConfig } from './connection.js';
import { DatabaseError } from './error.js';
/**
* Configuration options for creating a libSQL-compatible client.

View File

@@ -0,0 +1,7 @@
export class DatabaseError extends Error {
constructor(message: string) {
super(message);
this.name = 'DatabaseError';
Object.setPrototypeOf(this, DatabaseError.prototype);
}
}

View File

@@ -1,3 +1,4 @@
// Turso serverless driver entry point
export { Connection, connect, type Config } from './connection.js';
export { Statement } from './statement.js';
export { Statement } from './statement.js';
export { DatabaseError } from './error.js';

View File

@@ -1,3 +1,5 @@
import { DatabaseError } from './error';
export interface Value {
type: 'null' | 'integer' | 'float' | 'text' | 'blob';
value?: string | number;
@@ -165,12 +167,12 @@ export async function executeCursor(
} catch {
// If we can't parse the error body, use the default HTTP error message
}
throw new Error(errorMessage);
throw new DatabaseError(errorMessage);
}
const reader = response.body?.getReader();
if (!reader) {
throw new Error('No response body');
throw new DatabaseError('No response body');
}
const decoder = new TextDecoder();
@@ -238,7 +240,7 @@ export async function executePipeline(
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
throw new DatabaseError(`HTTP error! status: ${response.status}`);
}
return response.json();

View File

@@ -6,6 +6,7 @@ import {
type CursorResponse,
type CursorEntry
} from './protocol.js';
import { DatabaseError } from './error.js';
/**
* Configuration options for a session.
@@ -123,7 +124,7 @@ export class Session {
break;
case 'step_error':
case 'error':
throw new Error(entry.error?.message || 'SQL execution failed');
throw new DatabaseError(entry.error?.message || 'SQL execution failed');
}
}
@@ -204,7 +205,7 @@ export class Session {
break;
case 'step_error':
case 'error':
throw new Error(entry.error?.message || 'Batch execution failed');
throw new DatabaseError(entry.error?.message || 'Batch execution failed');
}
}

View File

@@ -3,6 +3,7 @@ import {
type CursorEntry
} from './protocol.js';
import { Session, type SessionConfig } from './session.js';
import { DatabaseError } from './error.js';
/**
* A prepared SQL statement that can be executed in multiple ways.
@@ -117,7 +118,7 @@ export class Statement {
break;
case 'step_error':
case 'error':
throw new Error(entry.error?.message || 'SQL execution failed');
throw new DatabaseError(entry.error?.message || 'SQL execution failed');
}
}
}