API Error Handling

API Error Handling

Teaches an AI coding agent to build consistent API error handling: typed errors, standard JSON responses, logging, monitoring, retries with backoff, validation and circuit breakers.

Skill files5 files

references/

SKILL.md110 lines
1
---
2
name: api-error-handling
3
description: >
4
Implement comprehensive API error handling with standardized error responses,
5
logging, monitoring, retry logic, and validation patterns. Use when building
6
resilient APIs, debugging issues, improving error reporting, implementing
7
retry logic, handling HTTP error codes, managing API timeouts, designing
8
error response handling, or adding circuit breaker patterns.
9
---
10
11
# API Error Handling
12
13
## Table of Contents
14
15
- [Overview](#overview)
16
- [When to Use](#when-to-use)
17
- [Quick Start](#quick-start)
18
- [Reference Guides](#reference-guides)
19
- [Best Practices](#best-practices)
20
21
## Overview
22
23
Build robust error handling systems with standardized error responses, detailed
24
logging, error categorization, and user-friendly error messages. This skill
25
covers the full lifecycle from throwing typed errors through logging, monitoring,
26
and client-facing response formatting.
27
28
## When to Use
29
30
- Handling API errors consistently across endpoints
31
- Debugging production issues with request tracing
32
- Implementing error recovery strategies (retry, circuit breaker)
33
- Monitoring and alerting on error rates
34
- Providing meaningful, actionable error messages to clients
35
- Validating request inputs before processing
36
- Tracking error patterns over time
37
38
## Quick Start
39
40
Minimal standardized error response format:
41
42
```json
43
{
44
"error": {
45
"code": "VALIDATION_ERROR",
46
"message": "Input validation failed",
47
"statusCode": 422,
48
"requestId": "req_abc123xyz789",
49
"timestamp": "2025-01-15T10:30:00Z",
50
"details": [
51
{ "field": "email", "message": "Invalid email format", "code": "INVALID_EMAIL" }
52
]
53
}
54
}
55
```
56
57
Custom error class (Node.js):
58
59
```javascript
60
class ApiError extends Error {
61
constructor(code, message, statusCode = null, details = null) {
62
super(message);
63
this.code = code;
64
this.statusCode = statusCode || ERROR_CODES[code]?.status || 500;
65
this.details = details;
66
this.timestamp = new Date().toISOString();
67
}
68
}
69
70
// Usage
71
throw new ApiError("NOT_FOUND", "User not found", 404);
72
throw new ApiError("VALIDATION_ERROR", "Missing fields", 422, fieldErrors);
73
```
74
75
## Reference Guides
76
77
Detailed implementations in the `references/` directory:
78
79
| Guide | Contents |
80
|---|---|
81
| [Error Codes & Response Format](references/error-codes-reference.md) | Complete `ERROR_CODES` map, response formatter, global middleware (Node.js + Python) |
82
| [Retry Strategies & Circuit Breaker](references/retry-strategies.md) | Exponential backoff, jitter, circuit breaker pattern |
83
| [Monitoring & Tracking](references/monitoring-patterns.md) | Sentry integration, error rate metrics, `/metrics/errors` endpoint |
84
| [Validation Patterns](references/validation-examples.md) | Input validation, schema guards, detecting bad responses before errors occur |
85
86
## Best Practices
87
88
### ✅ DO
89
90
- Use a consistent error response format across all endpoints
91
- Include `requestId` and `traceId` in every error for observability
92
- Log 5xx errors at `ERROR` level; log 4xx at `WARN` level
93
- Provide actionable error messages — tell the client what to fix
94
- Use standard HTTP status codes (4xx client errors, 5xx server errors)
95
- Implement retry with exponential backoff for transient failures
96
- Use circuit breakers to prevent cascade failures
97
- Validate inputs early and return all field errors at once
98
- Monitor error rates and alert on anomalous spikes
99
100
### ❌ DON'T
101
102
- Expose stack traces or internal implementation details to clients
103
- Return HTTP 200 for error responses
104
- Silently swallow errors
105
- Log sensitive data (passwords, tokens, PII)
106
- Use vague messages like "Something went wrong"
107
- Mix error handling logic with business logic
108
- Retry non-idempotent operations or client errors (4xx)
109
- Return different error shapes from different endpoints
110

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…