@@ -19,18 +19,43 @@ export class RemoteConnection implements LockContext {
1919 this . database = database ;
2020 }
2121
22- async executeBatch ( query : string , params : any [ ] [ ] = [ ] ) : Promise < QueryResult > {
23- const result = await this . database . executeBatch ( query , params ?? [ ] ) ;
24- return RemoteConnection . wrapQueryResult ( result ) ;
22+ /**
23+ * Runs the inner function, but appends the stack trace where this function was called. This is useful for workers
24+ * because stack traces from worker errors are otherwise unrelated to the application issue that has caused them.
25+ */
26+ private async recoverTrace < T > ( inner : ( ) => Promise < T > ) : Promise < T > {
27+ const trace = { } ;
28+ Error . captureStackTrace ( trace ) ;
29+
30+ try {
31+ return await inner ( ) ;
32+ } catch ( e ) {
33+ if ( e instanceof Error && e . stack ) {
34+ e . stack += ( trace as any ) . stack ;
35+ }
36+
37+ throw e ;
38+ }
39+ }
40+
41+ executeBatch ( query : string , params : any [ ] [ ] = [ ] ) : Promise < QueryResult > {
42+ return this . recoverTrace ( async ( ) => {
43+ const result = await this . database . executeBatch ( query , params ?? [ ] ) ;
44+ return RemoteConnection . wrapQueryResult ( result ) ;
45+ } ) ;
2546 }
2647
27- async execute ( query : string , params ?: any [ ] | undefined ) : Promise < QueryResult > {
28- const result = await this . database . execute ( query , params ?? [ ] ) ;
29- return RemoteConnection . wrapQueryResult ( result ) ;
48+ execute ( query : string , params ?: any [ ] | undefined ) : Promise < QueryResult > {
49+ return this . recoverTrace ( async ( ) => {
50+ const result = await this . database . execute ( query , params ?? [ ] ) ;
51+ return RemoteConnection . wrapQueryResult ( result ) ;
52+ } ) ;
3053 }
3154
32- async executeRaw ( query : string , params ?: any [ ] | undefined ) : Promise < any [ ] [ ] > {
33- return await this . database . executeRaw ( query , params ?? [ ] ) ;
55+ executeRaw ( query : string , params ?: any [ ] | undefined ) : Promise < any [ ] [ ] > {
56+ return this . recoverTrace ( async ( ) => {
57+ return await this . database . executeRaw ( query , params ?? [ ] ) ;
58+ } ) ;
3459 }
3560
3661 async getAll < T > ( sql : string , parameters ?: any [ ] ) : Promise < T [ ] > {
0 commit comments