Back to all reviewers

Use appropriate error types

denoland/deno
Based on 4 comments
TypeScript

Functions should use specific, appropriate error handling mechanisms rather than generic or brittle approaches. This includes returning proper error codes when operations fail, using specific error types instead of generic `notImplemented` errors, and avoiding fragile error detection methods like string matching.

Error Handling TypeScript

Reviewer Prompt

Functions should use specific, appropriate error handling mechanisms rather than generic or brittle approaches. This includes returning proper error codes when operations fail, using specific error types instead of generic notImplemented errors, and avoiding fragile error detection methods like string matching.

Key practices:

  • Return meaningful error codes when operations can fail, not just success values
  • Use specific error types that accurately represent the failure condition
  • Avoid brittle error detection like e.message === "cancelled" - prefer proper error type checking
  • Add appropriate safeguards (timeouts, limits) to prevent hanging in failure scenarios

Example of improvement:

// Instead of always returning success:
setBroadcast(_bool: 0 | 1): number {
  this.#listener?.setBroadcast(_bool === 1);
  return 0; // Always success - problematic
}

// Return appropriate error codes:
setBroadcast(_bool: 0 | 1): number {
  try {
    this.#listener?.setBroadcast(_bool === 1);
    return 0;
  } catch {
    return -1; // Proper error indication
  }
}

// Instead of generic errors:
export function setFipsCrypto(_fips: boolean) {
  notImplemented("crypto.setFipsCrypto"); // Generic
}

// Use specific error types:
export function setFipsCrypto(_fips: boolean) {
  throw new Error("FIPS mode is not available"); // Specific
}
4
Comments Analyzed
TypeScript
Primary Language
Error Handling
Category

Source Discussions