Error handling patterns

From farming-labs/fm-skills · View interactive version

Error handling patterns is an AI agent skill from the GitHub repository farming-labs/fm-skills. Explains error handling patterns in web applications including error boundaries, retries, recovery UX, normalized error shapes, graceful degradation, and observability hooks. Use when designing how applications should fail, recover, and communicate problems to users.

How to use this skill

Skill file

Error Handling Patterns

Overview

Error handling is not only about preventing crashes. It is about deciding:

Good error handling makes failures understandable, containable, and recoverable.

Types of Errors

Different failures need different handling.

| Error Type | Example | Typical Response |

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

| Validation | Invalid form input | Inline correction guidance |

| Network | Timeout, offline, DNS failure | Retry, offline fallback, preserve user intent |

| Authorization | 401, 403 | Re-auth, permission message, redirect |

| Server | 500, dependency outage | Retry or graceful fallback |

| Not found | Missing route or deleted record | Not-found UI, navigation recovery |

| Conflict | Stale write, version mismatch | Refresh, merge, or ask user to resolve |

| Rendering | Component crash | Error boundary, fallback UI |

| Background failure | Polling or refresh failure | Non-blocking notification, degraded data state |

Treating all failures the same leads to confusing UI and bad recovery behavior.

Error Handling Goals

Good systems try to:

Layered Error Handling

Handle errors at the right layer.

Component / Inline Layer

Good for:

Page / Route Layer

Good for:

Application / Global Layer

Good for:

Service / Backend Layer

Good for:

Not every error should bubble to the top of the app. Most should be handled close to the failing task.

Normalize Error Shapes

Applications become easier to handle when errors are mapped into a consistent shape.

{
  code: 'NETWORK_TIMEOUT',
  message: 'Request timed out',
  retryable: true,
  statusCode: 504,
  requestId: 'req_123'
}

Useful normalized fields:

Without normalization, each component has to guess how to interpret failures.

User-Facing Error UX

The UI should communicate what happened and what the user can do next.

Good Error Messages

Bad:

Unknown exception occurred

Better:

We couldn’t save your changes. Check your connection and try again.

Inline vs Global Errors

| UI Placement | Best For |

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

| Inline near field | Validation and input-specific errors |

| Inline near component | Failed widget or card action |

| Page-level message | Page data load failure |

| Toast | Secondary status updates, non-blocking failures |

| Full-screen fallback | Fatal route or app crash |

If the failure affects only one action, keep the error near that action.

Preserve User Intent

Failure handling is better when users do not lose work.

Examples:

Losing work turns a recoverable failure into a frustrating experience.

Retry Patterns

Retries are useful for transient failures, not permanent ones.

Good retry candidates:

Bad retry candidates:

Backoff Pattern

for (let attempt = 0; attempt < 3; attempt++) {
  try {
    return await request();
  } catch (error) {
    if (!isRetryable(error)) throw error;
    await wait(2 ** attempt * 500);
  }
}

Retries should usually use backoff and a cap. Blind aggressive retries can amplify outages.

Idempotency and Safe Recovery

Retries are much safer when the backend supports idempotent operations.

Examples:

Frontend recovery design is stronger when backend semantics are predictable.

Error Boundaries

Rendering crashes should not take down the whole application if they can be contained.

Error boundaries are useful for:

Conceptual Example

<ErrorBoundary fallback={<WidgetError />}>
  <RevenueChart />
</ErrorBoundary>

Important limitation:

Async Error States

Many application failures happen in async flows:

These need explicit UI states:

If those states are not modeled directly, async errors usually leak into inconsistent UI.

Graceful Degradation

Sometimes the best response is not full failure but reduced capability.

Examples:

Graceful degradation is valuable when:

Page Load Error Patterns

Full-Page Failure

Use when the entire route depends on one failed resource.

Typical options:

Partial Failure

Use when one page section failed but the rest still works.

Typical options:

Partial failures are often better than blank screens.

Mutation Error Patterns

Mutations need careful handling because user intent is active and recent.

Common rules:

Example:

try {
  await saveProfile(input);
  setStatus('saved');
} catch (error) {
  setStatus('error');
  setErrorMessage(toUserMessage(error));
}

Auth and Permission Errors

Auth failures need different handling from generic server errors.

Examples:

Recommended responses:

Accessibility in Error Handling

Errors should be perceivable and navigable.

Important patterns:

Accessible error handling is part of functional correctness, not just polish.

Observability Hooks

Not every user-facing error should become a noisy alert, but important failures should be observable.

Useful data to capture:

This helps distinguish:

Common Anti-Patterns

| Anti-Pattern | Why It Hurts |

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

| Generic “Something went wrong” everywhere | Gives no recovery path |

| Swallowing errors silently | Fails without user or developer visibility |

| Retrying non-retryable failures | Wastes time and increases confusion |

| Clearing user input on failed submit | Loses work |

| Treating empty state as error | Produces wrong UX |

| Full-page crash for one failed widget | Over-scopes the failure |

Decision Guide

Use this rough mapping:

Rules of Thumb

Related Skills

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.