@@ -10,6 +10,7 @@ import {
10
10
import { ModuleRef } from '@nestjs/core' ;
11
11
import * as mongoose from 'mongoose' ;
12
12
import { defer , lastValueFrom } from 'rxjs' ;
13
+ import { catchError } from 'rxjs/operators' ;
13
14
import { getConnectionToken , handleRetry } from './common/mongoose.utils' ;
14
15
import {
15
16
MongooseModuleAsyncOptions ,
@@ -39,11 +40,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
39
40
retryDelay,
40
41
connectionName,
41
42
connectionFactory,
43
+ connectionErrorFactory,
42
44
...mongooseOptions
43
45
} = options ;
44
46
45
47
const mongooseConnectionFactory =
46
48
connectionFactory || ( ( connection ) => connection ) ;
49
+
50
+ const mongooseConnectionError =
51
+ connectionErrorFactory || ( ( error ) => error ) ;
52
+
47
53
const mongooseConnectionName = getConnectionToken ( connectionName ) ;
48
54
49
55
const mongooseConnectionNameProvider = {
@@ -59,7 +65,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
59
65
await mongoose . createConnection ( uri , mongooseOptions ) . asPromise ( ) ,
60
66
mongooseConnectionName ,
61
67
) ,
62
- ) . pipe ( handleRetry ( retryAttempts , retryDelay ) ) ,
68
+ ) . pipe (
69
+ handleRetry ( retryAttempts , retryDelay ) ,
70
+ catchError ( ( error ) => {
71
+ throw mongooseConnectionError ( error ) ;
72
+ } ) ,
73
+ ) ,
63
74
) ,
64
75
} ;
65
76
return {
@@ -87,12 +98,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
87
98
retryDelay,
88
99
uri,
89
100
connectionFactory,
101
+ connectionErrorFactory,
90
102
...mongooseOptions
91
103
} = mongooseModuleOptions ;
92
104
93
105
const mongooseConnectionFactory =
94
106
connectionFactory || ( ( connection ) => connection ) ;
95
107
108
+ const mongooseConnectionError =
109
+ connectionErrorFactory || ( ( error ) => error ) ;
110
+
96
111
return await lastValueFrom (
97
112
defer ( async ( ) =>
98
113
mongooseConnectionFactory (
@@ -101,7 +116,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
101
116
. asPromise ( ) ,
102
117
mongooseConnectionName ,
103
118
) ,
104
- ) . pipe ( handleRetry ( retryAttempts , retryDelay ) ) ,
119
+ ) . pipe (
120
+ handleRetry ( retryAttempts , retryDelay ) ,
121
+ catchError ( ( error ) => {
122
+ throw mongooseConnectionError ( error ) ;
123
+ } ) ,
124
+ ) ,
105
125
) ;
106
126
} ,
107
127
inject : [ MONGOOSE_MODULE_OPTIONS ] ,
0 commit comments