Handling errors

From rileyhilliard/claude-essentials · View interactive version

Handling errors is an AI agent skill from the GitHub repository rileyhilliard/claude-essentials. Prevents silent failures and context loss in error handling. Use when writing try-catch blocks, writing error messages or error responses, designing error propagation, reviewing catch blocks, or implementing Result patterns.

How to use this skill

Skill file

If the current repo has its own rules/skills covering this topic (check .claude/rules/ and repo CLAUDE.md), those take precedence. Apply this skill where they're silent.

Handling Errors

Error Categories

| Type | Examples | Handling |

| ------------ | ---------------------------------- | --------------------------- |

| Expected | Validation, Not found, Unauthorized| Return Result type, log info|

| Transient| Network timeout, Rate limit | Retry with backoff, log warn|

| Unexpected| Null reference, DB crash | Log error, show support ID |

| Critical | Auth down, Payment gateway offline | Circuit breaker, alert |

Writing Error Messages

An error message should tell the reader what happened, why, and what they can do next. The reader may be a developer, an end user, or an agent calling your API or tool, and an agent can only recover from what the message says. A message like "invalid input" forces them to guess, retry blindly or give up.

| Instead of | Write |

| --- | --- |

| Error: invalid input | Invalid date "2026-13-01": month must be 1-12. Use YYYY-MM-DD. |

| Upload failed | Upload failed: report.pdf is 14 MB and the limit is 10 MB. Compress it or split it. |

| Request failed | GET /v1/orders returned 429. Retry after 30s (see the Retry-After header). |

For text an end user will read, also avoid blame ("You entered...") and internal details, and use the writer skill's UX Writer persona. Developers get the full detail in logs; users get the short version with the next step.

Fail Fast vs Degrade Gracefully

Fail fast for critical dependencies:

await connectToDatabase(); // Throws on failure - app can't run without it

Degrade gracefully for optional features:

const prefs = await loadPreferences(userId).catch(() => DEFAULT_PREFS);

Log at the Right Layer

// Bad: logging at every layer writes the same error 3x
async function fetchData() {
  try { return await fetch(url); }
  catch (e) { console.error("Fetch failed:", e); throw e; }
}

// Good: log once where the error is handled
async function fetchData() {
  const response = await fetch(url);
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return response;
}
// Top level logs the error once

Language-Specific Patterns

Anti-Patterns

| Pattern | Problem | Fix |

|---------|---------|-----|

| Empty catch blocks | Hides errors | Log or re-throw |

| return false on error | Loses context | Return Result type |

| Generic "Error" messages | Undebuggable | Say what, why and the next step |

| Logging same error at each layer | Log pollution | Log once at boundary |

| Bare except: / catch (e) all | Catches system signals | Catch specific types |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.