Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add connection error handle #1550

Merged
merged 5 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/interfaces/mongoose-options.interface.ts
@@ -1,5 +1,5 @@
import { ModuleMetadata, Type } from '@nestjs/common';
import { ConnectOptions } from 'mongoose';
import { ConnectOptions, MongooseError } from 'mongoose';

export interface MongooseModuleOptions
extends ConnectOptions,
Expand All @@ -9,6 +9,7 @@ export interface MongooseModuleOptions
retryDelay?: number;
connectionName?: string;
connectionFactory?: (connection: any, name: string) => any;
connectionError?: (error: MongooseError) => void;
}

export interface MongooseOptionsFactory {
Expand Down
26 changes: 24 additions & 2 deletions lib/mongoose-core.module.ts
Expand Up @@ -10,6 +10,7 @@ import {
import { ModuleRef } from '@nestjs/core';
import * as mongoose from 'mongoose';
import { defer, lastValueFrom } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { getConnectionToken, handleRetry } from './common/mongoose.utils';
import {
MongooseModuleAsyncOptions,
Expand Down Expand Up @@ -39,11 +40,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
retryDelay,
connectionName,
connectionFactory,
connectionError,
...mongooseOptions
} = options;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionError || ((error) => {});
jiayisheji marked this conversation as resolved.
Show resolved Hide resolved

const mongooseConnectionName = getConnectionToken(connectionName);

const mongooseConnectionNameProvider = {
Expand All @@ -59,7 +65,13 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await mongoose.createConnection(uri, mongooseOptions).asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
).pipe(
handleRetry(retryAttempts, retryDelay),
catchError((error) => {
mongooseConnectionError(error);
throw error;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mongooseConnectionError(error);
throw error;
throw mongooseConnectionError(error);

what about this approach instead (this in combination with https://github.com/nestjs/mongoose/pull/1550/files#r1003070783)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very nice suggestion

}),
),
),
};
return {
Expand Down Expand Up @@ -87,12 +99,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
retryDelay,
uri,
connectionFactory,
connectionError,
...mongooseOptions
} = mongooseModuleOptions;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionError || ((error) => {});

return await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
Expand All @@ -101,7 +117,13 @@ export class MongooseCoreModule implements OnApplicationShutdown {
.asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
).pipe(
handleRetry(retryAttempts, retryDelay),
catchError((error) => {
mongooseConnectionError(error);
throw error;
}),
),
);
},
inject: [MONGOOSE_MODULE_OPTIONS],
Expand Down