Implement Robust Error Handling in Node.js
Create a professional Node.js error handling architecture with custom error classes, centralized middleware, and monitoring.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
Tu es un expert Node.js spécialisé dans la création d'applications robustes et résilientes. Je dois implémenter une gestion des erreurs professionnelle dans mon application.
Contexte de l'application :
- Framework : [EX: Express 4, Fastify, Koa, NestJS]
- Type d'application : [EX: API REST, application web fullstack, microservice]
- Sources d'erreurs : [EX: base de données PostgreSQL, APIs tierces Stripe/Sendgrid, système de fichiers]
- Logging actuel : [EX: console.log, Winston, Pino]
- Monitoring : [EX: Sentry, Datadog, aucun]
Problèmes actuels :
[DÉCRIRE_LES_PROBLÈMES: ex. erreurs non capturées qui crashent l'app, pas de distinction erreur métier/technique, logs insuffisants]
Implémente une stratégie de gestion des erreurs complète :
- Hiérarchie de classes d'erreurs : crée une classe AppError de base et des classes spécialisées (ValidationError, NotFoundError, UnauthorizedError, DatabaseError, ExternalServiceError) avec codes d'erreur standardisés.
- Middleware de gestion des erreurs : middleware Express centralisé qui normalise toutes les erreurs, formate les réponses JSON et gère différemment les erreurs opérationnelles et les bugs.
- Capture des erreurs non gérées : gestion des process.on('uncaughtException') et process.on('unhandledRejection') avec graceful shutdown.
- Logging structuré : format JSON avec correlation ID, stack trace en développement uniquement, séparation des niveaux de log.
- Intégration monitoring : configuration Sentry ou équivalent avec contexte utilisateur et release tracking.
- Tests : tests unitaires pour la hiérarchie d'erreurs et le middleware.
Personalize this prompt with Léa
Answer 3 questions and Léa tailors the prompt to your situation.
Why this prompt works
<p>This prompt intelligently distinguishes operational errors (predictable, manageable: user not found, validation failed) from programming errors (unexpected bugs: undefined is not a function). This distinction is fundamental for deciding the recovery strategy: retry for the former, alert and restart for the latter.</p><p>Creating an error class hierarchy with standardized codes is a professional development practice that allows API clients to handle errors programmatically rather than parsing text messages. This is the foundation of a robust API.</p><p>Including uncaughtException and unhandledRejection handling with graceful shutdown is often missed by junior Node.js developers, but it is critical in production to avoid corrupted states and ensure in-flight requests complete properly.</p>
Use Cases
Expected Output
Error class hierarchy, Express middleware, uncaught error handling, logging configuration, and monitoring integration.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Ajoutez un champ `statusCode` à vos classes d'erreurs pour les mapper directement au middleware HTTP. Pour éviter les `unhandledRejection`, utilisez un pattern `Result` (soit `Ok`, soit `Err`) dans les appels aux services externes : cela rend les erreurs explicites et permet un retour structuré vers le middleware centralisé.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Similar Prompts
Convert CSS to Tailwind
Migrate styling to Tailwind
Refactor legacy code to modern patterns
Gradually modernizing legacy code
Gemini Prompt for Generating SQL Queries
Gemini, Google's artificial intelligence model, excels at generating SQL queries through its deep understanding of data structures and database syntax. Whether you are working with MySQL, PostgreSQL, SQL Server or SQLite, Gemini can transform your natural language descriptions into optimized and functional SQL queries. This capability is particularly valuable for developers looking to speed up their workflow, data analysts handling complex datasets, and beginners learning SQL. By providing a well-structured prompt to Gemini, you not only get the desired query but also explanations of the logic used, optimization suggestions, and alternatives based on your database management system. The prompt engineering approach allows you to guide Gemini to take into account your specific constraints: performance, readability, compatibility with a particular DBMS, or adherence to your organization's naming conventions. Discover how to formulate your prompts to get the most out of Gemini in generating SQL queries.
Implement secrets management
Secure credentials management