Skip to content

Conversation

github-actions[bot]
Copy link

Closes #551

Applied changes based on Issue #551.

I’ve refactored src/utils/error.ts to centralize the instanceof Error check into a reusable type‐guard, and updated the two existing helpers to leverage it—improving readability and avoiding duplication, with zero change in behavior. Here’s what was done:


Changes in src/utils/error.ts

/**
 * @file Error handling utilities module.
 * Provides functions to convert unknown errors safely to message and stack strings.
 */

/**
 * Type guard to determine if a value is an Error.
 * @param error - The value to check.
 * @returns True if the value is an instance of Error.
 */
function isError(error: unknown): error is Error {
  return error instanceof Error;
}

/**
 * Safely extracts the message from an unknown error object.
 * @param error - The caught error or value.
 * @returns The error message string.
 */
export function toErrorMessage(error: unknown): string {
  return isError(error) ? error.message : String(error);
}

/**
 * Safely extracts the stack trace from an unknown error object.
 * @param error - The caught error or value.
 * @returns The error stack trace or undefined if unavailable.
 */
export function toErrorStack(error: unknown): string | undefined {
  return isError(error) ? error.stack : undefined;
}

【F:src/utils/error.ts†L6-L13】【F:src/utils/error.ts†L20-L22】【F:src/utils/error.ts†L29-L31】


Summary of refactoring:

  • Extracted a private isError() type‐guard for checking instanceof Error.
  • Consolidated the duplicate instanceof logic in toErrorMessage and toErrorStack into a single call to isError.
  • Kept all existing JSDoc comments and overall functionality identical.

No other files were touched. Let me know if you’d like any additional adjustments!

@YiweiShen YiweiShen merged commit f55e454 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-551-refactor-utils-simplify-error-utility-module-3121019612 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/error.ts

1 participant