mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-12-05 10:31:56 +00:00
feat:优化日志输出
This commit is contained in:
parent
7a5a9edde3
commit
c2194e6140
@ -26,21 +26,21 @@ export class AllExceptionsFilter implements ExceptionFilter {
|
|||||||
if (exception instanceof HttpException) {
|
if (exception instanceof HttpException) {
|
||||||
status = exception.getStatus();
|
status = exception.getStatus();
|
||||||
message = exception.message;
|
message = exception.message;
|
||||||
|
|
||||||
// 获取HttpException的详细信息
|
|
||||||
const exceptionResponse = exception.getResponse();
|
const exceptionResponse = exception.getResponse();
|
||||||
if (typeof exceptionResponse === 'object' && exceptionResponse !== null) {
|
if (typeof exceptionResponse === 'object' && exceptionResponse !== null) {
|
||||||
errorDetails = exceptionResponse;
|
errorDetails = exceptionResponse;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
// 处理非HttpException的错误
|
|
||||||
if (exception instanceof Error) {
|
if (exception instanceof Error) {
|
||||||
message = exception.message || '服务器内部错误';
|
message = exception.message || '服务器内部错误';
|
||||||
errorDetails = {
|
errorDetails = {
|
||||||
name: exception.name,
|
name: exception.name,
|
||||||
stack: process.env.NODE_ENV === 'development' ? exception.stack : undefined,
|
stack:
|
||||||
|
process.env.NODE_ENV === 'development'
|
||||||
|
? exception.stack
|
||||||
|
: undefined,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
message = '服务器内部错误';
|
message = '服务器内部错误';
|
||||||
@ -48,17 +48,16 @@ export class AllExceptionsFilter implements ExceptionFilter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录错误日志
|
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`异常捕获 - ${request.method} ${request.url} - ${status} - ${message}`,
|
`异常捕获 - ${request.method} ${request.url} - ${status} - ${message}`,
|
||||||
exception instanceof Error ? exception.stack : exception,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const errorResponse = {
|
const errorResponse = {
|
||||||
success: false,
|
success: false,
|
||||||
data: null,
|
data: null,
|
||||||
message,
|
message,
|
||||||
...(process.env.NODE_ENV === 'development' && errorDetails && { errorDetails }),
|
...(process.env.NODE_ENV === 'development' &&
|
||||||
|
errorDetails && { errorDetails }),
|
||||||
};
|
};
|
||||||
|
|
||||||
response.status(status).json(errorResponse);
|
response.status(status).json(errorResponse);
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
import { HttpException, HttpStatus, Logger } from '@nestjs/common';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误处理工具类
|
* 错误处理工具类
|
||||||
*/
|
*/
|
||||||
export class ErrorUtil {
|
export class ErrorUtil {
|
||||||
|
private static readonly logger = new Logger(ErrorUtil.name);
|
||||||
/**
|
/**
|
||||||
* 创建业务异常
|
* 创建业务异常
|
||||||
* @param message 错误消息
|
* @param message 错误消息
|
||||||
@ -30,7 +31,10 @@ export class ErrorUtil {
|
|||||||
* @param message 错误消息
|
* @param message 错误消息
|
||||||
* @param originalError 原始错误
|
* @param originalError 原始错误
|
||||||
*/
|
*/
|
||||||
static createInternalError(message: string, originalError?: any): HttpException {
|
static createInternalError(
|
||||||
|
message: string,
|
||||||
|
originalError?: any,
|
||||||
|
): HttpException {
|
||||||
return new HttpException(
|
return new HttpException(
|
||||||
{
|
{
|
||||||
message,
|
message,
|
||||||
@ -46,11 +50,14 @@ export class ErrorUtil {
|
|||||||
* @param resource 资源名称
|
* @param resource 资源名称
|
||||||
* @param identifier 资源标识符
|
* @param identifier 资源标识符
|
||||||
*/
|
*/
|
||||||
static createNotFoundError(resource: string, identifier?: string): HttpException {
|
static createNotFoundError(
|
||||||
const message = identifier
|
resource: string,
|
||||||
|
identifier?: string,
|
||||||
|
): HttpException {
|
||||||
|
const message = identifier
|
||||||
? `${resource} '${identifier}' 不存在`
|
? `${resource} '${identifier}' 不存在`
|
||||||
: `${resource} 不存在`;
|
: `${resource} 不存在`;
|
||||||
|
|
||||||
return this.createBusinessError(message, HttpStatus.NOT_FOUND);
|
return this.createBusinessError(message, HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,14 +81,26 @@ export class ErrorUtil {
|
|||||||
* 处理未知错误并转换为HttpException
|
* 处理未知错误并转换为HttpException
|
||||||
* @param error 未知错误
|
* @param error 未知错误
|
||||||
* @param defaultMessage 默认错误消息
|
* @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) {
|
if (error instanceof HttpException) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error instanceof 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)}`);
|
return this.createInternalError(`${defaultMessage}: ${String(error)}`);
|
||||||
|
|||||||
@ -29,8 +29,7 @@ export class CdnController {
|
|||||||
|
|
||||||
this.logger.log(`成功投递文件: ${filePath}`);
|
this.logger.log(`成功投递文件: ${filePath}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('晶灵数据请求处理失败:', error);
|
throw ErrorUtil.handleUnknownError(error, 'CDN处理文件请求失败', 'deliverFile');
|
||||||
throw ErrorUtil.handleUnknownError(error, 'CDN处理文件请求失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -177,8 +177,7 @@ export class MemeController {
|
|||||||
stream.pipe(throttle).pipe(res);
|
stream.pipe(throttle).pipe(res);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`获取表情包失败:${e.message}`);
|
throw ErrorUtil.handleUnknownError(e, '获取表情包失败', 'handleMemeRequest');
|
||||||
throw ErrorUtil.handleUnknownError(e, '获取表情包失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,8 +270,7 @@ export class MemeController {
|
|||||||
this.logger.log(`表情包上传成功: ${remoteFilePath}`);
|
this.logger.log(`表情包上传成功: ${remoteFilePath}`);
|
||||||
return '表情上传成功..';
|
return '表情上传成功..';
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('表情包上传失败:', error);
|
throw ErrorUtil.handleUnknownError(error, '上传失败', 'uploadMeme');
|
||||||
throw ErrorUtil.handleUnknownError(error, '上传失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -73,8 +73,7 @@ export class WordsController {
|
|||||||
|
|
||||||
return text;
|
return text;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`getText 失败: ${e}`);
|
throw ErrorUtil.handleUnknownError(e, '获取文案失败', 'getText');
|
||||||
throw ErrorUtil.handleUnknownError(e, '获取文案失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,8 +109,7 @@ export class WordsController {
|
|||||||
throw ErrorUtil.createBusinessError('重载失败', HttpStatus.BAD_REQUEST);
|
throw ErrorUtil.createBusinessError('重载失败', HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`reloadWord 失败: ${e}`);
|
throw ErrorUtil.handleUnknownError(e, '重载文案失败', 'reloadWord');
|
||||||
throw ErrorUtil.handleUnknownError(e, '重载文案失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,8 +132,7 @@ export class WordsController {
|
|||||||
}
|
}
|
||||||
return names;
|
return names;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`listWords 失败: ${e}`);
|
throw ErrorUtil.handleUnknownError(e, '获取文案列表失败', 'listWords');
|
||||||
throw ErrorUtil.handleUnknownError(e, '获取文案列表失败');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user