@@ -10,6 +10,7 @@ import {
1010import { ModuleRef } from '@nestjs/core' ;
1111import * as mongoose from 'mongoose' ;
1212import { defer , lastValueFrom } from 'rxjs' ;
13+ import { catchError } from 'rxjs/operators' ;
1314import { getConnectionToken , handleRetry } from './common/mongoose.utils' ;
1415import {
1516 MongooseModuleAsyncOptions ,
@@ -39,11 +40,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
3940 retryDelay,
4041 connectionName,
4142 connectionFactory,
43+ connectionErrorFactory,
4244 ...mongooseOptions
4345 } = options ;
4446
4547 const mongooseConnectionFactory =
4648 connectionFactory || ( ( connection ) => connection ) ;
49+
50+ const mongooseConnectionError =
51+ connectionErrorFactory || ( ( error ) => error ) ;
52+
4753 const mongooseConnectionName = getConnectionToken ( connectionName ) ;
4854
4955 const mongooseConnectionNameProvider = {
@@ -59,7 +65,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
5965 await mongoose . createConnection ( uri , mongooseOptions ) . asPromise ( ) ,
6066 mongooseConnectionName ,
6167 ) ,
62- ) . pipe ( handleRetry ( retryAttempts , retryDelay ) ) ,
68+ ) . pipe (
69+ handleRetry ( retryAttempts , retryDelay ) ,
70+ catchError ( ( error ) => {
71+ throw mongooseConnectionError ( error ) ;
72+ } ) ,
73+ ) ,
6374 ) ,
6475 } ;
6576 return {
@@ -87,12 +98,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
8798 retryDelay,
8899 uri,
89100 connectionFactory,
101+ connectionErrorFactory,
90102 ...mongooseOptions
91103 } = mongooseModuleOptions ;
92104
93105 const mongooseConnectionFactory =
94106 connectionFactory || ( ( connection ) => connection ) ;
95107
108+ const mongooseConnectionError =
109+ connectionErrorFactory || ( ( error ) => error ) ;
110+
96111 return await lastValueFrom (
97112 defer ( async ( ) =>
98113 mongooseConnectionFactory (
@@ -101,7 +116,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
101116 . asPromise ( ) ,
102117 mongooseConnectionName ,
103118 ) ,
104- ) . pipe ( handleRetry ( retryAttempts , retryDelay ) ) ,
119+ ) . pipe (
120+ handleRetry ( retryAttempts , retryDelay ) ,
121+ catchError ( ( error ) => {
122+ throw mongooseConnectionError ( error ) ;
123+ } ) ,
124+ ) ,
105125 ) ;
106126 } ,
107127 inject : [ MONGOOSE_MODULE_OPTIONS ] ,
0 commit comments