Skip to content

Detect thrown custom Error subclasses in error-code lint rule - #51343

Merged
pelikhan merged 5 commits into
mainfrom
copilot/fix-eslint-require-error-code
Aug 8, 2026
Merged

Detect thrown custom Error subclasses in error-code lint rule#51343
pelikhan merged 5 commits into
mainfrom
copilot/fix-eslint-require-error-code

Conversation

Copilot AI commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

require-error-code-in-thrown-error only checked throw new Error(...), so local custom Error subclasses could throw uncoded messages unnoticed in files already importing error_codes.cjs.

  • Rule behavior
    • Resolves local class X extends Error declarations.
    • Follows local subclass chains transitively, e.g. class B extends A.
    • Applies the existing first-argument error-code check to throw new CustomError(...).
class A extends Error {}
class B extends A {}

throw new B("failed to fetch"); // now reported
throw new B(`${ERR_API}: failed to fetch`); // allowed
  • Coverage

    • Adds rule cases for direct custom Error subclasses.
    • Adds nested subclass-chain cases.
    • Covers subclasses declared after the throw site.
  • Existing violations

    • Updates push_signed_commits.cjs custom PushSignedCommits* throw sites to include standardized ERR_* prefixes rather than allowlisting them.

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 7.27 AIC · ⌖ 6.48 AIC · ⊞ 6.1K ·
Comment /souschef to run again

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix eslint-factory rule to handle custom Error subclasses Detect thrown custom Error subclasses in error-code lint rule Aug 8, 2026
Copilot AI requested a review from pelikhan August 8, 2026 07:09
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Hey @pelikhan 👋 — thanks for working on closing the ESLint rule gap for custom Error subclasses! This PR looks solid:

Rule enhancement — extends require-error-code-in-thrown-error to detect and validate custom Error subclasses and their transitive chains.

Test coverage — includes test cases for direct subclasses, nested chains, and subclasses declared after throw sites.

Violations resolved — updates push_signed_commits.cjs to use standardized error codes in the PushSignedCommits* throw sites (lines 303, 311, 320, 609, 662, 675, 679, 695, 699, 710, 714).

Clear description — PR body documents the rule behavior, coverage, and reasoning.

The diff is focused, well-scoped to the issue (#51311), and ready for review. 🚀

Generated by ✅ Contribution Check · auto · 76.5 AIC · ⌖ 3.49 AIC · ⊞ 8.7K ·

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

PR Triage

Extends error-code lint rule to custom Error subclasses. Isolated eslint-factory change, no CI results yet.

Generated by 🔧 PR Triage Agent · auto · 47.1 AIC · ⌖ 2.55 AIC · ⊞ 8K ·

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

Extends error-code linting to detect local Error subclasses and updates existing violations.

Changes:

  • Detects direct and transitive custom Error subclasses.
  • Adds subclass rule coverage.
  • Prefixes signed-commit errors with standardized codes.
Show a summary per file
File Description
eslint-factory/src/rules/require-error-code-in-thrown-error.ts Adds custom subclass detection.
eslint-factory/src/rules/require-error-code-in-thrown-error.test.ts Tests direct, transitive, and later declarations.
actions/setup/js/push_signed_commits.cjs Adds standardized error-code prefixes.

Review details

Tip

Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Suppressed comments (1)

actions/setup/js/push_signed_commits.cjs:844

  • All PushSignedCommitsPolicyViolation instances are now constructed with an ERR_VALIDATION prefix, and this wrapper adds the same prefix again. The surfaced message therefore contains duplicate error codes; normalize the nested message here or assign the code at only one layer.
      throw new Error(`${ERR_VALIDATION}: pushSignedCommits: refusing unsigned push for branch '${branch}': ${getErrorMessage(err)}`, { cause: err });
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Balanced

if (node.type === AST_NODE_TYPES.ClassDeclaration) {
const superclassName = getLocalSuperclassName(node);
if (superclassName && node.id) {
localClassExtends.set(node.id.name, superclassName);
if (err instanceof PushSignedCommitsUnsupportedShape) {
throw new Error(
`pushSignedCommits: refusing unsigned push for branch '${branch}': ${getErrorMessage(err)}. ` +
`${ERR_VALIDATION}: pushSignedCommits: refusing unsigned push for branch '${branch}': ${getErrorMessage(err)}. ` +
@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot Quick triage nudge for this PR.

Please refresh the branch if GitHub can update it cleanly, review any remaining maintainer-facing follow-up, and run the pr-finisher skill before handing back to maintainers.

  • No unresolved review threads were detected in the available snapshot.
  • Failed checks listed in compact candidate report: none.

Run: https://github.com/github/gh-aw/actions/runs/31263551516

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 7.27 AIC · ⌖ 6.48 AIC · ⊞ 6.1K ·
Comment /souschef to run again

- Replace program-wide class name map with ESLint scope-based resolution
  in require-error-code-in-thrown-error rule; this correctly handles
  shadowing (e.g. a parameter named CustomError shadows the outer class)
- Add shadowing test cases to rule tests
- Add stripLeadingErrorCode helper in push_signed_commits.cjs and use it
  when composing wrapper messages so ERR_VALIDATION is not emitted twice

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
…level strip

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>

Copilot AI commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Quick triage nudge for this PR.

Please refresh the branch if GitHub can update it cleanly, review any remaining maintainer-facing follow-up, and run the pr-finisher skill before handing ba...

Addressed the two reviewer issues:

  1. Scope-based class resolution (eslint-factory/src/rules/require-error-code-in-thrown-error.ts): Replaced the program-wide localClassExtends name map with ESLint scope-based resolution via sourceCode.getScope(callee). This correctly handles shadowing — e.g. a function parameter named CustomError is found first in the function scope and its def.type is 'Parameter' (not 'ClassName'), so it is not treated as an Error subclass. Added both valid (shadowed — not flagged) and invalid (unshadowed — still flagged) test cases to confirm.

  2. Double ERR_VALIDATION prefix (actions/setup/js/push_signed_commits.cjs): Added stripLeadingErrorCode() helper (with a JSDoc note that it strips exactly one level, which matches the single-prefix inner messages) and applied it at both wrapping throw sites (lines 840 and 849).

Commits: a6ade3d (scope fix + double-prefix fix) and 4adf9c0 (review follow-up).

Copilot AI requested a review from gh-aw-bot August 8, 2026 15:22
@pelikhan
pelikhan merged commit 59aacd1 into main Aug 8, 2026
@pelikhan
pelikhan deleted the copilot/fix-eslint-require-error-code branch August 8, 2026 15:23
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.86.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eslint-factory: require-error-code-in-thrown-error misses thrown custom Error subclasses

4 participants