feat(errors): bind constructCb's this to the new instance via fnCall#589
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #589 +/- ##
=======================================
Coverage 99.17% 99.17%
=======================================
Files 179 179
Lines 5479 5481 +2
Branches 1210 1214 +4
=======================================
+ Hits 5434 5436 +2
Misses 45 45
🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates createCustomError()’s construction path so that constructCb is invoked with this bound to the newly created error instance (in addition to receiving it as the first argument), and slightly streamlines stack capture by reusing the resolved leaf constructor.
Changes:
- Bind
constructCb’sthisto the error instance viafnCall(...)while preserving existing callback arguments. - Avoid a redundant constructor lookup when calling
Error.captureStackTraceby passing_ctordirectly. - Minor control-flow refactor around stack capture / callback invocation.
nevware21-bot
previously approved these changes
Jul 18, 2026
nevware21-bot
left a comment
Contributor
There was a problem hiding this comment.
Approved by nevware21-bot
constructCb is now invoked with fnCall(constructCb, _self, _self, theArgs, isOwnInstance) instead of a plain call, so `this` inside the callback refers to the new error instance in addition to receiving it as the first argument. Lets constructCb implementations use `this.prop = ...` instead of requiring the first (self) argument. Also pass the resolved leaf constructor (_ctor) directly to captureStackTrace instead of re-reading it off _this[CONSTRUCTOR], avoiding a redundant lookup since isOwnInstance already guarantees they're equal.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
lib/src/helpers/customError.ts:60
- The JSDoc currently implies the
constructCbthisbinding behavior is “(Since v0.16.0)”, but that version tag previously applied to theisOwnInstanceargument (andthisbinding is being introduced here). Also, referencing argument positions becomes confusing once athisparameter is added to the TypeScript type. Consider rewording to describe the actual runtime signature(self, args, isOwnInstance)and separately state thatthisis bound toself.
* new Custom Error instance is being created. (Since v0.16.0) The callback is invoked with `this` bound to
* the same instance that is also passed as the `self` (1st) argument, so implementations may use either
* `this.prop = ...` or `self.prop = ...` interchangeably - this only applies when `constructCb` is a normal
* (non-arrow) function, as arrow functions ignore the bound `this` and keep their own lexically captured value.
* The 3rd argument, `isOwnInstance`, is `true` when this class is the leaf-most (most-derived) type actually
nev21
enabled auto-merge (squash)
July 18, 2026 21:54
nevware21-bot
approved these changes
Jul 18, 2026
nevware21-bot
left a comment
Contributor
There was a problem hiding this comment.
Approved by nevware21-bot
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.
constructCb is now invoked with fnCall(constructCb, _self, _self, theArgs, isOwnInstance) instead of a plain call, so
thisinside the callback refers to the new error instance in addition to receiving it as the first argument. Lets constructCb implementations usethis.prop = ...instead of requiring the first (self) argument.Also pass the resolved leaf constructor (_ctor) directly to captureStackTrace instead of re-reading it off _this[CONSTRUCTOR], avoiding a redundant lookup since isOwnInstance already guarantees they're equal.