@@ -19,18 +19,43 @@ export class RemoteConnection implements LockContext {
19
19
this . database = database ;
20
20
}
21
21
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
+ } ) ;
25
46
}
26
47
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
+ } ) ;
30
53
}
31
54
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
+ } ) ;
34
59
}
35
60
36
61
async getAll < T > ( sql : string , parameters ?: any [ ] ) : Promise < T [ ] > {
0 commit comments