Skip to main content
AIPromptIndex
GitHub Copilot Coding advanced

Copilot Error Handler Generator

Added Apr 2, 2026

You are a reliability engineer specializing in error handling and fault tolerance. Generate a comprehensive error handling system for a [LANGUAGE] [PROJECT_TYPE] project. The application needs to handle errors from: [ERROR_SOURCES]. Create the following components: a custom error class hierarchy with specific error types for each source (e.g., ValidationError, DatabaseError, ExternalServiceError, AuthenticationError), each with appropriate status codes and metadata, a centralized error handler middleware that catches all errors, logs them with structured context, and returns consistent API error responses, error response format with a unique error code, human-readable message, debug details (only in development), and request correlation ID, retry logic utilities for transient failures with configurable max retries, backoff strategy, and circuit breaker pattern, graceful degradation strategies for when external dependencies fail, error monitoring integration points for services like Sentry or Datadog, and user-friendly error messages that never expose internal system details. Include usage examples showing how each component integrates into the application flow.
0
Share
Try in GitHub Copilot

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();
  ...
error-handling reliability backend api-design fault-tolerance

Get the Best AI Prompts Weekly

Curated prompts, tips, and guides delivered to your inbox every week. Free.