ExtendException.cs 3.88 KB
namespace Rcs.Domain.Extensions;

/// <summary>
/// 通用业务异常扩展集
/// </summary>
public class BusinessException : Exception
{
    public int ErrorCode { get; set; }

    public BusinessException(string message, int errorCode = 0)
        : base(message)
    {
        ErrorCode = errorCode;
    }
}
/// <summary>
/// 通用异常扩展集
/// </summary>
public class SharedException : Exception
{
    public int Code { get; }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="robotId"></param>
    /// <param name="code"></param>
    /// <param name="message"></param>
    public SharedException(int code, string message = "")
        : base($"错误代码[{code}] - {GetMessage(code)} {message}")
    {
        Code = code;
    }

    private static string GetMessage(int code)
    {
        return code switch
        {
            10000 => "入参不正确",
            10001 => "消息格式不正确",
            10002 => "消息已过期",
            _ => $"未知错误"
        };
    }
}
/// <summary>
/// 机器人异常扩展集
/// </summary>
public class ExtendRobotException : Exception
{
    public string RobotId { get; }
    public int Code { get; }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="robotId"></param>
    /// <param name="code"></param>
    /// <param name="message"></param>
    public ExtendRobotException(string robotId, int code, string message = "")
        : base($"错误代码[{code}] - 机器人 {robotId} : {GetMessage(code)} {message}")
    {
        RobotId = robotId;
        Code = code;
    }

    private static string GetMessage(int code)
    {
        return code switch
        {
            20000 => "机器人离线",
            20001 => "机器人路径锁死",
            20002 => "机器人上线失败",
            20003 => "不存在机器人",
            20004 => "机器人无法定位",
            20005 => "机器人不在拓扑点位",
            _ => $"未知机器人错误"
        };
    }
}

public class ExtendTaskException : Exception
{
    public string TaskId { get; }
    public int Code { get; }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="robotId"></param>
    /// <param name="code"></param>
    public ExtendTaskException(string taskId, int code, string message = "")
        : base($"错误代码[{code}] - 任务 {taskId} : {GetMessage(code)} {message}")
    {
        TaskId = taskId;
        Code = code;
    }

    private static string GetMessage(int code)
    {
        return code switch
        {
            30000 => "相同起点和终点任务已存在",
            30001 => "路径规划失败",
            30002 => "路径不可达",
            30003 => "起点和终点位置相同",
            30004 => "任务不存在",
            30005 => "任务已取消",
            30006 => "任务已完成",
            30007 => "任务执行中,无法取消",
            30008 => "任务未分配,无法取消",
            30009 => "任务未开始,无法暂停",
            30010 => "任务未暂停,无法恢复",
            30011 => "任务未暂停,无法取消",
            30012 => "起始位置不存在",
            30013 => "目标位置不存在",
            _ => "未知任务错误"
        };
    }


}
public class ExtendModelException : Exception
{
    public string TaskId { get; }
    public long Code { get; }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="robotId"></param>
    /// <param name="code"></param>
    public ExtendModelException(string modelId, long code, string message = "")
        : base($"错误代码[{code}] - 模型 {modelId} : {GetMessage(code)} {message}")
    {
        TaskId = modelId;
        Code = code;
    }

    private static string GetMessage(long code)
    {
        return code switch
        {
            40000 => "地图数据不存在",
            _ => "未知任务错误"
        };
    }


}