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

Whether a connection error handling method can be added #1247

Closed
1 task done
jiayisheji opened this issue Feb 11, 2022 · 6 comments
Closed
1 task done

Whether a connection error handling method can be added #1247

jiayisheji opened this issue Feb 11, 2022 · 6 comments
Labels

Comments

@jiayisheji
Copy link
Contributor

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

[Nest] 6788  - 2022/02/11 pm5:53:31   ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
[Nest] 6788  - 2022/02/11 pm5:53:34   ERROR [MongooseModule] Unable to connect to the database. Retrying (2)...
[Nest] 6788  - 2022/02/11 pm5:53:37   ERROR [MongooseModule] Unable to connect to the database. Retrying (3)...
[Nest] 6788  - 2022/02/11 pm5:53:40   ERROR [MongooseModule] Unable to connect to the database. Retrying (4)...
[Nest] 6788  - 2022/02/11 pm5:53:43   ERROR [MongooseModule] Unable to connect to the database. Retrying (5)...
[Nest] 6788  - 2022/02/11 pm5:53:46   ERROR [MongooseModule] Unable to connect to the database. Retrying (6)...
[Nest] 6788  - 2022/02/11 pm5:53:49   ERROR [MongooseModule] Unable to connect to the database. Retrying (7)...
[Nest] 6788  - 2022/02/11 pm5:53:52   ERROR [MongooseModule] Unable to connect to the database. Retrying (8)...
[Nest] 6788  - 2022/02/11 pm5:53:55   ERROR [MongooseModule] Unable to connect to the database. Retrying (9)...
[Nest] 6788  - 2022/02/11 pm5:53:55   ERROR [ExceptionHandler] Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"

Connection Error Failed to catch Error information.
Connection Error causes the connectionFactory method to not work.

Error handling is facilitated if a method is provided that can catch connection errors.

Describe the solution you'd like

https://github.com/nestjs/mongoose/blob/master/lib/mongoose-core.module.ts#L80

const {catchError} = require("rxjs/operators");

@Global()
@Module({})
export class MongooseCoreModule implements OnApplicationShutdown {
....
    const connectionProvider = {
      provide: mongooseConnectionName,
      useFactory: async (
        mongooseModuleOptions: MongooseModuleOptions,
      ): Promise<any> => {
        const {
          retryAttempts,
          retryDelay,
          connectionName,
          uri,
          connectionFactory,
         connectionError,
          ...mongooseOptions
        } = mongooseModuleOptions;

        const mongooseConnectionFactory =
          connectionFactory || ((connection) => connection);
        
       const mongooseConnectionError =
          connectionError || ((error) => {});
      
        return await lastValueFrom(
          defer(async () =>
            mongooseConnectionFactory(
              await mongoose
                .createConnection(
                  mongooseModuleOptions.uri as string,
                  mongooseOptions,
                )
                .asPromise(),
              mongooseConnectionName,
            ),
          ).pipe(
            handleRetry(
              mongooseModuleOptions.retryAttempts,
              mongooseModuleOptions.retryDelay,
            ),
            catchError(err => {
                  mongooseConnectionError(err);
                  throw error;
            })
          ),
        );
      },
      inject: [MONGOOSE_MODULE_OPTIONS],
    };
    .....
}

Just throw the error through the RXJS catchError.

Teachability, documentation, adoption, migration strategy

The connectionError method is used to capture connectionError information and report exceptions.

MongooseModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: async (configService: ConfigService) => {
      return {
        uri: 'mongodb://localhost/test',
        connectionError: (error) => {
          console.log('MongooseModule connectionError', error);
          // Report connection error messages by email
        },
      };
    },
    inject: [ConfigService],
}),

What is the motivation / use case for changing the behavior?

Currently, if the connection fails, the error cannot be caught and a remedy policy implemented.

The connectionFactory method is not executed after a connection error.

@jiayisheji jiayisheji changed the title Whether a Connection error handling method can be added Whether a connection error handling method can be added Feb 11, 2022
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@jiayisheji
Copy link
Contributor Author

@kamilmysliwiec ok

@morelikeahumanraghu
Copy link

Trying to know why the connection with Mongodb was failing -- guessing this PR should give the stack trace.

@kblestarge
Copy link

I would like to gracefully fail by catching and logging the thrown error from a connection problem, but not breaking the app. Would this proposed solution work for my use-case?
What is the status of your PR, @jiayisheji?

@CodyTseng
Copy link

@jiayisheji If you're busy, I'd be happy to implement this PR.

@kamilmysliwiec
Copy link
Member

Let's track this here #1550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants