增加一些 报错信息统一拦截

This commit is contained in:
chfei committed 2026-10-23 09:52:16 +08:00
1 parent c9ac7412ab
commit 4a5fada435
3 files changed
+26 -23

No files matched your search

+7 -3
View File
@@ -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)