Implement Robust Error Handling in Node.js
Set up Node.js error handling with custom error classes, centralized middleware, structured logs and monitoring.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are a Node.js expert specializing in robust and resilient applications. I need to implement professional error handling in my application.
Application context:
- Framework: [e.g., Express 4, Fastify, Koa, NestJS]
- Application type: [e.g., REST API, full-stack web application, microservice]
- Error sources: [e.g., PostgreSQL database, third-party Stripe/SendGrid APIs, file system]
- Current logging: [e.g., console.log, Winston, Pino]
- Monitoring: [e.g., Sentry, Datadog, none]
Current problems:
[Describe the problems, e.g., uncaught errors that crash the app, no distinction between business and technical errors, insufficient logs]
Implement a complete error-handling strategy:
- Error class hierarchy: create a base AppError class and specialized classes (ValidationError, NotFoundError, UnauthorizedError, DatabaseError, ExternalServiceError) with standardized error codes.
- Error-handling middleware: centralized Express middleware that normalizes all errors, formats JSON responses and handles operational errors differently from bugs.
- Unhandled errors: handle process.on('uncaughtException') and process.on('unhandledRejection') with graceful shutdown.
- Structured logging: JSON format with correlation ID, stack traces in development only, and separate log levels.
- Monitoring integration: configure Sentry or equivalent with user context and release tracking.
- Tests: unit tests for the error hierarchy and middleware.
Personalize this prompt with Léa · AI assistant
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
The prompt distinguishes expected failures such as a missing user or invalid input from programming errors such as "undefined is not a function." The response depends on what failed. Return an appropriate error for invalid input and retry only when a failure may be temporary and the operation can be safely repeated. Error codes let API clients respond without parsing message text. The prompt also asks for shutdown handlers for uncaught exceptions and unhandled rejections.
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
Be the first to comment on this prompt.
Glossary Terms
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Improve this prompt with the Optimizer
Analyze the prompt, strengthen the context and get a more precise version.
Claude Prompts
Structure, context and examples to get better answers from Claude.
AI prompts for web developers
Code review and debugging, keeping API keys and secrets out of the prompt.
Security, GDPR & Business Plans
ChatGPT Business, Claude Team, Gemini Workspace and Mistral Vibe compared from vendor sources: training, EU hosting and a GDPR checklist.
Similar Prompts
Task brief for Claude Code: frame a feature end to end
Brief template to delegate a complete feature to Claude Code: goal, starting point, constraints, definition of done, working rules and recap format.
Write a project's CLAUDE.md: the brief Claude Code reads at every session
Turns your project description into a dense CLAUDE.md: commands, structure, conventions, pitfalls, off-limits areas and situations where the agent must ask before acting.
Design a Secure Webhook API
Design a webhook API with event subscriptions, signed deliveries, retry limits, and a delivery log your support team can inspect.
Generate Storybook Stories for UI Components
Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.