11import { locks , HybridNitroSQLite } from '../nitro'
2+ import NitroSQLiteError from '../NitroSQLiteError'
23import type {
34 QueryResult ,
45 Transaction ,
@@ -25,7 +26,7 @@ export const transaction = (
2526 fn : ( tx : Transaction ) => Promise < void > | void ,
2627) : Promise < void > => {
2728 if ( locks [ dbName ] == null )
28- throw Error ( `Nitro SQLite Error: No lock found on db: ${ dbName } `)
29+ throw new NitroSQLiteError ( ` No lock found on db: ${ dbName } `)
2930
3031 let isFinalized = false
3132
@@ -35,8 +36,8 @@ export const transaction = (
3536 params ?: SQLiteQueryParams ,
3637 ) : QueryResult < Data > => {
3738 if ( isFinalized ) {
38- throw Error (
39- `Nitro SQLite Error: Cannot execute query on finalized transaction: ${ dbName } ` ,
39+ throw new NitroSQLiteError (
40+ `Cannot execute query on finalized transaction: ${ dbName } ` ,
4041 )
4142 }
4243 return execute ( dbName , query , params )
@@ -47,17 +48,17 @@ export const transaction = (
4748 params ?: SQLiteQueryParams ,
4849 ) : Promise < QueryResult < Data > > => {
4950 if ( isFinalized ) {
50- throw Error (
51- `Nitro SQLite Error: Cannot execute query on finalized transaction: ${ dbName } ` ,
51+ throw new NitroSQLiteError (
52+ `Cannot execute query on finalized transaction: ${ dbName } ` ,
5253 )
5354 }
5455 return executeAsync ( dbName , query , params )
5556 }
5657
5758 const commit = ( ) => {
5859 if ( isFinalized ) {
59- throw Error (
60- `Nitro SQLite Error: Cannot execute commit on finalized transaction: ${ dbName } ` ,
60+ throw new NitroSQLiteError (
61+ `Cannot execute commit on finalized transaction: ${ dbName } ` ,
6162 )
6263 }
6364 const result = HybridNitroSQLite . execute ( dbName , 'COMMIT' )
@@ -67,8 +68,8 @@ export const transaction = (
6768
6869 const rollback = ( ) => {
6970 if ( isFinalized ) {
70- throw Error (
71- `Nitro SQLite Error: Cannot execute rollback on finalized transaction: ${ dbName } ` ,
71+ throw new NitroSQLiteError (
72+ `Cannot execute rollback on finalized transaction: ${ dbName } ` ,
7273 )
7374 }
7475 const result = HybridNitroSQLite . execute ( dbName , 'ROLLBACK' )
@@ -107,8 +108,13 @@ export const transaction = (
107108
108109 return new Promise ( ( resolve , reject ) => {
109110 const tx : PendingTransaction = {
110- start : ( ) => {
111- run ( ) . then ( resolve ) . catch ( reject )
111+ start : async ( ) => {
112+ try {
113+ const result = await run ( )
114+ resolve ( result )
115+ } catch ( error ) {
116+ reject ( NitroSQLiteError . fromError ( error ) )
117+ }
112118 } ,
113119 }
114120
@@ -118,7 +124,8 @@ export const transaction = (
118124}
119125
120126function startNextTransaction ( dbName : string ) {
121- if ( locks [ dbName ] == null ) throw Error ( `Lock not found for db: ${ dbName } ` )
127+ if ( locks [ dbName ] == null )
128+ throw new NitroSQLiteError ( `Lock not found for db: ${ dbName } ` )
122129
123130 if ( locks [ dbName ] . inProgress ) {
124131 // Transaction is already in process bail out
0 commit comments