The error object to be processed and formatted
Express request object containing request context
Express response object for sending the error response
Express next function (unused in error handlers)
Sends JSON error response to client and terminates request
// Primary usage in main Express application (src/app.ts)
import { errorHandler } from '@/shared/middleware';
const app = express();
// ... other middleware and routes
// Error handler must be the last middleware
app.use(errorHandler);
// The middleware automatically handles different error types:
//
// AppError -> Returns structured error with custom status and code
// ZodError -> Returns 400 with validation details
// Prisma errors -> Returns appropriate status based on error type
// Generic errors -> Returns 500 with sanitized message
// Error response format sent to clients:
// {
// "error": {
// "code": "VALIDATION_ERROR",
// "message": "Email is required",
// "details": { "field": "email", "message": "Required" },
// "timestamp": "2024-01-20T10:30:00.000Z",
// "path": "/api/v1/auth/register"
// }
// }
// Zod validation error response:
// {
// "error": {
// "code": "VALIDATION_ERROR",
// "message": "Validation failed",
// "details": [
// { "field": "email", "message": "Invalid email", "code": "invalid_string" },
// { "field": "password", "message": "String must contain at least 8 character(s)", "code": "too_small" }
// ],
// "timestamp": "2024-01-20T10:30:00.000Z",
// "path": "/api/v1/auth/register"
// }
// }
Global Error Handler Middleware for WayrApp Backend API
A comprehensive Express error handling middleware that provides centralized error processing for the entire WayrApp language learning platform backend. This middleware serves as the final error processing layer in the Express middleware stack, catching and standardizing all errors that occur during request processing across all API endpoints.
The error handler intelligently processes different types of errors including custom AppErrors, Zod validation errors, Prisma database errors, and generic JavaScript errors. It transforms these various error types into a consistent API response format that clients can reliably parse and handle. The middleware also provides comprehensive error logging with request context for debugging and monitoring purposes.
This middleware is essential for the application's distributed architecture evolution, as it ensures consistent error handling across all nodes and services. It's configured in the main Express application (src/app.ts) as the final middleware in the stack, ensuring all unhandled errors are properly caught and formatted before being sent to clients.
The error handler supports the application's security posture by sanitizing error messages in production environments and providing detailed error information only when appropriate. It integrates with the application's logging system to provide comprehensive error tracking and monitoring capabilities.
Error Type Handling:
Logging Features:
Security Considerations: