Skip to content

feat(errors): bind constructCb's this to the new instance via fnCall#589

Merged
nev21 merged 1 commit into
mainfrom
nev21/Release
Jul 18, 2026
Merged

feat(errors): bind constructCb's this to the new instance via fnCall#589
nev21 merged 1 commit into
mainfrom
nev21/Release

Conversation

@nev21

@nev21 nev21 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

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.

@nev21 nev21 added this to the 0.16.0 milestone Jul 18, 2026
Copilot AI review requested due to automatic review settings July 18, 2026 20:05
@nev21
nev21 requested review from a team as code owners July 18, 2026 20:05
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.17%. Comparing base (efca6c5) to head (5a60db2).

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           
Files with missing lines Coverage Δ
lib/src/helpers/customError.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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’s this to the error instance via fnCall(...) while preserving existing callback arguments.
  • Avoid a redundant constructor lookup when calling Error.captureStackTrace by passing _ctor directly.
  • Minor control-flow refactor around stack capture / callback invocation.

Comment thread lib/src/helpers/customError.ts
Comment thread lib/src/helpers/customError.ts
nevware21-bot
nevware21-bot previously approved these changes Jul 18, 2026

@nevware21-bot nevware21-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 constructCb this binding behavior is “(Since v0.16.0)”, but that version tag previously applied to the isOwnInstance argument (and this binding is being introduced here). Also, referencing argument positions becomes confusing once a this parameter is added to the TypeScript type. Consider rewording to describe the actual runtime signature (self, args, isOwnInstance) and separately state that this is bound to self.
 * 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

Comment thread lib/src/helpers/customError.ts
@nev21
nev21 enabled auto-merge (squash) July 18, 2026 21:54

@nevware21-bot nevware21-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved by nevware21-bot

@nev21
nev21 merged commit a066abc into main Jul 18, 2026
11 checks passed
@nev21
nev21 deleted the nev21/Release branch July 18, 2026 21:55
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