-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Make sure MongooseError is exported in TypeScript #13387
Comments
Hey, I was skimming through the code to make sure it was exported, and I found out that it's actually exported as // main.ts
import mongoose from 'mongoose';
const UserSchema = new mongoose.Schema({ name: String });
const User = mongoose.model('user', UserSchema);
async function main() {
try {
await User.create({ name: 'John Doe' }); // MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
} catch (err) {
console.log(err instanceof mongoose.Error); // true
console.log((err as Error).name); // MongooseError
}
}
main(); I ended up finding the definition on those files: Line 51 in c1c3dfe
Line 1142 in c1c3dfe
Making the following export makes // lib/index.js#1143
Mongoose.prototype.MongooseError = require('./error/mongooseError'); I'm just wondering if |
Isn't it actually a Mongo error in your snippet? They're different. I meant Mongoose errors descrbed in docs here: https://mongoosejs.com/docs/api/error.html#Error() And its not a TS solution anyway %( |
I see. I changed my snippet using the example of the docs: import mongoose from 'mongoose';
const Model = mongoose.model('model', new mongoose.Schema({ answer: Number }));
async function main() {
const doc = new Model({ answer: 'not a number' });
const err = doc.validateSync();
console.log(err instanceof mongoose.Error); // true
console.log(err instanceof mongoose.MongooseError); // true
console.log(err instanceof mongoose.Error.ValidationError); // true
}
main(); I'm exporting |
It seems to me to be more useful to have an exported helper function in the same way they did it in Celebrate. I mean something like |
It's something that @vkarpov15 suggested on my PR. I originally had implemented a |
OK, I see. Thank you! |
Prerequisites
🚀 Feature Proposal
A tool to detect if error is MongooseError.
The most simple approach is to check
if (error instanceof MongooseError)
. But unfortunately this class is not exported, so one need to check if error is instance of all the exported descendants of it.The idea is to export a very simple member:
This will allow to find out if it's Mongoose's error and do not lead to leaking of private MongooseError class.
Motivation
Sometimes one need to transform thrown by Mongoose and caught in
.catch()
/catch
or passed to errors handler(s) by.catch(next)
(if using Express). It would be very nice to have a tool to detect if it's a Mongoose error, since several sources of error might exist in the same controller, like celebrate does (celebrate.isCelebrateError() method).At present, I use a helper that check against all 16 types of errors. It would be way more refined and elegant to have a built-in Mongoose method.
Example
The text was updated successfully, but these errors were encountered: