bag
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using Yh.Platform.Abstractions;
|
||||
|
||||
namespace YhCore.Common.Exceptions
|
||||
{
|
||||
/// <summary>int 错误码 → 新框架 <see cref="IErrorCode"/> 映射(对齐 Java ErrorCode 常量值)。</summary>
|
||||
/// <remarks>
|
||||
/// 用途:业务直接抛 <see cref="DException"/> 时,把数值错误码转成 IErrorCode:
|
||||
/// <code>throw new DException(ErrorCodeUtil.From(5001), "库存不足");</code>
|
||||
/// </remarks>
|
||||
public static class ErrorCodeUtil
|
||||
{
|
||||
public const int Default = 1;
|
||||
|
||||
/// <summary>按数值映射平台已知错误码;未知码保留原值(避免业务自定义错误码被吞成 Error=1)。</summary>
|
||||
public static IErrorCode From(int code) => code switch
|
||||
{
|
||||
0 => ErrorCode.Success,
|
||||
2 => ErrorCode.ParamError,
|
||||
3 => ErrorCode.Forbidden,
|
||||
4 => ErrorCode.Unauthorized,
|
||||
5 => ErrorCode.Custom,
|
||||
6 => ErrorCode.InternalServerError,
|
||||
405 => ErrorCode.HttpMethodNotAllowed,
|
||||
_ => new RawErrorCode(code, ""),
|
||||
};
|
||||
|
||||
/// <summary>未知码透传实现:保留调用方原始 code,msg 由业务消息承载。</summary>
|
||||
private sealed record RawErrorCode(int Code, string Msg) : IErrorCode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user