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

feat(ts): Export MongooseError class #13403

Merged
merged 1 commit into from May 18, 2023
Merged

Conversation

ramos-ph
Copy link
Contributor

@ramos-ph ramos-ph commented May 15, 2023

Summary

Exports the class MongooseError so it can be tested using err instanceof MongooseError

Examples

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();

Closes #13387

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for this change looks fine, but I don't think this is the right approach for fixing #13387. Like OP said, the ideal approach would be just to check if err instanceof MongooseError. So I think the better approach would be to make sure MongooseError is exported for use in TypeScript.

@ramos-ph ramos-ph changed the title feat(utils): Add isMongooseError feat(ts): Export MongooseError class May 16, 2023
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 I'll merge this into 7.2 branch.

@vkarpov15 vkarpov15 changed the base branch from master to 7.2 May 18, 2023 01:28
@vkarpov15 vkarpov15 added this to the 7.2.0 milestone May 18, 2023
@vkarpov15 vkarpov15 merged commit 88ba969 into Automattic:7.2 May 18, 2023
21 checks passed
@ramos-ph ramos-ph deleted the feat/13387 branch May 18, 2023 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make sure MongooseError is exported in TypeScript
2 participants