Download backend/src/middleware/error-handler.ts from qf46578/OmniCS: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/qf46578/OmniCS/resolve/8c21f94de855dc287d531bbf019bc54bc4b92594/backend/src/middleware/error-handler.ts
- Command line
-
hf download hf://qf46578/OmniCS@8c21f94de855dc287d531bbf019bc54bc4b92594/backend/src/middleware/error-handler.ts
-
curl -L -o error-handler.ts https://huggingface.co/qf46578/OmniCS/resolve/8c21f94de855dc287d531bbf019bc54bc4b92594/backend/src/middleware/error-handler.ts
2.35 kB
| import { Request, Response, NextFunction } from 'express'; | |
| import { createLogger } from '../utils/logger'; | |
| const logger = createLogger('error-handler'); | |
| export interface AppError extends Error { | |
| statusCode?: number; | |
| code?: string; | |
| isOperational?: boolean; | |
| details?: Record<string, unknown>; | |
| } | |
| export function createError( | |
| message: string, | |
| statusCode: number = 500, | |
| code: string = 'INTERNAL_ERROR', | |
| details?: Record<string, unknown> | |
| ): AppError { | |
| const error: AppError = new Error(message); | |
| error.statusCode = statusCode; | |
| error.code = code; | |
| error.isOperational = true; | |
| error.details = details; | |
| return error; | |
| } | |
| export function errorHandler( | |
| err: AppError, | |
| req: Request, | |
| res: Response, | |
| _next: NextFunction | |
| ): void { | |
| const statusCode = err.statusCode || 500; | |
| const code = err.code || 'INTERNAL_ERROR'; | |
| const message = err.isOperational ? err.message : '服务器内部错误'; | |
| // 记录错误日志 | |
| if (statusCode >= 500) { | |
| logger.error('Server Error:', { | |
| message: err.message, | |
| stack: err.stack, | |
| url: req.url, | |
| method: req.method, | |
| body: req.body, | |
| query: req.query, | |
| params: req.params, | |
| }); | |
| } else { | |
| logger.warn('Client Error:', { | |
| message: err.message, | |
| code, | |
| statusCode, | |
| }); | |
| } | |
| // 发送响应 | |
| res.status(statusCode).json({ | |
| code: statusCode, | |
| message, | |
| error: code, | |
| ...(process.env.NODE_ENV === 'development' && { | |
| stack: err.stack, | |
| details: err.details, | |
| }), | |
| }); | |
| } | |
| // 常见错误工厂函数 | |
| export const Errors = { | |
| BadRequest: (message: string = '请求参数错误', details?: Record<string, unknown>) => | |
| createError(message, 400, 'BAD_REQUEST', details), | |
| Unauthorized: (message: string = '未授权访问') => | |
| createError(message, 401, 'UNAUTHORIZED'), | |
| Forbidden: (message: string = '禁止访问') => | |
| createError(message, 403, 'FORBIDDEN'), | |
| NotFound: (message: string = '资源不存在') => | |
| createError(message, 404, 'NOT_FOUND'), | |
| Conflict: (message: string = '资源冲突') => | |
| createError(message, 409, 'CONFLICT'), | |
| TooManyRequests: (message: string = '请求过于频繁') => | |
| createError(message, 429, 'TOO_MANY_REQUESTS'), | |
| Internal: (message: string = '服务器内部错误') => | |
| createError(message, 500, 'INTERNAL_ERROR'), | |
| }; | |