namespace WebApplication1.Controllers; /// /// 异常结果类 /// public class ExceptionResult : ApiResult { public ExceptionResult(int errorCode, string errorMessage) { ErrorCode = errorCode; ErrorMessage = errorMessage; } public ExceptionResult(ApiException exception) : this(exception.ErrorCode, exception.Message) { } public ExceptionResult(Exception exception) : this(-1, exception.Message) { } } /// /// API 返回结果类 /// /// /// public class ApiResult(T result = default) { public int ErrorCode { get; set; } public string ErrorMessage { get; set; } public string ResourceId { get; set; } public T Result { get; set; } = result; public object Tag { get; set; } public static implicit operator ApiResult(T result) => new(result); } /// /// API 业务异常 /// /// 错误码 /// 错误信息 public class ApiException(int errorCode, string errorMessage = null) : Exception(errorMessage) { public const int UNKNOWN_ERROR = -1; public const int AUTHORIZED_ERROR = 1; public const int PARAMETER_ERROR = 2; public const int PERMISSION_ERROR = 3; public int ErrorCode { get; } = errorCode; } /// /// 权限异常 /// /// 状态码 /// 错误码 /// 错误信息 public class PermissionException(int statusCode, int errorCode = ApiException.AUTHORIZED_ERROR, string errorMessage = null) : ApiException(errorCode, errorMessage) { public int StatusCode { get; } = statusCode; }