fix(cast): preserve reason in CastError message after setModel()#16167
fix(cast): preserve reason in CastError message after setModel()#16167White-Devil2839 wants to merge 1 commit intoAutomattic:masterfrom
Conversation
|
I think this.reasons is sometimes undefined,null or an object let me fix it |
Summary of changesThis PR refines What was changed
Why this matters
CI / Fixes done
Let me know if you'd prefer a different approach @vkarpov15 (e.g., stricter filtering or alternative handling of Thanks for the review ! |
lib/error/cast.js
Outdated
| const msg = formatMessage(null, type, value, path, messageFormat, valueType, reason); | ||
|
|
||
| // Only include reason when defined | ||
| const msg = reason == null |
There was a problem hiding this comment.
Why are there 2 separate checks for reason == null?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
dfd1390 to
71e8168
Compare
|
@vkarpov15 I've removed the redundant conditional ternary checks from the CastError constructor and setModel methods just as the maintainer suggested. |
Fixes an issue where
CastError#setModel()drops the originalreasonwhen recomputing the error message.formatMessage()accepts areasonparameter, butsetModel()was not passing it, causing the"because of ..."portion to be lost after callingsetModel().This change ensures the original error context is preserved.