Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
aj-geddes avatar

Api Error Handling

  • 457 installs
  • 305 repo stars
  • Updated March 4, 2026
  • aj-geddes/useful-ai-prompts

api-error-handling is an agent skill that designs consistent REST and GraphQL error responses, retries, and monitoring patterns for developers building resilient public APIs with traceable failure contracts.

About

api-error-handling is a prompt skill in aj-geddes/useful-ai-prompts for implementing comprehensive API error systems across Node.js and Python services. It standardizes JSON error envelopes with code, message, statusCode, requestId, timestamp, and field-level details arrays for validation failures. Quick-start snippets define an ApiError class mapping ERROR_CODES to HTTP statuses and ship four reference guides covering error codes and middleware, exponential backoff with circuit breakers, Sentry monitoring with /metrics/errors endpoints, and input validation guards. Best practices distinguish logging 5xx at ERROR versus 4xx at WARN, mandate requestId traceability, and forbid exposing stack traces or returning HTTP 200 for failures. Developers reach for this skill when debugging production incidents, adding retry logic, or unifying error shapes across microservices. Use it during API design reviews where clients need actionable validation messages and operators need observable error-rate alerts without leaking secrets in logs.

  • HTTP status code conventions
  • Structured error payload schemas
  • Validation and domain error mapping
  • Idempotent retry guidance
  • Global exception middleware

Api Error Handling by the numbers

  • 457 all-time installs (skills.sh)
  • Ranked #939 of 4,347 Backend & APIs skills by installs in the Skillselion catalog
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill api-error-handling

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs457
repo stars305
Last updatedMarch 4, 2026
Repositoryaj-geddes/useful-ai-prompts

How do you standardize API error responses and retries?

Design consistent REST and GraphQL error responses, status codes, validation messages, retries, and client-safe failure contracts for public APIs.

Who is it for?

Backend developers designing public REST or GraphQL APIs who need uniform error envelopes, retry strategies, and observability patterns.

Skip if: Frontend-only teams with no API surface or error-contract ownership should skip api-error-handling.

When should I use this skill?

User asks to design API error handling, add requestId tracing, implement circuit breakers, or unify validation error responses.

What you get

Consistent error JSON schema, ApiError middleware, retry and circuit-breaker policies, and monitoring hooks with requestId tracing.

  • Standardized error response schema
  • ApiError middleware patterns
  • Retry and monitoring configuration snippets

By the numbers

  • Includes 4 reference guides in the references/ directory
  • Standard error JSON fields: code, message, statusCode, requestId, timestamp, details

Files

SKILL.mdMarkdownGitHub ↗

API Error Handling

Table of Contents

Overview

Build robust error handling systems with standardized error responses, detailed logging, error categorization, and user-friendly error messages. This skill covers the full lifecycle from throwing typed errors through logging, monitoring, and client-facing response formatting.

When to Use

  • Handling API errors consistently across endpoints
  • Debugging production issues with request tracing
  • Implementing error recovery strategies (retry, circuit breaker)
  • Monitoring and alerting on error rates
  • Providing meaningful, actionable error messages to clients
  • Validating request inputs before processing
  • Tracking error patterns over time

Quick Start

Minimal standardized error response format:

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Input validation failed",
    "statusCode": 422,
    "requestId": "req_abc123xyz789",
    "timestamp": "2025-01-15T10:30:00Z",
    "details": [
      { "field": "email", "message": "Invalid email format", "code": "INVALID_EMAIL" }
    ]
  }
}

Custom error class (Node.js):

class ApiError extends Error {
  constructor(code, message, statusCode = null, details = null) {
    super(message);
    this.code = code;
    this.statusCode = statusCode || ERROR_CODES[code]?.status || 500;
    this.details = details;
    this.timestamp = new Date().toISOString();
  }
}

// Usage
throw new ApiError("NOT_FOUND", "User not found", 404);
throw new ApiError("VALIDATION_ERROR", "Missing fields", 422, fieldErrors);

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Error Codes & Response FormatComplete ERROR_CODES map, response formatter, global middleware (Node.js + Python)
Retry Strategies & Circuit BreakerExponential backoff, jitter, circuit breaker pattern
Monitoring & TrackingSentry integration, error rate metrics, /metrics/errors endpoint
Validation PatternsInput validation, schema guards, detecting bad responses before errors occur

Best Practices

✅ DO

  • Use a consistent error response format across all endpoints
  • Include requestId and traceId in every error for observability
  • Log 5xx errors at ERROR level; log 4xx at WARN level
  • Provide actionable error messages — tell the client what to fix
  • Use standard HTTP status codes (4xx client errors, 5xx server errors)
  • Implement retry with exponential backoff for transient failures
  • Use circuit breakers to prevent cascade failures
  • Validate inputs early and return all field errors at once
  • Monitor error rates and alert on anomalous spikes

❌ DON'T

  • Expose stack traces or internal implementation details to clients
  • Return HTTP 200 for error responses
  • Silently swallow errors
  • Log sensitive data (passwords, tokens, PII)
  • Use vague messages like "Something went wrong"
  • Mix error handling logic with business logic
  • Retry non-idempotent operations or client errors (4xx)
  • Return different error shapes from different endpoints

Related skills

How it compares

Pick api-error-handling over generic backend prompts when the task is a full error lifecycle—from typed throws through logging, retries, and client-safe response formatting.

FAQ

What error JSON shape does api-error-handling recommend?

api-error-handling recommends a nested error object with code, message, statusCode, requestId, timestamp, and optional details arrays listing field-level validation codes such as INVALID_EMAIL for each failed input.

Which reference guides ship with api-error-handling?

api-error-handling bundles 4 references: error codes and response middleware, retry strategies with circuit breakers, monitoring and Sentry tracking, and validation examples for early input guards.

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.