Skip to content

chore(schema): use MongooseError instead of Error#16097

Merged
vkarpov15 merged 1 commit intoAutomattic:masterfrom
mahmoodhamdi:chore/issue-15995-standardize-errors-schema
Mar 14, 2026
Merged

chore(schema): use MongooseError instead of Error#16097
vkarpov15 merged 1 commit intoAutomattic:masterfrom
mahmoodhamdi:chore/issue-15995-standardize-errors-schema

Conversation

@mahmoodhamdi
Copy link
Contributor

Summary

Replaces 15 instances of throw new Error(...) with throw new MongooseError(...) in lib/schema.js as 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.js and #16096 for model.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 argument
  • indexTypes setter — cannot overwrite
  • virtual() — missing localField/foreignField, duplicate virtual (3 instances)

MongooseError extends Error, so this is fully backward-compatible.

Testing

All 747 schema-related tests pass.

Re: #15995

Replace 15 instances of `throw new Error(...)` with
`throw new MongooseError(...)` in lib/schema.js for consistency.

Re: Automattic#15995
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 Error throws with MongooseError across 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 vkarpov15 added this to the 9.3.1 milestone Mar 14, 2026
@vkarpov15 vkarpov15 merged commit 7044e58 into Automattic:master Mar 14, 2026
30 checks passed
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.

3 participants