Why does mongodb's official npm package(mongodb) can not catch some errors?

Hello everyone, I am using the official mongodb driver package to develop some interfaces for operating the database, but I found that even if I use try and catch, I still cannot catch some errors. I use the windows operating system for development. If I want to connect to my database, I have to start the mongdb service first, but when I do not start the service and then try to use the official mongodb driver package to connect to the database, this npm package will not throw any error, this is just one of the cases where no error is thrown. Does this mongodb npm package provide any other way for users to catch errors?

This is the npm package I use: mongodb

This is my code:

db_config.js

const defaultUrl = 'mongodb://localhost:27017'
const defaultName = 'tumbleweed'

class DataBaseConfig {

    #dbUrl
    #dbName

    constructor() {
        this.#dbUrl = this.dbConnectUrl
        this.#dbName = this.dbConnectName
    }

    get dbConnectUrl() {
        return this.#dbUrl === undefined ? defaultUrl : this.#dbUrl
    }

    set dbConnectUrl(value) {
        this.#dbUrl = value === undefined ? defaultUrl : value
    }

    get dbConnectName() {
        return this.#dbName === undefined ? defaultName : this.#dbName
    }

    set dbConnectName(value) {
        this.#dbName = value === undefined ? defaultName : value
    }

}

const DataBaseShareConfig = new DataBaseConfig()
export default DataBaseShareConfig

db.js

import { MongoClient } from "mongodb";
import DataBaseShareConfig from "./db_config.js";

class DataBase {

    #db

    constructor() {
        this.#db = null
    }

    async #connect() {
        return new Promise(async (resolve, reject)=> {
            try {
                console.log(`begain to connecting: ${DataBaseShareConfig.dbConnectUrl}`)
                const client = await MongoClient.connect(DataBaseShareConfig.dbConnectUrl)
                this.#db = client.db(DataBaseShareConfig.dbConnectName)
                console.log(`db: ${DataBaseShareConfig.dbConnectName} connected succeed`)
                resolve(this.#db)
            } catch (error) {
                reject(error)
            }
        })
    }

    async find(collectionName, json) {
        console.log("begain to find...")
        return new Promise(async (resolve, reject)=> {
            try {
                if(!this.#db) {
                    await this.#connect()
                    const collection = this.#db.collection(collectionName)
                    const result = await collection.find(json).toArray()
                    resolve(result)
                } else {
                    const collection = this.#db.collection(collectionName)
                    const result = await collection.find(json).toArray()
                    resolve(result)
                }
            } catch (error) {
                reject(error)
            }
        })
    }

}

const DataBaseShareInstance = new DataBase()
export default DataBaseShareInstance

main.js

import DataBaseShareInstance from "./db/db.js"
import DataBaseShareConfig from "./db/db_config.js"

DataBaseShareConfig.dbConnectUrl = 'mongodb://localhost:27017'
DataBaseShareConfig.dbConnectName = 'tumbleweed'


const main = (function () {

    DataBaseShareInstance.find("users", {name: 'fq'}).then(result => {
        console.log(result)
    }).catch(error => {
        console.log(error)
    })

})()

Welcome to the MongoDB Community @Arrose_Chen !

Were you able to find an answer for this question?

If not, can you clarify which errors you expect to catch but that are not caught in your current code?

Per the MongoDB Server Discovery and Monitoring (SDAM) specification, clients do not perform any I/O in their constructor so perhaps you are trying catch some exceptions earlier than they will be thrown.

Regards,
Stennie