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

Exporting MongooseServerSelectionError and other errors #11202

Closed
kasir-barati opened this issue Jan 9, 2022 · 1 comment
Closed

Exporting MongooseServerSelectionError and other errors #11202

kasir-barati opened this issue Jan 9, 2022 · 1 comment
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@kasir-barati
Copy link

kasir-barati commented Jan 9, 2022

Do you want to request a feature or report a bug? Yes. I think exposing MongooseServerSelectionError and other custom errors written in Mongoose package should be exported. Why? because then I can do error instanceof MongooseServerSelectionError and It would be much much simpler and secure. It is also helps me to write tests with jest. Test like this

// connect-to-mongodb.config.ts
import mongoose from 'mongoose';
import { serializeError } from 'serialize-error';

import { logger } from './winston.config';
import { Configs } from '../contracts/types/configs.type';

mongoose.Promise = global.Promise;
export let mongooseInstance: typeof mongoose;

export const connectToDb = async (config: Configs) => {
    try {
        if (config.nodeEnv === 'development') {
            mongoose.set('debug', true);
        }
        mongooseInstance = await mongoose.connect(config.db_uri, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: true,
            useCreateIndex: true,
            autoReconnect: true,
            connectTimeoutMS: 100_000,
            reconnectInterval: 10_000,
        });
        logger.info('[conn stat] connected to MongoDb', {
            meta: {
                connectionString: config.db_uri,
            },
        });
    } catch (err) {
        let message = 'During connecting to the mongodb an error occurred';
        if (err.name === 'MongooseServerSelectionError') {
            message = 'Could not connect to the specified connection string';
        }
        logger.error(message, {
            meta: serializeError(err),
        });

        throw err;
    }
};
// connect-to-mongodb.config.test.ts
import { MongooseServerSelectionError } from 'mongoose';

import {
    connectToDb,
    mongooseInstance,
} from '../../src/config/connect-to-mongodb.config';
import { Configs } from '../../src/contracts/types/configs.type';

test('Test connecting to database', function () {
    describe('connect to database failed', function () {
        // @ts-ignore
        const configs: Configs = {
            nodeEnv: 'development',
            db_uri: 'mongodb://user:[email protected]/db-name',
        };
        expect(connectToDb(configs)).toThrowError(MongooseServerSelectionError);
    });
});

What is the current behavior? No access to the errors (MongooseServerSelectionError)
screenshot from the current exported errors

What is the expected behavior? Exporting custom errors from the library. I mean I would like to import { MongooseServerSelectionError } from 'mongoose';

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJS: 17.3.0
MongooseJS: 5.6.2
MongoDB NodeJS driver: 3.5.1

@vkarpov15 vkarpov15 added this to the 6.2.0 milestone Jan 10, 2022
@IslandRhythms IslandRhythms added the new feature This change adds new functionality, like a new method or class label Jan 10, 2022
@vkarpov15
Copy link
Collaborator

In 6.2.0 you'll be able to do err instanceof mongoose.Error.MongooseServerSelectionError. Looks like there's already a typedef for this in index.d.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

3 participants