Jelajahi Sumber

feat(exception): 自定义异常

wangpx 9 bulan lalu
induk
melakukan
2419d45113

+ 49 - 0
admin/src/main/java/com/dcs/equipment/exception/device/DeviceConnectionException.java

@@ -0,0 +1,49 @@
+package com.dcs.equipment.exception.device;
+
+import lombok.Data;
+
+/**
+ * 设备连接异常
+ * @author: wangpx
+ * @date: 2025-07-22 15:20
+ */
+@Data
+public class DeviceConnectionException extends DeviceException {
+    private static final long serialVersionUID = 1L;
+    private String deviceAddress;
+    private Integer devicePort;
+
+    public DeviceConnectionException() {
+        super();
+    }
+
+    public DeviceConnectionException(String message) {
+        super(message);
+    }
+
+    public DeviceConnectionException(String message, Integer code) {
+        super(message, code);
+    }
+
+    public DeviceConnectionException(String message, String deviceAddress, Integer devicePort) {
+        super(message);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+
+    public DeviceConnectionException(String message, Integer code, String deviceAddress, Integer devicePort) {
+        super(message, code);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+
+    public DeviceConnectionException(Exception e) {
+        super(e);
+    }
+
+    public DeviceConnectionException(Exception e, String deviceAddress, Integer devicePort) {
+        super(e);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+} 

+ 99 - 0
admin/src/main/java/com/dcs/equipment/exception/device/DeviceExceptionConverter.java

@@ -0,0 +1,99 @@
+package com.dcs.equipment.exception.device;
+
+import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
+import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
+import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.UnknownHostException;
+
+/**
+ * 设备异常转换工具类
+ * @author: wangpx
+ * @date: 2025-01-02 10:00
+ */
+public class DeviceExceptionConverter {
+    private static final Logger logger = LoggerFactory.getLogger(DeviceExceptionConverter.class);
+
+    /**
+     * 转换连接相关异常
+     */
+    public static DeviceConnectionException convertConnectionException(Exception e, String deviceAddress, Integer devicePort) {
+        String message;
+        if (e instanceof UnknownHostException) {
+            message = String.format("设备地址解析失败: %s:%d - 请检查主机地址是否正确", deviceAddress, devicePort);
+        } else if (e instanceof ModbusIOException) {
+            message = String.format("设备网络连接失败: %s:%d - 请检查网络连接和设备状态", deviceAddress, devicePort);
+        } else {
+            message = String.format("设备连接异常: %s:%d - %s", deviceAddress, devicePort, e.getMessage());
+        }
+        
+        logger.error(message, e);
+        DeviceConnectionException exception = new DeviceConnectionException(message, deviceAddress, devicePort);
+        exception.initCause(e); // 保存原始异常信息
+        return exception;
+    }
+
+    /**
+     * 转换协议相关异常
+     */
+    public static DeviceProtocolException convertProtocolException(Exception e, String functionCode, Integer slaveId, Integer offset, Integer quantity) {
+        String message;
+        if (e instanceof ModbusProtocolException) {
+            message = String.format("Modbus协议异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
+                functionCode, slaveId, offset, quantity, e.getMessage());
+        } else if (e instanceof ModbusNumberException) {
+            message = String.format("Modbus参数异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
+                functionCode, slaveId, offset, quantity, e.getMessage());
+        } else {
+            message = String.format("设备协议异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
+                functionCode, slaveId, offset, quantity, e.getMessage());
+        }
+        
+        logger.error(message, e);
+        return new DeviceProtocolException(message, functionCode, slaveId, offset, quantity);
+    }
+
+    /**
+     * 转换参数相关异常
+     */
+    public static DeviceParameterException convertParameterException(String parameterName, Object parameterValue, String expectedRange) {
+        String message = String.format("参数验证失败 - 参数名:%s, 参数值:%s, 期望范围:%s", 
+            parameterName, parameterValue, expectedRange);
+        
+        logger.error(message);
+        return new DeviceParameterException(message, parameterName, parameterValue, expectedRange);
+    }
+
+    /**
+     * 转换超时相关异常
+     */
+    public static DeviceTimeoutException convertTimeoutException(String deviceAddress, Integer devicePort, Long timeoutDuration, String operationType) {
+        String message = String.format("设备操作超时 - 地址:%s:%d, 超时时间:%dms, 操作类型:%s", 
+            deviceAddress, devicePort, timeoutDuration, operationType);
+        
+        logger.error(message);
+        return new DeviceTimeoutException(message, deviceAddress, devicePort, timeoutDuration, operationType);
+    }
+
+    /**
+     * 转换通用设备异常
+     */
+    public static DeviceException convertDeviceException(Exception e) {
+        String message = String.format("设备操作异常: %s", e.getMessage());
+        
+        logger.error(message, e);
+        return new DeviceException(message);
+    }
+
+    /**
+     * 转换IO异常
+     */
+    public static DeviceIOException convertIOException(Exception e) {
+        String message = String.format("设备IO异常: %s", e.getMessage());
+        
+        logger.error(message, e);
+        return new DeviceIOException(message);
+    }
+} 

+ 64 - 0
admin/src/main/java/com/dcs/equipment/exception/device/DeviceParameterException.java

@@ -0,0 +1,64 @@
+package com.dcs.equipment.exception.device;
+
+import lombok.Data;
+
+/**
+ * 设备参数异常
+ * @author: wangpx
+ * @date: 2025-07-22 14:55
+ */
+@Data
+public class DeviceParameterException extends DeviceException {
+    private static final long serialVersionUID = 1L;
+    private String parameterName;
+    private Object parameterValue;
+    private String expectedRange;
+
+    public DeviceParameterException() {
+        super();
+    }
+
+    public DeviceParameterException(String message) {
+        super(message);
+    }
+
+    public DeviceParameterException(String message, Integer code) {
+        super(message, code);
+    }
+
+    public DeviceParameterException(String message, String parameterName, Object parameterValue) {
+        super(message);
+        this.parameterName = parameterName;
+        this.parameterValue = parameterValue;
+    }
+
+    public DeviceParameterException(String message, Integer code, String parameterName, Object parameterValue) {
+        super(message, code);
+        this.parameterName = parameterName;
+        this.parameterValue = parameterValue;
+    }
+
+    public DeviceParameterException(String message, String parameterName, Object parameterValue, String expectedRange) {
+        super(message);
+        this.parameterName = parameterName;
+        this.parameterValue = parameterValue;
+        this.expectedRange = expectedRange;
+    }
+
+    public DeviceParameterException(String message, Integer code, String parameterName, Object parameterValue, String expectedRange) {
+        super(message, code);
+        this.parameterName = parameterName;
+        this.parameterValue = parameterValue;
+        this.expectedRange = expectedRange;
+    }
+
+    public DeviceParameterException(Exception e) {
+        super(e);
+    }
+
+    public DeviceParameterException(Exception e, String parameterName, Object parameterValue) {
+        super(e);
+        this.parameterName = parameterName;
+        this.parameterValue = parameterValue;
+    }
+} 

+ 57 - 0
admin/src/main/java/com/dcs/equipment/exception/device/DeviceProtocolException.java

@@ -0,0 +1,57 @@
+package com.dcs.equipment.exception.device;
+
+import lombok.Data;
+
+/**
+ * 设备协议异常
+ * @author: wangpx
+ * @date: 2025-07-22 15:06
+ */
+@Data
+public class DeviceProtocolException extends DeviceException {
+    private static final long serialVersionUID = 1L;
+    private String functionCode;
+    private Integer slaveId;
+    private Integer offset;
+    private Integer quantity;
+
+    public DeviceProtocolException() {
+        super();
+    }
+
+    public DeviceProtocolException(String message) {
+        super(message);
+    }
+
+    public DeviceProtocolException(String message, Integer code) {
+        super(message, code);
+    }
+
+    public DeviceProtocolException(String message, String functionCode, Integer slaveId, Integer offset, Integer quantity) {
+        super(message);
+        this.functionCode = functionCode;
+        this.slaveId = slaveId;
+        this.offset = offset;
+        this.quantity = quantity;
+    }
+
+    public DeviceProtocolException(String message, Integer code, String functionCode, Integer slaveId, Integer offset, Integer quantity) {
+        super(message, code);
+        this.functionCode = functionCode;
+        this.slaveId = slaveId;
+        this.offset = offset;
+        this.quantity = quantity;
+    }
+
+    public DeviceProtocolException(Exception e) {
+        super(e);
+    }
+
+    public DeviceProtocolException(Exception e, String functionCode, Integer slaveId, Integer offset, Integer quantity) {
+        super(e);
+        this.functionCode = functionCode;
+        this.slaveId = slaveId;
+        this.offset = offset;
+        this.quantity = quantity;
+    }
+} 

+ 67 - 0
admin/src/main/java/com/dcs/equipment/exception/device/DeviceTimeoutException.java

@@ -0,0 +1,67 @@
+package com.dcs.equipment.exception.device;
+
+import lombok.Data;
+
+/**
+ * 设备超时异常
+ * @author: wangpx
+ * @date: 2025-07-22 16:13
+ */
+@Data
+public class DeviceTimeoutException extends DeviceException {
+    private static final long serialVersionUID = 1L;
+    private String deviceAddress;
+    private Integer devicePort;
+    private Long timeoutDuration;
+    private String operationType;
+
+    public DeviceTimeoutException() {
+        super();
+    }
+
+    public DeviceTimeoutException(String message) {
+        super(message);
+    }
+
+    public DeviceTimeoutException(String message, Integer code) {
+        super(message, code);
+    }
+
+    public DeviceTimeoutException(String message, String deviceAddress, Integer devicePort) {
+        super(message);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+
+    public DeviceTimeoutException(String message, Integer code, String deviceAddress, Integer devicePort) {
+        super(message, code);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+
+    public DeviceTimeoutException(String message, String deviceAddress, Integer devicePort, Long timeoutDuration, String operationType) {
+        super(message);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+        this.timeoutDuration = timeoutDuration;
+        this.operationType = operationType;
+    }
+
+    public DeviceTimeoutException(String message, Integer code, String deviceAddress, Integer devicePort, Long timeoutDuration, String operationType) {
+        super(message, code);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+        this.timeoutDuration = timeoutDuration;
+        this.operationType = operationType;
+    }
+
+    public DeviceTimeoutException(Exception e) {
+        super(e);
+    }
+
+    public DeviceTimeoutException(Exception e, String deviceAddress, Integer devicePort) {
+        super(e);
+        this.deviceAddress = deviceAddress;
+        this.devicePort = devicePort;
+    }
+}