Skip to content

Commit 4fae301

Browse files
Merge pull request #1550 from jiayisheji/master
feat: Add connection error handle
2 parents cd646d5 + 2effb51 commit 4fae301

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/interfaces/mongoose-options.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ModuleMetadata, Type } from '@nestjs/common';
2-
import { ConnectOptions } from 'mongoose';
2+
import { ConnectOptions, MongooseError } from 'mongoose';
33

44
export interface MongooseModuleOptions
55
extends ConnectOptions,
@@ -9,6 +9,7 @@ export interface MongooseModuleOptions
99
retryDelay?: number;
1010
connectionName?: string;
1111
connectionFactory?: (connection: any, name: string) => any;
12+
connectionErrorFactory?: (error: MongooseError) => MongooseError;
1213
}
1314

1415
export interface MongooseOptionsFactory {

lib/mongoose-core.module.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { ModuleRef } from '@nestjs/core';
1111
import * as mongoose from 'mongoose';
1212
import { defer, lastValueFrom } from 'rxjs';
13+
import { catchError } from 'rxjs/operators';
1314
import { getConnectionToken, handleRetry } from './common/mongoose.utils';
1415
import {
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

Comments
 (0)