mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-06 01:34:21 +01:00
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:
@@ -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.
|
||||
|
||||
7
packages/turso-serverless/src/error.ts
Normal file
7
packages/turso-serverless/src/error.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export class DatabaseError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'DatabaseError';
|
||||
Object.setPrototypeOf(this, DatabaseError.prototype);
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
@@ -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();
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user