版本260406
This commit is contained in:
@@ -47,6 +47,20 @@ public class Result
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建成功结果(携带 TraceId)。
|
||||
/// </summary>
|
||||
public static Result SuccessWithTrace(string traceId, string code = "OK", string message = "Success")
|
||||
{
|
||||
return new Result
|
||||
{
|
||||
Succeeded = true,
|
||||
Code = code,
|
||||
Message = message,
|
||||
TraceId = traceId
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建失败结果。
|
||||
/// </summary>
|
||||
@@ -60,6 +74,45 @@ public class Result
|
||||
Errors = errors
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建失败结果(携带 TraceId)。
|
||||
/// </summary>
|
||||
public static Result FailWithTrace(string code, string message, string traceId, params string[] errors)
|
||||
{
|
||||
return new Result
|
||||
{
|
||||
Succeeded = false,
|
||||
Code = code,
|
||||
Message = message,
|
||||
Errors = errors,
|
||||
TraceId = traceId
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 由异常创建失败结果。
|
||||
/// </summary>
|
||||
public static Result FromException(Exception exception, string code = "UNHANDLED_EXCEPTION", string message = "系统出现未处理异常。", string? traceId = null)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
var current = exception;
|
||||
|
||||
while (current is not null)
|
||||
{
|
||||
errors.Add(current.Message);
|
||||
current = current.InnerException;
|
||||
}
|
||||
|
||||
return new Result
|
||||
{
|
||||
Succeeded = false,
|
||||
Code = code,
|
||||
Message = message,
|
||||
Errors = errors,
|
||||
TraceId = traceId
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -86,6 +139,21 @@ public sealed class Result<T> : Result
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建成功结果(携带 TraceId)。
|
||||
/// </summary>
|
||||
public static Result<T> SuccessWithTrace(T data, string traceId, string code = "OK", string message = "Success")
|
||||
{
|
||||
return new Result<T>
|
||||
{
|
||||
Succeeded = true,
|
||||
Code = code,
|
||||
Message = message,
|
||||
Data = data,
|
||||
TraceId = traceId
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建失败结果。
|
||||
/// </summary>
|
||||
@@ -99,4 +167,19 @@ public sealed class Result<T> : Result
|
||||
Errors = errors
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建失败结果(携带 TraceId)。
|
||||
/// </summary>
|
||||
public static new Result<T> FailWithTrace(string code, string message, string traceId, params string[] errors)
|
||||
{
|
||||
return new Result<T>
|
||||
{
|
||||
Succeeded = false,
|
||||
Code = code,
|
||||
Message = message,
|
||||
Errors = errors,
|
||||
TraceId = traceId
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user