Implement standardized API error handling
Generates production-ready error handling middleware with custom error classes and logging.
/plugin marketplace add jeremylongshore/claude-code-plugins-plus/plugin install api-error-handler@claude-code-plugins-plusCreate standardized, production-ready error handling middleware with proper HTTP status codes, consistent error formats, and comprehensive logging. This command generates custom error classes, middleware, and error recovery strategies for Node.js, Python, and other backend frameworks.
Why standardized error handling matters:
Alternatives considered:
This approach balances: Developer experience, security, debugging, and API consistency.
Use this command when:
Don't use when:
Generate Custom Error Classes
Build Error Middleware
Configure Environment-Specific Behavior
Integrate Logging
Add Error Recovery
// errors/AppError.js
class AppError extends Error {
constructor(message, statusCode, errorCode = null) {
super(message);
this.statusCode = statusCode;
this.errorCode = errorCode;
this.isOperational = true;
this.timestamp = new Date().toISOString();
Error.captureStackTrace(this, this.constructor);
}
}
class ValidationError extends AppError {
constructor(message, errors = []) {
super(message, 400, 'VALIDATION_ERROR');
this.errors = errors;
}
}
class NotFoundError extends AppError {
constructor(resource) {
super(`${resource} not found`, 404, 'NOT_FOUND');
this.resource = resource;
}
}
class UnauthorizedError extends AppError {
constructor(message = 'Unauthorized') {
super(message, 401, 'UNAUTHORIZED');
}
}
class ForbiddenError extends AppError {
constructor(message = 'Forbidden') {
super(message, 403, 'FORBIDDEN');
}
}
module.exports = { AppError, ValidationError, NotFoundError, UnauthorizedError, ForbiddenError };
// middleware/errorHandler.js
const logger = require('../utils/logger');
const errorHandler = (err, req, res, next) => {
// Default to 500 server error
let statusCode = err.statusCode || 500;
let message = err.message || 'Internal Server Error';
// Log error with context
logger.error({
message: err.message,
statusCode,
errorCode: err.errorCode,
stack: err.stack,
path: req.path,
method: req.method,
requestId: req.id,
userId: req.user?.id,
ip: req.ip
});
// Production: Don't leak error details
if (process.env.NODE_ENV === 'production' && !err.isOperational) {
message = 'An unexpected error occurred';
statusCode = 500;
}
// Send error response
res.status(statusCode).json({
error: {
message,
code: err.errorCode,
statusCode,
timestamp: err.timestamp || new Date().toISOString(),
path: req.path,
...(process.env.NODE_ENV === 'development' && { stack: err.stack }),
...(err.errors && { details: err.errors })
}
});
};
module.exports = errorHandler;
# errors/exceptions.py
from typing import Optional, Any, Dict
from fastapi import HTTPException
from datetime import datetime
class AppError(HTTPException):
def __init__(
self,
status_code: int,
message: str,
error_code: Optional[str] = None,
details: Optional[Dict[str, Any]] = None
):
self.status_code = status_code
self.message = message
self.error_code = error_code
self.details = details or {}
self.timestamp = datetime.utcnow().isoformat()
super().__init__(status_code=status_code, detail=message)
class ValidationError(AppError):
def __init__(self, message: str, errors: list = None):
super().__init__(
status_code=400,
message=message,
error_code="VALIDATION_ERROR",
details={"errors": errors or []}
)
class NotFoundError(AppError):
def __init__(self, resource: str):
super().__init__(
status_code=404,
message=f"{resource} not found",
error_code="NOT_FOUND",
details={"resource": resource}
)
class UnauthorizedError(AppError):
def __init__(self, message: str = "Unauthorized"):
super().__init__(
status_code=401,
message=message,
error_code="UNAUTHORIZED"
)
# main.py
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import logging
app = FastAPI()
logger = logging.getLogger(__name__)
@app.exception_handler(AppError)
async def app_error_handler(request: Request, exc: AppError):
logger.error(
f"AppError: {exc.message}",
extra={
"status_code": exc.status_code,
"error_code": exc.error_code,
"path": request.url.path,
"method": request.method
}
)
return JSONResponse(
status_code=exc.status_code,
content={
"error": {
"message": exc.message,
"code": exc.error_code,
"statusCode": exc.status_code,
"timestamp": exc.timestamp,
"path": str(request.url.path),
**exc.details
}
}
)
@app.exception_handler(Exception)
async def generic_error_handler(request: Request, exc: Exception):
logger.exception("Unhandled exception", exc_info=exc)
return JSONResponse(
status_code=500,
content={
"error": {
"message": "Internal server error",
"code": "INTERNAL_ERROR",
"statusCode": 500,
"timestamp": datetime.utcnow().isoformat()
}
}
)
const { ValidationError } = require('./errors/AppError');
const { body, validationResult } = require('express-validator');
router.post('/users',
[
body('email').isEmail().normalizeEmail(),
body('password').isLength({ min: 8 }),
body('name').trim().notEmpty()
],
async (req, res, next) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
throw new ValidationError('Validation failed', errors.array());
}
const user = await createUser(req.body);
res.status(201).json(user);
} catch (error) {
next(error);
}
}
);
// Response for invalid input:
// {
// "error": {
// "message": "Validation failed",
// "code": "VALIDATION_ERROR",
// "statusCode": 400,
// "timestamp": "2025-10-11T12:00:00.000Z",
// "path": "/users",
// "details": [
// { "field": "email", "message": "Invalid email format" },
// { "field": "password", "message": "Password must be at least 8 characters" }
// ]
// }
// }
from fastapi import APIRouter
from errors.exceptions import NotFoundError
router = APIRouter()
@router.get("/users/{user_id}")
async def get_user(user_id: int):
user = await db.get_user(user_id)
if not user:
raise NotFoundError("User")
return user
# Response:
# {
# "error": {
# "message": "User not found",
# "code": "NOT_FOUND",
# "statusCode": 404,
# "timestamp": "2025-10-11T12:00:00.000Z",
# "path": "/users/123",
# "details": {
# "resource": "User"
# }
# }
# }
const { AppError } = require('./errors/AppError');
const retry = require('async-retry');
async function callExternalAPI(endpoint) {
return retry(async (bail, attempt) => {
try {
const response = await fetch(endpoint);
if (!response.ok) {
// Don't retry client errors
if (response.status >= 400 && response.status < 500) {
bail(new AppError('External API error', response.status));
return;
}
// Retry server errors
throw new Error(`API returned ${response.status}`);
}
return response.json();
} catch (error) {
console.log(`Attempt ${attempt} failed: ${error.message}`);
throw error;
}
}, {
retries: 3,
minTimeout: 1000,
maxTimeout: 5000
});
}
Common issues and solutions:
Problem: Errors logged multiple times
Problem: Stack traces visible in production
NODE_ENV=production is set, check conditional logicProblem: Lost error context (request ID, user info)
Problem: Async errors not caught
Problem: Database connection errors crashing app
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection', { reason, promise });
// Optionally exit: process.exit(1);
});
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception', { error });
process.exit(1); // Must exit, app is in undefined state
});
const errorHandlerOptions = {
// Include stack traces
showStack: process.env.NODE_ENV === 'development',
// Log level for different error types
logLevel: {
operational: 'error',
programmer: 'critical'
},
// Send errors to monitoring service
reportToMonitoring: process.env.NODE_ENV === 'production',
// Sanitize sensitive fields
sanitizeFields: ['password', 'ssn', 'creditCard'],
// Custom error formatters
formatters: {
json: (err) => ({ error: err.toJSON() }),
xml: (err) => convertToXML(err)
}
};
DO:
DON'T:
TIPS:
INSUFFICIENT_FUNDS, RATE_LIMIT_EXCEEDED)/validate-api-responses - Validate API responses match schemas/setup-logging - Configure structured logging for errors/scan-api-security - Scan for security vulnerabilities in error handling/create-monitoring - Set up error monitoring dashboards/generate-rest-api - Generate REST API with built-in error handlingOptimization strategies:
// Disable stack traces in production for performance
if (process.env.NODE_ENV === 'production') {
Error.stackTraceLimit = 0;
}
// Use structured logging with async writes
const logger = winston.createLogger({
transports: [
new winston.transports.File({
filename: 'error.log',
level: 'error'
})
]
});
Security checklist:
// BAD: Exposes internal structure
throw new Error(`User ${userId} not found in users table`);
// GOOD: Generic message
throw new NotFoundError('User');
// BAD: Reveals existence
if (!user) throw new Error('User not found');
if (password !== user.password) throw new Error('Wrong password');
// GOOD: Generic auth failure
if (!user || password !== user.password) {
throw new UnauthorizedError('Invalid credentials');
}
Error handler not catching errors:
next(error) or use express-async-errorsErrors not logged:
Production errors too verbose:
Error monitoring not working: