1 / 1
Jan 28

I am working on express backend app with Mongoose as ODM. I just want to know if is there any built-in way to import the error codes/types so that we compare or use it something like below code snippet.

import { WriteConflict } from 'mongodb' if (errorCode === WriteConflict) { // do someting }

Instead of using magic number (error code)
``
if (errorCode === 112) {
// do someting
}

or creating a new file in our code base manually and use that across application like below (not preferred, if there is any way to import directly from the MongoDB library); eg: mongodb.error.ts file ```export const MONGODB_ERROR_CODES = { HostUnreachable: 6, HostNotFound: 7, AuthenticationFailed: 18, NetworkTimeout: 89, ShutdownInProgress: 91, PrimarySteppedDown: 189, ExceededTimeLimit: 262, SocketException: 9001, ... ... } as const satisfies Record<string, number>;

I have checked the MongoDB native nodejs library source code and found that error codes are not exported from the index.ts file see below links

https://github.com/mongodb/node-mongodb-native/blob/654069fc1750b6c3336f1002e3d3fb18bbc1451d/src/index.ts#L88

error.ts file

https://github.com/mongodb/node-mongodb-native/blob/654069fc1750b6c3336f1002e3d3fb18bbc1451d/src/error.ts#L38C14-L38C34

Other Related links:

error_codes.err file:

https://github.com/mongodb/mongo/blob/a21c7c7e0d0dcd7bd9b11d62f95ec6496bc617e8/src/mongo/base/error_codes.err

lib_db.js.html file

https://github.com/mongodb/node-mongodb-native/blob/654069fc1750b6c3336f1002e3d3fb18bbc1451d/docs/3.0/api/lib_db.js.html#L3006