123456789101112131415 |
- module.exports = (err, req, res, next) => {
- const statusCode = err.statusCode || 500;
- const isProduction = process.env.NODE_ENV === 'production';
-
- const response = {
- error: isProduction ? '服务器错误' : err.message,
- ...(!isProduction && { stack: err.stack })
- };
- // 记录完整错误日志
- console.error(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
- console.error(err);
- res.status(statusCode).json(response);
- };
|