Patterns for consistent error handling in backend applications
View on GitHubplugins/aai-dev-backend/skills/error-handling/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-dev-backend/skills/error-handling/SKILL.md -a claude-code --skill error-handlingInstallation paths:
.claude/skills/error-handling/# Error Handling Skill
Patterns for implementing robust error handling in backend applications.
## Error Class Hierarchy
```typescript
// Base application error
class AppError extends Error {
constructor(
public statusCode: number,
public message: string,
public code: string,
public isOperational = true,
public details?: Record<string, any>
) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
toJSON() {
return {
error: {
code: this.code,
message: this.message,
...(this.details && { details: this.details }),
},
};
}
}
// Specific error types
class ValidationError extends AppError {
constructor(details: Record<string, string[]>) {
super(400, 'Validation failed', 'VALIDATION_ERROR', true, details);
}
}
class NotFoundError extends AppError {
constructor(resource: string, id?: string) {
const message = id ? `${resource} with id '${id}' not found` : `${resource} not found`;
super(404, message, 'NOT_FOUND', true);
}
}
class UnauthorizedError extends AppError {
constructor(message = 'Authentication required') {
super(401, message, 'UNAUTHORIZED', true);
}
}
class ForbiddenError extends AppError {
constructor(message = 'Access denied') {
super(403, message, 'FORBIDDEN', true);
}
}
class ConflictError extends AppError {
constructor(message: string) {
super(409, message, 'CONFLICT', true);
}
}
class RateLimitError extends AppError {
constructor(retryAfter: number) {
super(429, 'Too many requests', 'RATE_LIMIT_EXCEEDED', true, { retryAfter });
}
}
class InternalError extends AppError {
constructor(message = 'Internal server error') {
super(500, message, 'INTERNAL_ERROR', false);
}
}
```
## Error Handler Middleware
```typescript
import { Request, Response, NextFunction } from 'express';
import { logger } from '../utils/logger';
interface ErrorResponse {
error: {
c