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 3 commits
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) => MongooseError;
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
connectionError?: (error: MongooseError) => MongooseError;
connectionErrorFactory?: (error: MongooseError) => MongooseError;

}

export interface MongooseOptionsFactory {
Expand Down
24 changes: 22 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) => error);

const mongooseConnectionName = getConnectionToken(connectionName);

const mongooseConnectionNameProvider = {
Expand All @@ -59,7 +65,12 @@ export class MongooseCoreModule implements OnApplicationShutdown {
await mongoose.createConnection(uri, mongooseOptions).asPromise(),
mongooseConnectionName,
),
).pipe(handleRetry(retryAttempts, retryDelay)),
).pipe(
handleRetry(retryAttempts, retryDelay),
catchError((error) => {
throw mongooseConnectionError(error);
}),
),
),
};
return {
Expand Down Expand Up @@ -87,12 +98,16 @@ export class MongooseCoreModule implements OnApplicationShutdown {
retryDelay,
uri,
connectionFactory,
connectionError,
...mongooseOptions
} = mongooseModuleOptions;

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

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

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