增加一些 报错信息统一拦截
This commit is contained in:
1 parent
c9ac7412ab
commit
4a5fada435
3 files changed
+26
-23
No files matched your search
@@ -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<GlobalExceptionFilter> _logger;
|
||||
private readonly IHostEnvironment _environment;
|
||||
|
||||
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
|
||||
public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> 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);
|
||||
|
||||
@@ -34,6 +34,10 @@ namespace MAX.Framework.Dtos
|
||||
/// <summary>提示信息</summary>
|
||||
public string? Msg { get; init; }
|
||||
|
||||
/// <summary>失败请求的跟踪标识,用于关联服务器日志。</summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? TraceId { get; init; }
|
||||
|
||||
/// <summary>请求是否成功</summary>
|
||||
public bool Success => Code == ResultCode.Success;
|
||||
|
||||
@@ -52,7 +56,7 @@ namespace MAX.Framework.Dtos
|
||||
public static implicit operator ApiResult<T>(ApiResult result)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(result);
|
||||
return new ApiResult<T> { Code = result.Code, Msg = result.Msg, Data = default };
|
||||
return new ApiResult<T> { Code = result.Code, Msg = result.Msg, TraceId = result.TraceId, Data = default };
|
||||
}
|
||||
|
||||
public static ApiResult<T> 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)
|
||||
|
||||
@@ -36,21 +36,14 @@ namespace KJD_Web.Controllers
|
||||
/// 批量按 MD5 查询图片缓存。
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public ApiResult<List<ImageCacheDto>> GetImageCache(ImageCacheBatchQueryRequest request)
|
||||
public List<ImageCacheDto> GetImageCache(ImageCacheBatchQueryRequest request)
|
||||
{
|
||||
List<ImageCacheDto> data = new List<ImageCacheDto>();
|
||||
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<string>();
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user