1
1
import { Injectable , OnModuleDestroy } from '@nestjs/common' ;
2
2
import { AsyncLocalStorage } from 'async_hooks' ;
3
- import { Executor } from 'edgedb' ;
3
+ import { EdgeDBError , Executor } from 'edgedb' ;
4
+ import { getPreviousList } from '~/common' ;
4
5
import { Client } from './reexports' ;
5
6
6
7
@Injectable ( )
@@ -13,9 +14,31 @@ export class TransactionContext
13
14
}
14
15
15
16
async inTx < R > ( fn : ( ) => Promise < R > ) : Promise < R > {
16
- return await this . client . transaction ( async ( tx ) => {
17
- return await this . run ( tx , fn ) ;
18
- } ) ;
17
+ const errorMap = new WeakMap < Error , Error > ( ) ;
18
+
19
+ try {
20
+ return await this . client . transaction ( async ( tx ) => {
21
+ try {
22
+ return await this . run ( tx , fn ) ;
23
+ } catch ( error ) {
24
+ // If the error "wraps" an EdgeDB error, then
25
+ // throw that here and save the original.
26
+ // This allows EdgeDB to check if the error is retry-able.
27
+ // If it is, then this error doesn't matter; otherwise we'll unwrap below.
28
+ const maybeRetryableError = getPreviousList ( error , true ) . find (
29
+ ( e ) => e instanceof EdgeDBError ,
30
+ ) ;
31
+ if ( maybeRetryableError ) {
32
+ errorMap . set ( maybeRetryableError , error ) ;
33
+ throw maybeRetryableError ;
34
+ }
35
+ throw error ;
36
+ }
37
+ } ) ;
38
+ } catch ( error ) {
39
+ // Unwrap the original error if it was wrapped above.
40
+ throw errorMap . get ( error ) ?? error ;
41
+ }
19
42
}
20
43
21
44
get current ( ) {
0 commit comments