Skip to content

feat(errors): expose isOwnInstance to createCustomError's constructCb and skip redundant stack captures#587

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

feat(errors): expose isOwnInstance to createCustomError's constructCb and skip redundant stack captures#587
nev21 merged 1 commit into
mainfrom
nev21/CustomErrors

Conversation

@nev21

@nev21 nev21 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Custom errors built with createCustomError can extend other custom errors, chaining constructCb calls at every level. There was previously no way for a level to know whether it was the leaf class actually being instantiated or just running as a base-class step for some subclass further down the chain, so hierarchies with their own per-level stack capture logic (e.g. a TripwireError -> AssertionError -> AssertionFailure -> AssertionFatal chain) had no way to skip that work except at the leaf.

  • constructCb now receives a 3rd argument, isOwnInstance, computed by comparing the instance's resolved constructor against the class's own constructor function via a self-referencing named function expression The internal automatic Error.captureStackTrace call is now gated by the same isOwnInstance check, so it only fires once per instance (at the leaf-most class) instead of redundantly at every level of a chain
  • Add regression tests covering the internal capture optimization and isOwnInstance correctness across single classes, multi-level chains, and a class used both directly and as a base

@nev21 nev21 added this to the 0.16.0 milestone Jul 18, 2026
@nev21
nev21 requested a review from a team as a code owner July 18, 2026 04:50
Copilot AI review requested due to automatic review settings July 18, 2026 04:50
@nev21
nev21 requested a review from a team as a code owner July 18, 2026 04:50
@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 (4d61687) to head (d016feb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #587   +/-   ##
=======================================
  Coverage   99.17%   99.17%           
=======================================
  Files         179      179           
  Lines        5475     5479    +4     
  Branches     1209     1210    +1     
=======================================
+ Hits         5430     5434    +4     
  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 improves createCustomError to better support multi-level custom error inheritance by exposing whether a constructor is running for the leaf-most instance and by avoiding redundant internal Error.captureStackTrace calls across a base-class chain.

Changes:

  • Extend createCustomError’s constructCb callback to receive a 3rd isOwnInstance argument indicating leaf-most instantiation.
  • Gate the internal automatic Error.captureStackTrace call so it only runs once per instance (at the leaf-most class).
  • Add regression tests validating single-capture behavior and isOwnInstance semantics across inheritance chains.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
lib/src/helpers/customError.ts Adds isOwnInstance detection and gates internal stack capture to avoid repeated work in inheritance chains.
lib/test/src/common/helpers/throw.test.ts Adds regression tests for single stack capture and isOwnInstance behavior across direct and chained instantiation.

Comment thread lib/src/helpers/customError.ts
Comment thread lib/test/src/common/helpers/throw.test.ts
Comment thread lib/test/src/common/helpers/throw.test.ts
Comment thread lib/test/src/common/helpers/throw.test.ts
@nev21
nev21 force-pushed the nev21/CustomErrors branch from 1b856e1 to d016feb Compare July 18, 2026 05:01
… and skip redundant stack captures

Custom errors built with createCustomError can extend other custom errors, chaining constructCb calls at every level. There was previously no way for a level to know whether it was the leaf class actually being instantiated or just running as a base-class step for some subclass further down the chain, so hierarchies with their own per-level stack capture logic (e.g. a TripwireError -> AssertionError -> AssertionFailure -> AssertionFatal chain) had no way to skip that work except at the leaf.

- constructCb now receives a 3rd argument, isOwnInstance, computed by comparing the instance's resolved constructor against the class's own constructor function via a self-referencing named function expression The internal automatic Error.captureStackTrace call is now gated by the same isOwnInstance check, so it only fires once per instance (at the leaf-most class) instead of redundantly at every level of a chain
- Add regression tests covering the internal capture optimization and isOwnInstance correctness across single classes, multi-level chains, and a class used both directly and as a base
@nev21
nev21 enabled auto-merge (squash) July 18, 2026 05:18

@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 e92818d into main Jul 18, 2026
10 checks passed
@nev21
nev21 deleted the nev21/CustomErrors branch July 18, 2026 05:30
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