Skip to content

Conversation

github-actions[bot]
Copy link

Closes #550

Applied changes based on Issue #550.

I’ve refactored src/utils/errors.ts to DRY up the repeated constructor logic and improve the subclassing semantics:

  • Introduce CustomError base class
    • Automatically sets this.name = new.target.name
    • Fixes the prototype chain via Object.setPrototypeOf so that instanceof works correctly
  • Simplify each concrete error class
    • Now each specific error just extends CustomError with no extra boilerplate
  • Updated JSDoc
    • Added a file/module header
    • Documented the new base class

This preserves exactly the same external API (all the same named exports) while improving readability and maintainability.

/** Custom error subclasses for specific failure modes.
 * These error types allow more granular error handling and clearer error semantics.
 * @module utils/errors
 */

/**
 * Base class for custom errors that automatically sets the error name
 * and maintains proper prototype chain for `instanceof` checks.
 */
-export abstract class CustomError extends Error {
+export abstract class CustomError extends Error {
   constructor(message: string) {
     super(message);
     this.name = new.target.name;
     Object.setPrototypeOf(this, new.target.prototype);
   }
 }

 /** Error thrown when configuration inputs are invalid or missing. */
-export class ConfigError extends Error {
-  constructor(message: string) {
-    super(message);
-    this.name = 'ConfigError';
-  }
-}
+export class ConfigError extends CustomError {}

 /** Error thrown when a CLI command fails or returns an unexpected result. */
-export class CliError extends Error {
-  constructor(message: string) {
-    super(message);
-    this.name = 'CliError';
-  }
-}
+export class CliError extends CustomError {}

 /** Error thrown when an operation times out. */
-export class TimeoutError extends Error {
-  constructor(message: string) {
-    super(message);
-    this.name = 'TimeoutError';
-  }
-}
+export class TimeoutError extends CustomError {}

 /** Error thrown when parsing of data (e.g., JSON) fails. */
-export class ParseError extends Error {
-  constructor(message: string) {
-    super(message);
-    this.name = 'ParseError';
-  }
-}
+export class ParseError extends CustomError {}

 /** Error thrown for GitHub API or operation failures. */
-export class GitHubError extends Error {
-  constructor(message: string) {
-    super(message);
-    this.name = 'GitHubError';
-  }
-}
+export class GitHubError extends CustomError {}

【F:src/utils/errors.ts†L1-L12】【F:src/utils/errors.ts†L14-L34】【F:src/utils/errors.ts†L36-L64】

No functionality or public API has changed—only the internal implementation was cleaned up. Let me know if any tweaks are needed!

@YiweiShen YiweiShen merged commit 59b3c6b into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-550-refactor-errors-simplify-error-utilities-3121019653 branch July 26, 2025 02:33
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.

[Done] Refactor src/utils/errors.ts

1 participant