Skip to content

Commit ee33215

Browse files
committed
feat: add better errors
1 parent ad478a3 commit ee33215

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

package/src/NitroSQLiteError.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const NITRO_SQLITE_ERROR_NAME = 'NitroSQLiteError'
2+
3+
/**
4+
* Custom error class for NitroSQLite operations
5+
* Extends the native Error class with proper prototype chain and error handling
6+
*/
7+
export default class NitroSQLiteError extends Error {
8+
constructor(message: string, options?: ErrorOptions) {
9+
super(message, options)
10+
this.name = NITRO_SQLITE_ERROR_NAME
11+
12+
// Maintains proper prototype chain for instanceof checks
13+
Object.setPrototypeOf(this, NitroSQLiteError.prototype)
14+
}
15+
16+
/**
17+
* Converts an unknown error to a NitroSQLiteError
18+
* Preserves stack traces and error causes when available
19+
*/
20+
static fromError(error: unknown): NitroSQLiteError {
21+
if (error instanceof NitroSQLiteError) {
22+
return error
23+
}
24+
25+
if (error instanceof Error) {
26+
const nitroSQLiteError = new NitroSQLiteError(error.message, {
27+
cause: error.cause,
28+
})
29+
// Preserve original stack trace if available
30+
if (error.stack) {
31+
nitroSQLiteError.stack = error.stack
32+
}
33+
return nitroSQLiteError
34+
}
35+
36+
if (typeof error === 'string') {
37+
return new NitroSQLiteError(error)
38+
}
39+
40+
return new NitroSQLiteError('Unknown error occurred', {
41+
cause: error,
42+
})
43+
}
44+
45+
/**
46+
* Converts a native error (from C++ bridge) to a NitroSQLiteError
47+
* Alias for fromError for semantic clarity
48+
*/
49+
static fromNativeError(error: unknown): NitroSQLiteError {
50+
return NitroSQLiteError.fromError(error)
51+
}
52+
}

package/src/operations/execute.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
SQLiteQueryParams,
99
QueryResultRow,
1010
} from '../types'
11+
import NitroSQLiteError from '../NitroSQLiteError'
1112

1213
export function execute<Row extends QueryResultRow = never>(
1314
dbName: string,
@@ -18,13 +19,17 @@ export function execute<Row extends QueryResultRow = never>(
1819
? toNativeQueryParams(params)
1920
: (params as NativeSQLiteQueryParams)
2021

21-
const nativeResult = HybridNitroSQLite.execute(
22-
dbName,
23-
query,
24-
transformedParams
25-
)
26-
const result = buildJsQueryResult<Row>(nativeResult)
27-
return result
22+
try {
23+
const nativeResult = HybridNitroSQLite.execute(
24+
dbName,
25+
query,
26+
transformedParams
27+
)
28+
29+
return buildJsQueryResult<Row>(nativeResult)
30+
} catch (error) {
31+
throw NitroSQLiteError.fromError(error)
32+
}
2833
}
2934

3035
export async function executeAsync<Row extends QueryResultRow = never>(
@@ -36,13 +41,16 @@ export async function executeAsync<Row extends QueryResultRow = never>(
3641
? toNativeQueryParams(params)
3742
: (params as NativeSQLiteQueryParams)
3843

39-
const nativeResult = await HybridNitroSQLite.executeAsync(
40-
dbName,
41-
query,
42-
transformedParams
43-
)
44-
const result = buildJsQueryResult<Row>(nativeResult)
45-
return result
44+
try {
45+
const nativeResult = await HybridNitroSQLite.executeAsync(
46+
dbName,
47+
query,
48+
transformedParams
49+
)
50+
return buildJsQueryResult<Row>(nativeResult)
51+
} catch (error) {
52+
throw NitroSQLiteError.fromError(error)
53+
}
4654
}
4755

4856
function toNativeQueryParams(

0 commit comments

Comments
 (0)