2026-06-29 16:34:26 +08:00
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
2026-06-01 13:42:40 +08:00
|
|
|
namespace QYZH.InteractiveMagazine.Models.Common;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 业务异常类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BusinessException : Exception
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 错误码
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Code { get; set; }
|
|
|
|
|
|
2026-06-29 16:34:26 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 业务状态码
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ResultCode ResultCode { get; set; } = ResultCode.FAIL;
|
|
|
|
|
|
2026-06-01 13:42:40 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 无参构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public BusinessException() : base()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">异常消息</param>
|
|
|
|
|
public BusinessException(string message) : base(message)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">异常消息</param>
|
|
|
|
|
/// <param name="code">错误码</param>
|
|
|
|
|
public BusinessException(string message, int code) : base(message)
|
|
|
|
|
{
|
|
|
|
|
Code = code;
|
2026-06-29 16:34:26 +08:00
|
|
|
ResultCode = System.Enum.IsDefined(typeof(ResultCode), code) ? (ResultCode)code : ResultCode.FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数(按业务状态码)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">异常消息</param>
|
|
|
|
|
/// <param name="resultCode">业务状态码</param>
|
|
|
|
|
public BusinessException(string message, ResultCode resultCode) : base(message)
|
|
|
|
|
{
|
|
|
|
|
ResultCode = resultCode;
|
|
|
|
|
Code = (int)resultCode;
|
2026-06-01 13:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">异常消息</param>
|
|
|
|
|
/// <param name="innerException">内部异常</param>
|
|
|
|
|
public BusinessException(string message, Exception innerException) : base(message, innerException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2026-06-11 17:01:24 +08:00
|
|
|
|
|
|
|
|
public static void ThrowIf(bool isTrue, string message)
|
|
|
|
|
{
|
|
|
|
|
if (isTrue) throw new BusinessException(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ThrowIf(bool isTrue, Func<string> action)
|
|
|
|
|
{
|
|
|
|
|
if (isTrue) throw new BusinessException(action.Invoke());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ThrowIf(bool isTrue, string message, Action action)
|
|
|
|
|
{
|
|
|
|
|
if (isTrue)
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
throw new BusinessException(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-29 16:34:26 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 条件抛出业务异常(带业务状态码)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isTrue">是否触发</param>
|
|
|
|
|
/// <param name="message">异常消息</param>
|
|
|
|
|
/// <param name="resultCode">业务状态码</param>
|
|
|
|
|
public static void ThrowIf(bool isTrue, string message, ResultCode resultCode)
|
|
|
|
|
{
|
|
|
|
|
if (isTrue) throw new BusinessException(message, resultCode);
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
}
|