hm-nodejs-error-handling

Implements centralized Fastify error handling with custom error classes and consistent response shapes.

Updated Apr 26, 2026
One-click install
npx skills add https://github.com/ArkhiMuttaqina/publisher-for-campuss --skill hm-nodejs-error-handling-arkhimuttaqina
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: hm-nodejs-error-handling
Source: https://github.com/ArkhiMuttaqina/publisher-for-campuss/tree/main/skills/hm-nodejs-error-handling
Command: npx skills add https://github.com/ArkhiMuttaqina/publisher-for-campuss --skill hm-nodejs-error-handling-arkhimuttaqina

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Node.js APIs often end up with scattered try/catch blocks, inconsistent error responses, and leaked stack traces in production. This Skill provides a complete pattern for centralizing error handling in Fastify so services throw domain errors and one handler maps them to consistent HTTP responses. ## Core Features & Use Cases - Custom Error Class Hierarchy: Build an AppError base class with statusCode, code, and isOperational flags, plus subclasses like NotFoundError, ValidationError, UnauthorizedError, ForbiddenError, and ConflictError. - Centralized Fastify Error Handler: Configure setErrorHandler and setNotFoundHandler to map Zod validation errors, Prisma error codes (P2002, P2025), and unknown errors to proper HTTP responses. - Consistent Error Response Shape: Return RFC 7807-inspired responses with machine-readable code, human-readable message, and optional field-level details, while hiding stack traces in production. - Use Case: When building a Fastify + Prisma + Zod API, use this Skill to set up the full error layer so a duplicate database record automatically returns a 409 Conflict and a failed Zod parse returns a 400 with per-field messages. ## Quick Start Set up centralized error handling in my Fastify app with custom error classes, a global setErrorHandler, and consistent error response shapes.

Frequently Asked Questions about hm-nodejs-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up global error handling in Fastify?▼

Use Fastify's setErrorHandler to register a single function that receives err, request, and reply, then map error types to HTTP responses. Register it after all route plugins, and pair it with setNotFoundHandler for unmatched routes.

How to create custom error classes in Node.js?▼

Extend a base AppError class that carries statusCode, code, and isOperational fields, then create subclasses like NotFoundError or ValidationError. Use Object.setPrototypeOf and Error.captureStackTrace so instanceof checks and stack traces work correctly.

How do I format Zod validation errors in an API response?▼

Throw a ValidationError containing the ZodIssue array, then map each issue in the error handler to a field and message pair using issue.path.join and issue.message. Return a 400 response with a details array under a consistent error shape.

How do I handle Prisma errors like P2002 in Fastify?▼

Catch PrismaClientKnownRequestError in the central error handler and switch on err.code. Map P2002 unique constraint violations to 409 Conflict and P2025 record-not-found to 404, defaulting other codes to a generic 500 database error.

Should I use try/catch in every Fastify route handler?▼

No, throw domain errors from services and let setErrorHandler catch them centrally, since Fastify routes handle async errors automatically. Per-route try/catch leads to duplicated logic and inconsistent response shapes.

How do I avoid leaking stack traces in production APIs?▼

Check env.NODE_ENV before including err.stack in the response, and send a generic message like 'An unexpected error occurred' in production. Always log the full error with stack server-side for debugging.