Copilot Error Handler Generator
Added Apr 2, 2026
About This Prompt
This prompt generates a production-grade error handling system that goes far beyond simple try-catch blocks. It creates a structured error hierarchy, centralized handling middleware, retry utilities with circuit breaker patterns, and graceful degradation strategies. The system ensures consistent error responses across your entire API, proper logging for debugging, and user-safe messages that never leak internal details. This is critical infrastructure that many projects implement poorly or inconsistently, leading to confusing error messages, silent failures, and difficult debugging. The output gives you a complete error handling architecture ready for production.
Variables to Customize
[LANGUAGE]
Programming language
Example: TypeScript
[PROJECT_TYPE]
Type of project
Example: Express.js REST API
[ERROR_SOURCES]
Where errors can originate in the system
Example: input validation, PostgreSQL database, Stripe payment API, Redis cache, JWT authentication
Tips for Best Results
- Implement the error hierarchy first and update existing code to throw specific error types gradually
- Configure the circuit breaker thresholds based on your actual dependency SLA requirements
- Never expose stack traces or internal error details to end users in production
Example Output
// errors/base.ts
export abstract class AppError extends Error {
abstract readonly statusCode: number;
abstract readonly errorCode: string;
abstract readonly isOperational: boolean;
constructor(message: string, public readonly context?: Record<string, unknown>) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
// errors/validation.ts
export class ValidationError extends AppError {
readonly statusCode = 400;
readonly errorCode = 'VALIDATION_ERROR';
readonly isOperational = true;
constructor(public readonly fields: FieldError[], context?: Record<string, unknown>) {
super('Input validation failed', context);
}
}
// middleware/error-handler.ts
export function errorHandler(err: Error, req: Request, res: Response, next: NextFunction) {
const correlationId = req.headers['x-correlation-id'] || uuid();
...