← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

JavaScript Error Types & Custom Errors

javascript ES5 Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list ESLint and TypeScript as the tools. ESLint can flag bare catch(e) with no type discrimination and throwing string literals, but these rules are not default — they require specific plugins or rules (e.g. no-throw-literal). TypeScript can catch some cases but not runtime error-type narrowing. This places it at d5 rather than d3 (not a default linter catch) or d7 (automated tooling does exist).

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is to use custom error classes extending Error with meaningful name and code properties, and to catch specific types. This is a small, pattern-based refactor — replacing generic catch blocks and string throws with proper class definitions — contained within one component or file at a time, not a cross-cutting architectural change.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to covers web and cli contexts broadly, but the impact is scoped to error-handling code paths. Misuse of error types doesn't poison the broader architecture — it affects catch blocks and error propagation locally. Each fix is contained to the module or component that handles a specific error flow, so the structural weight is moderate but not system-wide.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception is explicit: developers believe catch(e) is safe, but broad catches silently swallow unexpected bugs. This contradicts patterns from other languages (e.g. Java/C# where typed catch clauses are enforced), and the 'obvious' pattern in JavaScript — a plain catch — is almost always the wrong approach for robust error handling. This is a well-documented but frequently violated gotcha that contradicts how similar constructs behave in typed languages.

About DEBT scoring →

Also Known As

TypeError RangeError custom error error handling JavaScript

TL;DR

JavaScript has built-in error types (TypeError, RangeError, SyntaxError) and supports custom error classes — enabling typed error handling and meaningful error hierarchies.

Explanation

Built-in errors: Error (base), TypeError (wrong type), RangeError (value out of range), ReferenceError (undefined variable), SyntaxError (invalid syntax), URIError, EvalError. Custom errors extend Error: class ValidationError extends Error {}. Key gotcha: Error.prototype.stack and instanceof checks can break across browser contexts and iframes. For custom errors, always set this.name = 'ValidationError' explicitly. Use instanceof for catching specific error types in catch blocks.

Common Misconception

Catching all errors with catch(e) is safe — broad catches hide bugs; catch specific error types and let unexpected errors propagate.

Why It Matters

Custom error types enable specific error handling — a NetworkError can trigger retry logic while a ValidationError shows user feedback, which is impossible with generic Error objects.

Common Mistakes

  • Not setting this.name in custom errors — error.name stays 'Error' instead of 'ValidationError'.
  • Not calling super(message) in custom error constructor — error.message is empty.
  • Catching all errors and swallowing unexpected ones — only catch errors you can handle.
  • Throwing strings instead of Error objects — strings have no stack trace.

Code Examples

✗ Vulnerable
// Throwing strings — no stack trace:
throw 'User not found'; // Untyped, no stack

// Catching everything — hides bugs:
try {
    processPayment();
} catch (e) {
    console.log('Error'); // TypeError from programming bug silently swallowed
}
✓ Fixed
// Custom error hierarchy:
class AppError extends Error {
    constructor(message: string, public readonly code: string) {
        super(message);
        this.name = this.constructor.name; // 'AppError'
    }
}
class ValidationError extends AppError {}
class NotFoundError extends AppError {}

// Specific handling:
try {
    await processPayment(order);
} catch (e) {
    if (e instanceof ValidationError) showUserError(e.message);
    else if (e instanceof NetworkError) scheduleRetry();
    else throw e; // Rethrow unknown errors — don't swallow
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 2 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S
No pings yet today
Amazonbot 9 Unknown AI 3 Google 2 Ahrefs 2 ChatGPT 2 Majestic 1 Perplexity 1
crawler 17 crawler_json 2 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Use custom error classes extending Error with a meaningful name and code property — catch specific error types not generic Error
📦 Applies To
javascript ES5 web cli
🔗 Prerequisites
🔍 Detection Hints
catch(e) with no type check; throwing string literals instead of Error objects; no custom error classes
Auto-detectable: ✓ Yes eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update
CWE-755 CWE-390

✓ schema.org compliant