Skip to content
Back to skills

Error Handling

ASecurity

Cross-cutting standards for error design, response shapes, error codes, and boundary placement. Use when handling errors, designing exception flows, or standardizing error responses.

  • 43 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added May 30, 2026
developmentgojavaapidatabase

Works with

  • api

Security analysis

A100/100

Scanned May 30, 2026

npx -y skills add ComeOnOliver/skillshub --skill error-handling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Error Handling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Error Handling
[![Security: A β€” Skills Directory](https://www.skillsdirectory.com/api/skills/comeonoliver-error-handling/badge)](https://www.skillsdirectory.com/skills/comeonoliver-error-handling)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: Common Error Handling
description: Cross-cutting standards for error design, response shapes, error codes, and boundary placement. Use when handling errors, designing exception flows, or standardizing error responses.
metadata:
  labels: [error-handling, exceptions, resilience, api-errors]
  triggers:
    files:
      [
        '**/*.service.ts',
        '**/*.handler.ts',
        '**/*.controller.ts',
        '**/*.go',
        '**/*.java',
        '**/*.kt',
        '**/*.py',
      ]
    keywords:
      [
        'error handling',
        exception,
        'try catch',
        'error boundary',
        'error response',
        'error code',
        throw,
        Result,
      ]
---

# Common Error Handling Standards

## **Priority: P1 (OPERATIONAL)**

Consistent, predictable error handling is the backbone of maintainable systems. Errors are first-class citizens β€” design them explicitly.

## πŸ— Error Response Shape (HTTP APIs)

All API errors MUST use a consistent envelope:

```json
{
  "error": {
    "code": "USER_NOT_FOUND",
    "message": "The requested user does not exist.",
    "traceId": "4bf92f3577b34da6a3ce929d0e0e4736",
    "details": []
  }
}
```

| Field     | Rule                                                                              |
| --------- | --------------------------------------------------------------------------------- |
| `code`    | SCREAMING_SNAKE_CASE machine-readable code. Never localize.                       |
| `message` | Human-readable English summary. Safe for end-users (no stack traces).             |
| `traceId` | Correlation ID from the request context.                                          |
| `details` | Optional array of field-level validation errors. Empty for non-validation errors. |

## πŸ—‚ Error Classification

| Layer          | Error Type                  | Strategy                                 |
| -------------- | --------------------------- | ---------------------------------------- |
| Validation     | `400 Bad Request`           | Return `details[]` with field paths      |
| Authentication | `401 Unauthorized`          | Generic message β€” never expose reason    |
| Authorization  | `403 Forbidden`             | Log attempt, never expose role info      |
| Not Found      | `404 Not Found`             | Distinguishable from auth errors         |
| Conflict       | `409 Conflict`              | Include conflicting resource ID          |
| Unhandled      | `500 Internal Server Error` | Log full context, return generic message |

## πŸ“¦ Error Wrapping vs Replacement

- **Wrap** when adding context: `fmt.Errorf("processOrder: %w", err)` (Go) / `new ServiceError('msg', { cause: err })` (JS).
- **Replace** only when the original error leaks sensitive internal details.
- **Never swallow**: Catch without logging or re-throwing hides bugs β€” forbidden.

## πŸ›‘ Boundary Placement

- **API Layer**: Translate domain/infrastructure errors into HTTP responses. Use a global exception filter/middleware.
- **Domain Layer**: Throw domain-specific errors (e.g., `InsufficientStockError`). Never reference HTTP status codes.
- **Infrastructure Layer**: Throw infrastructure errors (e.g., `DatabaseConnectionError`). Wrap 3rd party exceptions.
- **Never**: Let infrastructure errors (raw DB/network exceptions) bubble up to the API response.

```
Request β†’ [API Layer: maps to HTTP] β†’ [Domain: business errors] β†’ [Infra: DB/network errors]
```

## πŸ”’ Error Code Design

- Codes are **permanent IDs** β€” treat them like API contracts. Once published, never rename.
- Format: `<DOMAIN>_<NOUN>_<VERB>` β†’ `ORDER_PAYMENT_FAILED`, `USER_EMAIL_DUPLICATE`.
- Define in a centralized constants file; never inline magic strings.

## Anti-Patterns

- **No `catch(e) {}`**: Always log or re-throw.
- **No stack traces in responses**: Leak internal structure to attackers.
- **No generic `500` for validation**: Use `400` with `details`.
- **No HTTP status codes in domain layer**: Domain errors are business concepts, not transport decisions.
- **No error-code proliferation**: Prefer a small, well-documented set over one code per exception class.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…