diff --git a/src/common/filters/all-exception.filter.ts b/src/common/filters/all-exception.filter.ts index c5aea0a..c4b728d 100644 --- a/src/common/filters/all-exception.filter.ts +++ b/src/common/filters/all-exception.filter.ts @@ -26,21 +26,21 @@ export class AllExceptionsFilter implements ExceptionFilter { if (exception instanceof HttpException) { status = exception.getStatus(); message = exception.message; - - // 获取HttpException的详细信息 + const exceptionResponse = exception.getResponse(); if (typeof exceptionResponse === 'object' && exceptionResponse !== null) { errorDetails = exceptionResponse; } } else { status = HttpStatus.INTERNAL_SERVER_ERROR; - - // 处理非HttpException的错误 if (exception instanceof Error) { message = exception.message || '服务器内部错误'; errorDetails = { name: exception.name, - stack: process.env.NODE_ENV === 'development' ? exception.stack : undefined, + stack: + process.env.NODE_ENV === 'development' + ? exception.stack + : undefined, }; } else { message = '服务器内部错误'; @@ -48,17 +48,16 @@ export class AllExceptionsFilter implements ExceptionFilter { } } - // 记录错误日志 this.logger.error( `异常捕获 - ${request.method} ${request.url} - ${status} - ${message}`, - exception instanceof Error ? exception.stack : exception, ); const errorResponse = { success: false, data: null, message, - ...(process.env.NODE_ENV === 'development' && errorDetails && { errorDetails }), + ...(process.env.NODE_ENV === 'development' && + errorDetails && { errorDetails }), }; response.status(status).json(errorResponse); diff --git a/src/common/utils/error.util.ts b/src/common/utils/error.util.ts index 6bdbde1..facf8cc 100644 --- a/src/common/utils/error.util.ts +++ b/src/common/utils/error.util.ts @@ -1,9 +1,10 @@ -import { HttpException, HttpStatus } from '@nestjs/common'; +import { HttpException, HttpStatus, Logger } from '@nestjs/common'; /** * 错误处理工具类 */ export class ErrorUtil { + private static readonly logger = new Logger(ErrorUtil.name); /** * 创建业务异常 * @param message 错误消息 @@ -30,7 +31,10 @@ export class ErrorUtil { * @param message 错误消息 * @param originalError 原始错误 */ - static createInternalError(message: string, originalError?: any): HttpException { + static createInternalError( + message: string, + originalError?: any, + ): HttpException { return new HttpException( { message, @@ -46,11 +50,14 @@ export class ErrorUtil { * @param resource 资源名称 * @param identifier 资源标识符 */ - static createNotFoundError(resource: string, identifier?: string): HttpException { - const message = identifier + static createNotFoundError( + resource: string, + identifier?: string, + ): HttpException { + const message = identifier ? `${resource} '${identifier}' 不存在` : `${resource} 不存在`; - + return this.createBusinessError(message, HttpStatus.NOT_FOUND); } @@ -74,14 +81,26 @@ export class ErrorUtil { * 处理未知错误并转换为HttpException * @param error 未知错误 * @param defaultMessage 默认错误消息 + * @param context 错误上下文(可选) */ - static handleUnknownError(error: any, defaultMessage: string = '服务器内部错误'): HttpException { + static handleUnknownError( + error: any, + defaultMessage: string = '服务器内部错误', + context?: string, + ): HttpException { + const errorMsg = error?.message || String(error); + const logMessage = context ? `${context}: ${errorMsg}` : errorMsg; + this.logger.error(logMessage); + if (error instanceof HttpException) { return error; } if (error instanceof Error) { - return this.createInternalError(`${defaultMessage}: ${error.message}`, error); + return this.createInternalError( + `${defaultMessage}: ${error.message}`, + error, + ); } return this.createInternalError(`${defaultMessage}: ${String(error)}`); diff --git a/src/modules/cdn/cdn.controller.ts b/src/modules/cdn/cdn.controller.ts index 81cf7f2..661a7ab 100644 --- a/src/modules/cdn/cdn.controller.ts +++ b/src/modules/cdn/cdn.controller.ts @@ -29,8 +29,7 @@ export class CdnController { this.logger.log(`成功投递文件: ${filePath}`); } catch (error) { - this.logger.error('晶灵数据请求处理失败:', error); - throw ErrorUtil.handleUnknownError(error, 'CDN处理文件请求失败'); + throw ErrorUtil.handleUnknownError(error, 'CDN处理文件请求失败', 'deliverFile'); } } diff --git a/src/modules/meme/meme.controller.ts b/src/modules/meme/meme.controller.ts index f329af4..cf3e726 100644 --- a/src/modules/meme/meme.controller.ts +++ b/src/modules/meme/meme.controller.ts @@ -177,8 +177,7 @@ export class MemeController { stream.pipe(throttle).pipe(res); } } catch (e) { - this.logger.error(`获取表情包失败:${e.message}`); - throw ErrorUtil.handleUnknownError(e, '获取表情包失败'); + throw ErrorUtil.handleUnknownError(e, '获取表情包失败', 'handleMemeRequest'); } } @@ -271,8 +270,7 @@ export class MemeController { this.logger.log(`表情包上传成功: ${remoteFilePath}`); return '表情上传成功..'; } catch (error) { - this.logger.error('表情包上传失败:', error); - throw ErrorUtil.handleUnknownError(error, '上传失败'); + throw ErrorUtil.handleUnknownError(error, '上传失败', 'uploadMeme'); } } } diff --git a/src/modules/words/words.controller.ts b/src/modules/words/words.controller.ts index c6b4027..42eaf55 100644 --- a/src/modules/words/words.controller.ts +++ b/src/modules/words/words.controller.ts @@ -73,8 +73,7 @@ export class WordsController { return text; } catch (e) { - this.logger.error(`getText 失败: ${e}`); - throw ErrorUtil.handleUnknownError(e, '获取文案失败'); + throw ErrorUtil.handleUnknownError(e, '获取文案失败', 'getText'); } } @@ -110,8 +109,7 @@ export class WordsController { throw ErrorUtil.createBusinessError('重载失败', HttpStatus.BAD_REQUEST); } } catch (e) { - this.logger.error(`reloadWord 失败: ${e}`); - throw ErrorUtil.handleUnknownError(e, '重载文案失败'); + throw ErrorUtil.handleUnknownError(e, '重载文案失败', 'reloadWord'); } } @@ -134,8 +132,7 @@ export class WordsController { } return names; } catch (e) { - this.logger.error(`listWords 失败: ${e}`); - throw ErrorUtil.handleUnknownError(e, '获取文案列表失败'); + throw ErrorUtil.handleUnknownError(e, '获取文案列表失败', 'listWords'); } } }