chore(schema): use MongooseError instead of Error#16097
Merged
vkarpov15 merged 1 commit intoAutomattic:masterfrom Mar 14, 2026
Merged
Conversation
Replace 15 instances of `throw new Error(...)` with `throw new MongooseError(...)` in lib/schema.js for consistency. Re: Automattic#15995
Contributor
There was a problem hiding this comment.
Pull request overview
This PR is part of the ongoing effort (#15995) to standardize Mongoose’s internal thrown errors by replacing throw new Error(...) with throw new MongooseError(...) in lib/schema.js, improving consistency of error types across the codebase.
Changes:
- Replaced multiple
Errorthrows withMongooseErroracross schema aliasing, encryption/FLE, index removal, path definition, plugin registration, and virtual definitions. - Kept existing error messages largely intact while changing the error class used.
vkarpov15
approved these changes
Mar 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces 15 instances of
throw new Error(...)withthrow new MongooseError(...)inlib/schema.jsas part of the effort to standardize error types across the codebase.This is the third PR in the series for #15995 (after #16095 for
aggregate.jsand #16096 formodel.js).Changed locations:
_applyAliases()— invalid alias option (2 instances)addVirtualProperties()— invalid encryptionType, encryption mismatch, missing encryptionType, invalid BSON type (4 instances)removeIndex()— wrong argument count and type (2 instances)path()— reserved property name, schema path errors (2 instances)plugin()— invalid plugin argumentindexTypessetter — cannot overwritevirtual()— missing localField/foreignField, duplicate virtual (3 instances)MongooseErrorextendsError, so this is fully backward-compatible.Testing
All 747 schema-related tests pass.
Re: #15995