From 4a5fada435e12e375e38394ed8e9df8946de9fa4 Mon Sep 17 00:00:00 2001 From: chfei Date: Fri, 23 Oct 2026 09:52:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=20?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E4=BF=A1=E6=81=AF=E7=BB=9F=E4=B8=80=E6=8B=A6?= =?UTF-8?q?=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commons/GlobalExceptionFilter.cs | 16 +++++++++---- Core/MAX.Framework/Dtos/ApiResult.cs | 10 +++++--- .../Controllers/ImageCacheController.cs | 23 +++++++------------ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Core/MAX.Framework/Commons/GlobalExceptionFilter.cs b/Core/MAX.Framework/Commons/GlobalExceptionFilter.cs index 465a872..85a3679 100644 --- a/Core/MAX.Framework/Commons/GlobalExceptionFilter.cs +++ b/Core/MAX.Framework/Commons/GlobalExceptionFilter.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -12,10 +13,12 @@ namespace MAX.Framework.Commons public class GlobalExceptionFilter : IExceptionFilter { private readonly ILogger _logger; + private readonly IHostEnvironment _environment; - public GlobalExceptionFilter(ILogger logger) + public GlobalExceptionFilter(ILogger logger, IHostEnvironment environment) { _logger = logger; + _environment = environment; } public void OnException(ExceptionContext context) @@ -24,10 +27,13 @@ namespace MAX.Framework.Commons var result = context.Exception switch { - AppException be => ApiResult.Fail(be.Message, be.Code), - ValidationException ve => ApiResult.Fail(ve.Message, ResultCode.ValidationError), - UnauthorizedAccessException => ApiResult.Fail("未授权访问", ResultCode.Unauthorized), - _ => ApiResult.Fail("服务器内部错误", ResultCode.ServerError) + AppException be => ApiResult.Fail(be.Message, be.Code, traceId), + ArgumentException arge => ApiResult.Fail(arge.Message, ResultCode.ValidationError, traceId), + ValidationException ve => ApiResult.Fail(ve.Message, ResultCode.ValidationError, traceId), + UnauthorizedAccessException => ApiResult.Fail("未授权访问", ResultCode.Unauthorized, traceId), + _ => ApiResult.Fail( + _environment.IsDevelopment() ? context.Exception.Message : "服务器内部错误", + ResultCode.ServerError, traceId) }; _logger.LogError(context.Exception, "请求异常 TraceId: {TraceId}", traceId); diff --git a/Core/MAX.Framework/Dtos/ApiResult.cs b/Core/MAX.Framework/Dtos/ApiResult.cs index 2df8c1e..102b4f0 100644 --- a/Core/MAX.Framework/Dtos/ApiResult.cs +++ b/Core/MAX.Framework/Dtos/ApiResult.cs @@ -34,6 +34,10 @@ namespace MAX.Framework.Dtos /// 提示信息 public string? Msg { get; init; } + /// 失败请求的跟踪标识,用于关联服务器日志。 + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? TraceId { get; init; } + /// 请求是否成功 public bool Success => Code == ResultCode.Success; @@ -52,7 +56,7 @@ namespace MAX.Framework.Dtos public static implicit operator ApiResult(ApiResult result) { ArgumentNullException.ThrowIfNull(result); - return new ApiResult { Code = result.Code, Msg = result.Msg, Data = default }; + return new ApiResult { Code = result.Code, Msg = result.Msg, TraceId = result.TraceId, Data = default }; } public static ApiResult Ok(T data, string msg = "操作成功") @@ -85,10 +89,10 @@ namespace MAX.Framework.Dtos public static ApiResult Ok() => new ApiResult { Code = ResultCode.Success, Msg = "操作成功" }; - public static ApiResult Fail(string msg = "操作失败", int code = ResultCode.Fail) + public static ApiResult Fail(string msg = "操作失败", int code = ResultCode.Fail, string? traceId = null) { ValidateFailureCode(code); - return new ApiResult { Code = code, Msg = msg }; + return new ApiResult { Code = code, Msg = msg, TraceId = traceId }; } internal static void ValidateFailureCode(int code) diff --git a/Plugins/Store.ImageCache/Controllers/ImageCacheController.cs b/Plugins/Store.ImageCache/Controllers/ImageCacheController.cs index a147feb..7245714 100644 --- a/Plugins/Store.ImageCache/Controllers/ImageCacheController.cs +++ b/Plugins/Store.ImageCache/Controllers/ImageCacheController.cs @@ -36,21 +36,14 @@ namespace KJD_Web.Controllers /// 批量按 MD5 查询图片缓存。 /// [HttpPost] - public ApiResult> GetImageCache(ImageCacheBatchQueryRequest request) + public List GetImageCache(ImageCacheBatchQueryRequest request) { List data = new List(); - try + if (request != null && request.Md5s != null) { - if (request != null && request.Md5s != null) - { - data = _imageCacheService.GetByMd5s(request.Md5s, request.Platform); - } - return ApiResult.Ok(data); - } - catch (ArgumentException ex) - { - return ApiResult.Fail(ex.Message); + data = _imageCacheService.GetByMd5s(request.Md5s, request.Platform); } + return data; } @@ -63,7 +56,7 @@ namespace KJD_Web.Controllers { if (requests == null || requests.Length == 0) { - return ApiResult.Fail("参数不能为空"); + throw new ArgumentException("参数不能为空"); } var failedItems = new List(); @@ -71,7 +64,7 @@ namespace KJD_Web.Controllers { if (item == null) { - return ApiResult.Fail("图片参数不能包含空项", ResultCode.ValidationError); + throw new ArgumentException("图片参数不能包含空项"); } try { @@ -88,8 +81,8 @@ namespace KJD_Web.Controllers } return failedItems.Count == 0 - ? ApiResult.Ok() - : ApiResult.Fail("部分图片失败", failedItems); + ? ApiResult.Ok() + : throw new ArgumentException("部分图片失败"); } public class ImageCacheBatchQueryRequest {