Skip to content

fix(cast): preserve reason in CastError message after setModel()#16167

Open
White-Devil2839 wants to merge 1 commit intoAutomattic:masterfrom
White-Devil2839:fix/cast-error-setmodel-reason
Open

fix(cast): preserve reason in CastError message after setModel()#16167
White-Devil2839 wants to merge 1 commit intoAutomattic:masterfrom
White-Devil2839:fix/cast-error-setmodel-reason

Conversation

@White-Devil2839
Copy link
Copy Markdown
Contributor

Fixes an issue where CastError#setModel() drops the original reason when recomputing the error message.

formatMessage() accepts a reason parameter, but setModel() was not passing it, causing the "because of ..." portion to be lost after calling setModel().

This change ensures the original error context is preserved.

@White-Devil2839
Copy link
Copy Markdown
Contributor Author

I think this.reasons is sometimes undefined,null or an object let me fix it

@White-Devil2839
Copy link
Copy Markdown
Contributor Author

Summary of changes

This PR refines CastError message construction to preserve backward compatibility while still allowing richer error context when appropriate.

What was changed

  1. Conditional inclusion of reason

    • reason is now only passed to formatMessage() when explicitly defined.
    • This prevents unintended changes in error messages where reason is null or unused.
  2. Preserve backward compatibility for error messages

    • Existing tests revealed that including certain internal errors (like BSONError) in the "because of" clause changes expected output.
    • To maintain consistency with current behavior, BSONError is explicitly excluded from being appended to the error message.
  3. No functional regression

    • All existing error formats remain unchanged unless a meaningful reason (non-internal) is present.
    • This ensures no breaking changes for code that relies on specific error message formats.

Why this matters

  • Avoids breaking existing tests and downstream code relying on exact error strings
  • Retains improved debuggability for relevant error types
  • Keeps behavior aligned with current Mongoose expectations

CI / Fixes done

  • Fixed lint issues (indentation + formatting)
  • Addressed failing tests caused by message mismatch (because of "BSONError")
  • Verified consistency across Node/Deno test environments

Let me know if you'd prefer a different approach @vkarpov15 (e.g., stricter filtering or alternative handling of reason).

Thanks for the review !

const msg = formatMessage(null, type, value, path, messageFormat, valueType, reason);

// Only include reason when defined
const msg = reason == null
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why are there 2 separate checks for reason == null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for replying rate @vkarpov15 Sir
And the two reason == null checks are redundant since

formatMessage
already handles null reason internally with if (reason != null && ...). Passing null/undefined as the 7th argument is harmless.

The reason I added the conditional was to avoid CI failures: with this.reason now being passed through, the

formatMessage
function was appending because of "BSONError" to messages where reason is a BSON driver error. This broke tests like model.querying.test.js:541 which assert on exact error message strings.

I'll simplify this — the clean fix is just:

Pass this.reason directly in

setModel()
(no conditional needed)
Add 'BSONError' to the exclusion list inside

formatMessage
(which I already did)
I'll squash and push a cleaner version. Thanks for the review!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also I am not resolving this conversation if you think this is a good fix please resolve it @vkarpov15 Sir.
Thank you very much once again

Fixes an issue where CastError#setModel() drops the original reason
when recomputing the error message. formatMessage() accepts a reason,
but setModel() was not passing it, causing the 'because of ...'
portion to be lost after calling setModel().

Also explicitly excludes BSONError from the 'because of ...' suffix
to maintain backward compatibility with existing tests that assert
exact message strings for internal driver cast errors.
@White-Devil2839 White-Devil2839 force-pushed the fix/cast-error-setmodel-reason branch from dfd1390 to 71e8168 Compare March 25, 2026 17:33
@White-Devil2839
Copy link
Copy Markdown
Contributor Author

@vkarpov15 I've removed the redundant conditional ternary checks from the CastError constructor and setModel methods just as the maintainer suggested.
I've also preserved your fix that excludes BSONError from being appended to the message, ensuring model.querying.test.js:541 and other CI tests won't fail.
Finally, I rebased and force-pushed these changes so the PR now has a single, clean commit instead of many messy ones.

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.

2 participants