agentleFS
suhailkakarsuhailkakar/the-skills/error-handling/SKILL.md

error-handling

Skill26 starsChanged 6 months agoMITView on GitHub

Use when the user writes try/catch, throws errors, builds error boundaries, returns errors from an API, sets up global error handling, or designs how failures surface to users. Catches swallowed errors, leaked internals in error responses, missing graceful degradation, and "the spinner spins forever" UX.

80 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use convention-learner, caching and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • codewithmukeshSkill

    convention-learner

    Detects and enforces project-specific coding conventions by analyzing existing codebase patterns. Learns naming conventions, folder structure, test organization, and coding style from the existing code. Load when: "conventions", "coding standards", "project patterns", "enforce style", "detect patterns", "learn conventions", "code consistency".

    Also in 3 projects that use this

  • wpankSkill

    caching

    Caching strategies, invalidation, eviction policies, HTTP caching, distributed caching, and anti-patterns. Use when designing cache layers, choosing eviction policies, debugging stale data, or optimizing read-heavy workloads.

    Also in 6 projects that use this

  • diego-tobalinaSkill

    logging

    Logging best practices for Java (SLF4J/Logback) and Node.js (Winston/Pino). Use for structured logging, log levels, and avoiding sensitive data exposure.

    Also in 12 projects that use this

  • codewithmukeshSkill

    opentelemetry

    OpenTelemetry observability for .NET 10 applications. Covers traces, metrics, and logs using the OpenTelemetry SDK with OTLP export. Includes custom ActivitySource, IMeterFactory metrics, resource configuration, and Aspire Dashboard integration. Load this skill when setting up distributed tracing, custom metrics, OTLP export, or when the user mentions "OpenTelemetry", "OTLP", "traces", "spans", "Activity", "ActivitySource", "metrics", "IMeterFactory", "Meter", "Counter", "Histogram", "Gauge", "telemetry", "observability", "distributed tracing", "OTEL", or "Aspire Dashboard".

    Also in 3 projects that use this

  • MovGP0Skill

    modern-csharp

    Modern C# style guidance for Bendex and MicroSea code changes, including primary constructors, expression-bodied members, var usage, target-typed new, lambdas, and pattern matching.

    Also in 3 projects that use this

  • MicrosoftSkill

    resilience

    Apply resilience patterns (retry, backoff, circuit breaking) when writing or reviewing code that makes outbound HTTP/REST calls, database operations, or any I/O that can experience transient failures. Use this skill when: (1) writing new code that calls external services or APIs, (2) reviewing code that uses fetch, HTTP clients, or service-to-service calls, (3) adding queue consumers or workers that interact with remote systems, (4) debugging intermittent failures in production.

    Also in 4 projects that use this

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 64 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If you don't know what to do with an error, let it propagate to the boundary that does.

    line 51
  2. Distinctive:

    Every async UI surface needs three states: loading, error, empty.

    line 137
  3. Distinctive:

    Search index unreachable? Fail open (show "search temporarily unavailable", let the rest of the app work).

    line 159

“Common” is measured: kept by at least 6 in 10 of the 64 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Error Handling & User-Facing Failures

Two failure modes are unforgivable: silently swallowing errors so debugging is impossible, and leaking internals so attackers learn your stack. The middle path is narrow and worth getting right.

When to use

  • Writing try/catch, Promise.catch, error boundaries, error.tsx
  • Returning errors from an API or Server Action
  • Designing fallback UI when something fails
  • Wrapping third-party calls that might throw
  • Reviewing how the app behaves when the network / DB / dependency goes down

When to skip

  • Pure deterministic functions with no I/O and no exceptional conditions

The mental model

Two kinds of errors. Treat them differently:

KindExamplesWhat to do
Expected"email already taken", "out of stock", "not found"Return a typed result; show user-facing UI; log at info
UnexpectedDB unreachable, OOM, bug, third-party 500Catch at the boundary, log full detail, show a generic message, alert

Conflating them produces both bad UX (cryptic errors for normal cases) and bad ops (alert fatigue from "expected" errors).

The rules

1. Never swallow.

// BAD — error vanishes; you'll wonder why nothing works at 3am
try { await doThing() } catch {}

// BAD — log + ignore is also swallowing
try { await doThing() } catch (e) { console.log(e) }

// GOOD — handle deliberately, or rethrow
try {
  await doThing()
} catch (e) {
  if (isExpectedNotFound(e)) return null
  throw e
}

If you don't know what to do with an error, let it propagate to the boundary that does.

2. Catch at the boundary. Not in the middle.

// BAD — every layer wraps in try/catch and re-throws or returns null. Stack trace is lost.

// GOOD — boundaries catch (route handler, job handler, top-level UI). Inner code throws freely.
export async function POST(req: Request) {
  try {
    return await handler(req)
  } catch (err) {
    return errorResponse(err) // single place that maps errors → response
  }
}

3. User-facing message ≠ error.message.

// BAD — leaks internals
return Response.json({ error: err.message }, { status: 500 })
// "PrismaClientKnownRequestError: relation 'user' does not exist" → tells attackers your DB

// GOOD — generic to user, full detail to logs
logger.error({ err, requestId }, 'request_failed')
return Response.json(
  { error: { code: 'internal_error', message: 'Something went wrong.', requestId } },
  { status: 500 },
)

The requestId lets you correlate the user's complaint to the log entry without revealing internals.

4. Result types for expected errors. Throws for unexpected.

// Expected outcome → typed result
type Result<T, E = string> = { ok: true; value: T } | { ok: false; error: E }

async function createUser(email: string): Promise<Result<User, 'email_taken' | 'invalid_email'>> {
  if (!isEmail(email)) return { ok: false, error: 'invalid_email' }
  const existing = await db.user.findUnique({ where: { email } })
  if (existing) return { ok: false, error: 'email_taken' }
  const user = await db.user.create({ data: { email } })
  return { ok: true, value: user }
}

// Unexpected → throw, let the boundary catch

The caller pattern-matches; the type system makes you handle every error case.

5. Tagged error classes. Don't string-match err.message.

// BAD — fragile
catch (e) { if (e.message.includes('not found')) ... }

// GOOD — branded
class NotFoundError extends Error { _tag = 'NotFoundError' as const }
class RateLimitError extends Error { _tag = 'RateLimitError' as const }

catch (e) {
  if (e instanceof NotFoundError) return new Response(null, { status: 404 })
  if (e instanceof RateLimitError) return new Response('Slow down', { status: 429 })
  throw e
}

6. Always log with structure and correlation.

logger.error({
  err,                     // logger serializes Error properly (stack, cause)
  requestId,
  userId,
  route: '/api/x',
  durationMs,
}, 'request.failed')

{ err } not String(err). Modern loggers (Pino, Logtape) serialize the chain via err.cause. See observability.

7. UI: never strand the user on a spinner.

Every async UI surface needs three states: loading, error, empty. Plus a way out (retry, back, contact support).

// Next.js: app/products/error.tsx + loading.tsx
'use client'
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
  useEffect(() => { logToService(error) }, [error])
  return (
    <div>
      <h2>We hit a problem.</h2>
      <button onClick={reset}>Try again</button>
      <p className="text-xs text-muted-foreground">Error ID: {error.digest}</p>
    </div>
  )
}

In React 19+: error boundaries log the digest (server-generated ID); pair with your server-side log via that digest.

8. Fail closed for security. Fail open for non-critical features.

  • Permission check failed because Redis is down? Fail closed (deny access).
  • Search index unreachable? Fail open (show "search temporarily unavailable", let the rest of the app work).

The default for new features should be fail-closed; relax deliberately for feature-by-feature non-criticality.

9. Graceful degradation: design fallbacks before you need them.

Third-party API down → cached last-known-good value, or a "service degraded" badge, not a 500. Image CDN down → original image URL or placeholder, not broken images. Analytics down → swallow the error (analytics isn't core), don't break the user flow.

Identify what's load-bearing for the user task and what's auxiliary. Auxiliary failures must not break load-bearing flows.

10. Top-level unhandledrejection and uncaughtException handlers.

process.on('unhandledRejection', (reason) => logger.error({ reason }, 'unhandled.rejection'))
process.on('uncaughtException', (err) => {
  logger.fatal({ err }, 'uncaught.exception')
  process.exit(1) // crash & restart; don't keep running in unknown state
})

A process that's seen an uncaught exception is in undefined state. Log, exit, let the orchestrator restart it. Don't try to "keep going."

11. Preserve the cause when wrapping.

// BAD — lose the original
throw new Error('failed to load user')

// GOOD — chain via cause (ES2022+)
throw new Error('failed to load user', { cause: err })

Modern loggers serialize the cause chain. You see "user load failed → DB query failed → connection timeout" in one log line.

React error boundaries — what they catch and don't

  • Catch: render-time errors in children
  • Don't catch: errors in event handlers, async code (.then()/useEffect), errors thrown after suspension
  • For async errors in components, surface them through state and re-throw during render to enter the boundary, OR use a library like react-error-boundary's useErrorBoundary().showBoundary(err)

War story

A vibe-coded SaaS wrapped every Server Action body in try { ... } catch (e) { console.log(e); return null }. Worked great until one day every checkout silently failed because Stripe rotated a webhook secret and the error was eaten. Three days of $0 revenue before someone noticed. The fix wasn't fancy: remove the swallow, let the route handler's outer try/catch log it, and an alert fired the next time it happened — within minutes.

Quick checklist

  • No empty catch {} and no catch (e) { console.log(e) } (that's swallowing)
  • Errors caught at boundaries (route handler, job handler, error boundary), not in the middle
  • User-facing messages are generic; full detail goes to structured logs
  • requestId in every error response and matching log line
  • Expected errors use typed results; unexpected errors throw
  • Tagged error classes, not message string-matching
  • UI has loading + error + empty states for every async surface
  • Auth/permission failures fail closed; auxiliary feature failures fail open
  • Top-level handlers for unhandledRejection / uncaughtException
  • Errors wrap with { cause } to preserve the chain

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.