P
💻DeveloppementIntermediateClaude

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

Adapt this prompt

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:

  1. Error class hierarchy: create a base AppError class and specialized classes (ValidationError, NotFoundError, UnauthorizedError, DatabaseError, ExternalServiceError) with standardized error codes.
  2. Error-handling middleware: centralized Express middleware that normalizes all errors, formats JSON responses and handles operational errors differently from bugs.
  3. Unhandled errors: handle process.on('uncaughtException') and process.on('unhandledRejection') with graceful shutdown.
  4. Structured logging: JSON format with correlation ID, stack traces in development only, and separate log levels.
  5. Monitoring integration: configure Sentry or equivalent with user context and release tracking.
  6. 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

Improving Node.js API robustnessSetting up error monitoringStandardizing error handling across a team

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 Optimizer

Comments

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateClaude

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.

1681
💻DeveloppementIntermediateClaude

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.

640
💻DeveloppementAdvancedAll AIs

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.

411
💻DeveloppementIntermediateAll AIs

Generate Storybook Stories for UI Components

Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.

474