- Added api-security-hardening (helmet, rate limits) - Added nodejs-backend-patterns (error handling) - Added observability-monitoring (pino logging) - Added websocket-engineer (socket.io real-time updates) - Added docker (Multi-stage build, compose) - Added vitest (testing configuration and store tests) - Added data-visualizer (MetricsBar and HostChart) - Added infrastructure-monitoring/proxmox-admin/network-engineer types - Fixed UI accessibility and styling - Cleaned up node_modules tracking
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import { AppError, ValidationError } from '../errors';
|
|
import { logger } from './requestLogger';
|
|
|
|
/**
|
|
* Global error handler middleware.
|
|
* Catches all errors, logs unexpected ones with Pino, and returns safe JSON responses.
|
|
* Must be registered AFTER all routes.
|
|
*/
|
|
export const errorHandler = (
|
|
err: Error,
|
|
req: Request,
|
|
res: Response,
|
|
_next: NextFunction
|
|
) => {
|
|
if (err instanceof AppError) {
|
|
return res.status(err.statusCode).json({
|
|
status: 'error',
|
|
message: err.message,
|
|
...(err instanceof ValidationError && err.errors ? { errors: err.errors } : {}),
|
|
});
|
|
}
|
|
|
|
// Log unexpected errors
|
|
logger.error({
|
|
error: err.message,
|
|
stack: err.stack,
|
|
url: req.url,
|
|
method: req.method,
|
|
});
|
|
|
|
// Don't leak error details in production
|
|
const message =
|
|
process.env.NODE_ENV === 'production'
|
|
? 'Internal server error'
|
|
: err.message;
|
|
|
|
res.status(500).json({
|
|
status: 'error',
|
|
message,
|
|
});
|
|
};
|