AGENT SKILL

error-handling

Designs typed, observable, recoverable error handling - an explicit taxonomy of retryable vs terminal failures, Result types at boundaries, single-point logging, and retry policies with real backoff numbers. Use when someone asks "should I retry this error", "how do I structure error types", "why are my logs full of duplicate stack traces", "where do I put the try/catch", or is designing failure paths for an API, job, or client. Do NOT use for building the circuit breaker component itself - use circuit-breaker-builder instead; do NOT use for handling 429s against third-party APIs - use rate-limit-handler instead; do NOT use for writing user-facing error copy - use error-message-writer instead.

License 许可
MIT
所属仓库
SkillMedev/skills
最近更新
2026-10-14
Stars
★ 25
Forks
3

01 前言 Front Matter

name
error-handling
metadata
{ "title": "Error Handling" }
description
Designs typed, observable, recoverable error handling - an explicit taxonomy of retryable vs terminal failures, Result types at boundaries, single-point logging, and retry policies with real backoff numbers. Use when someone asks "should I retry this error", "how do I structure error types", "why are my logs full of duplicate stack traces", "where do I put the try/catch", or is designing failure paths for an API, job, or client. Do NOT use for building the circuit breaker component itself - use circuit-breaker-builder instead; do NOT use for handling 429s against third-party APIs - use rate-limit-handler instead; do NOT use for writing user-facing error copy - use error-message-writer instead.

03 SKILL.md 正文

Error Handling

Most production incidents are not caused by the original failure - they are caused by the handling: a retry storm that turns one slow dependency into a full outage, a swallowed exception that surfaces as silent data corruption three weeks later, or duplicate logging at five layers that buries the root cause. This skill designs error handling as a first-class part of the system: every failure classified, every class given one owner and one response.

Operating procedure

Step 1: Gather inputs

  • The unit of work being protected (a request, a job, a batch, a UI subtree).
  • Every external dependency it touches and which operations are idempotent.
  • Where errors currently surface (logs, tracker, user) and who consumes each.
  • Latency budget - it caps how much retrying is even allowed.

Step 2: Build the error taxonomy

Classify every failure the unit can hit into exactly one row. The classification decides the response mechanically - no per-call-site judgment.

Class Examples Retry? Response
Transient infrastructure network reset, DNS blip, connection pool timeout Yes, with backoff Retry, then degrade or fail with correlation id
Server transient 502/503/504, database failover Yes, with backoff Retry; circuit-break the dependency if sustained
Rate limiting 429 Yes - honor Retry-After Back off to the server's hint, never your own schedule (pair with rate-limit-handler)
Client error 400, 401, 403, 404, 422, validation failure Never Fix the request or surface to the caller; retrying a 4xx just repeats the failure
Business rule insufficient funds, seat already booked Never Return as a typed value; this is a domain outcome, not an exception
Bug null deref, type error, assertion failure Never Crash the unit of work, log loudly, alert - retrying a bug corrupts state

Principles behind the table: distinguish expected failures (validation, not-found) from bugs; model expected failures as values and let truly exceptional bugs throw; fail fast and loud in development, degrade gracefully in production.

Step 3: Type the errors

type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };

class NotFoundError extends Error {
  readonly code = 'NOT_FOUND';
  readonly retryable = false;
  constructor(readonly resource: string, opts?: { cause?: unknown }) {
    super(`${resource} not found`, opts);
  }
}
  • Give each error a stable code for programmatic handling and i18n, and a retryable flag so the taxonomy travels with the error.
  • Use a Result type at API boundaries so callers must handle the error path; never swallow with an empty catch {}.
  • Preserve the cause chain: new Error(msg, { cause }) - a re-wrapped error without its cause is undebuggable.

Step 4: Place the boundaries

  • Wrap each independent unit (a request, a React subtree, a job) in exactly one boundary that catches, logs, and renders/returns a fallback.
  • The boundary owns the recovery decision and never re-throws silently.
  • Log once, at the boundary, with full context - not at every layer. Duplicate logs at each layer hide the real cause behind noise. Inner layers add context to the error and re-throw; only the boundary writes the log line.
  • Validate input at the edge so the core can assume well-formed data instead of defensively checking everywhere.

Step 5: Set the retry policy

Retry only rows the taxonomy marks retryable, and only on idempotent operations (non-idempotent writes need an idempotency key first - see idempotency-enforcer).

  • Exponential backoff with full jitter: delay = random(0, min(cap, base × 2^attempt)). Sane defaults: base 200ms, cap 20-30s.
  • Max 3 attempts for user-facing request paths (the user is waiting); up to 5 for background jobs.
  • Total retry time must fit the latency budget: a 3-attempt policy at base 200ms adds up to ~1.4s worst case before jitter - check that against the SLO.
  • Jitter is not optional: synchronized retries after a shared failure create a thundering herd that re-kills the recovering dependency.
  • Add a circuit breaker in front of any dependency where sustained failure is possible, so a dead dependency sheds load instead of amplifying it (breaker mechanics: circuit-breaker-builder).

Step 6: Wire observability

  • Attach context (request id, user id, key inputs) where the error is captured, not only at the throw site.
  • Send to an error tracker grouping by code + stack; alert on new error codes and on rate spikes, not on every occurrence.
  • Return the user a safe message plus a correlation id; never leak stack traces, SQL, or internal hostnames to clients.
  • Make cleanup run on every path - finally, defer, context managers - including the error paths.

Worked artifact: boundary with policy applied

async function handleRequest(req: Request): Promise<Response> {
  const correlationId = req.headers.get('x-request-id') ?? crypto.randomUUID();
  try {
    const input = parseInput(req);              // 400 on failure - terminal
    const order = await withRetry(              // taxonomy-driven retry
      () => orders.fetch(input.orderId),
      { attempts: 3, baseMs: 200, capMs: 20_000, retryOn: (e) => e.retryable === true },
    );
    return json(200, order);
  } catch (err) {
    logger.error({ err, correlationId, path: req.url }); // logged ONCE, here
    const code = err instanceof AppError ? err.code : 'INTERNAL';
    const status = err instanceof AppError ? err.status : 500;
    return json(status, { code, message: safeMessage(code), correlationId });
  }
}

Deliverable

Produce an error-handling design containing: the filled taxonomy table for this system, the typed error hierarchy with codes and retryable flags, a boundary map (which layer logs, which layers wrap-and-rethrow), the retry policy with concrete numbers checked against the latency budget, and the observability plan (grouping, alert rules, correlation-id flow).

Do NOT

  • Do not retry 4xx/validation/business errors - it repeats the failure and doubles load.
  • Do not retry non-idempotent operations without an idempotency key.
  • Do not retry without jitter; synchronized backoff is a self-inflicted DDoS.
  • Do not log the same error at multiple layers.
  • Do not swallow errors in empty catch blocks - silent corruption is the most expensive failure mode there is.
  • Do not leak internals to clients; safe message + correlation id, always.
  • Do not put dependencies in the fallback path - an error handler that can itself fail (network call, template render) turns one incident into two. Keep it dependency-free and simple.

Quality bar

  • Every failure the unit can hit maps to exactly one taxonomy row; no call site makes an ad-hoc retry decision.
  • Every error carries a stable code, a retryable flag, and its cause chain.
  • One log line per failure, at the boundary, with correlation id.
  • Retry math verified against the latency budget; circuit breaker in front of every breakable dependency.
  • Node services install a global unhandled-rejection handler that logs and exits cleanly - an unhandled rejection crashes the process.
  • Batch jobs collect per-item errors and continue, reporting a summary, instead of dying on item 3 of 10,000.

04 同仓库更多 Skills

SkillMedev
SkillMedev/skills

有氧运动计划

zone-2-cardio-plan

当有人要求制定稳态有氧训练计划、进行有氧基础训练或达到第二区间、安排轻松或耐力型的每周有氧训练、按心率区间训练,或提高静息心率、最大摄氧量或耐力时,可以使用此方案。该方案构建了一个包含训练计划结构、训练方式选择和进度安排的心率区间有氧训练模块。本方案仅提供一般健身指导,不构成医疗建议。请勿用于设计力量或举重训练计划(请使用力量训练计划)、高强度间歇训练 (HIIT) 或循环训练(请使用体能训练和 HIIT 计划),或灵活性/柔韧性训练(请使用灵活性训练计划)。

★ 25
SkillMedev
SkillMedev/skills

youtube-script-writer

Writes retention-engineered long-form YouTube scripts on a hook/body/payoff architecture - a first-30-seconds hook that survives the retention cliff, a beat-sheet body with mini-hooks and bridges, timed pattern interrupts, and a payoff that over-delivers - with B-roll and editing cues marked inline. Use when someone says "script my next video", "my retention graph dies at the start", "tighten this draft", or "outline a video on X". Do NOT use for the 3-second opener of TikTok, Reels, or Shorts - use video-hook-writer instead; do NOT use for spoken voiceover synced to on-screen tutorial actions - use narration-script instead; do NOT use for a sales demo built on a buyer's discovery notes - use demo-script instead; do NOT use to design the thumbnail and title - use thumbnail-concept instead.

★ 25
SkillMedev
SkillMedev/skills

win-back-campaign

Designs segmented win-back email sequences for dormant users - dormancy tiers, a three-beat sequence with a single proportional incentive, and a suppression rule - and measures success by re-activation, not opens. Use when someone says "our users went quiet", "design a win-back campaign", "how do I re-engage lapsed customers", "should we email people who haven't logged in for months", or lifecycle messaging has stopped landing with a lapsed segment. Do NOT use for recovering an unfinished checkout - use abandoned-cart-sequence instead. Do NOT use for diagnosing and fixing why active customers cancel - use churn-reduction instead. Do NOT use for building always-on onboarding or nurture flows - use email-drip-builder instead.

★ 25
SkillMedev
SkillMedev/skills

wikipedia-style

Writes neutral, verifiable, encyclopedic prose following Wikipedia's core content policies and manual of style - NPOV, inline citations, notability-backed claims, and a strict ban on promotional language. Use when someone asks "write this like a Wikipedia article", "draft a Wikipedia page for this company or person", "make this neutral and encyclopedic", or "why does my article draft keep getting rejected". Do NOT use for persuasive or opinion writing - use op-ed-writer instead; for thesis-driven academic papers, use academic-essay; for verifying claims in existing text, use fact-checker.

★ 25
SkillMedev
SkillMedev/skills

whitepapers

Structures and writes authoritative B2B whitepapers - a standalone executive summary, a data-sized problem statement, methodology, evidence-driven findings, recommendations, and a soft call to action - flagging every claim that needs a real citation. Use when a marketer asks "write a whitepaper on this topic", "turn our research into a lead-gen asset", "make this credible enough for executives", or a draft reads like a product pitch instead of analysis. Do NOT use for customer-story-driven proof - use case-study-builder instead; for policy audiences, use policy-brief.

★ 25
SkillMedev
SkillMedev/skills

white-space-analysis

Finds unmet market gaps by ranking jobs-to-be-done on importance versus satisfaction, overlaying them against current solutions and workarounds, and validating that each gap is real, winnable, and worth winning. Use when someone asks "where are the gaps in this market", "what's underserved here", "where should we play next", or is hunting for product, feature, or market-entry opportunities. Do NOT use for profiling competitors and building battle cards - use competitive-intelligence instead; for putting a dollar figure on an identified opportunity - use market-sizing instead; for extracting jobs-to-be-done from raw customer interviews - use jtbd-extractor instead.

★ 25