diff --git a/src/main/java/a8k/A8kApplication.java b/src/main/java/a8k/A8kApplication.java index 96faa68..2eb159a 100644 --- a/src/main/java/a8k/A8kApplication.java +++ b/src/main/java/a8k/A8kApplication.java @@ -1,23 +1,24 @@ package a8k; + +import a8k.appbean.ecode.AppRet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; + import java.util.HashMap; import java.util.Map; + @ControllerAdvice public class A8kApplication { + + Logger logger = LoggerFactory.getLogger(A8kApplication.class); + @ResponseBody - @ExceptionHandler(value=Exception.class) - public Map controllerExceptionHandler(Exception e){ - StringBuilder traceSb = new StringBuilder(); - for ( var trace : e.getStackTrace() ) { - traceSb.append(trace.toString()).append("\n"); - } - String trace = traceSb.toString(); - Map map = new HashMap<>(); - map.put("success",false); - map.put("message",String.format("%s\n\nStack Trace : \n%s",e.getMessage(), trace)); - map.put("data", null); - return map; + @ExceptionHandler(value = Exception.class) + public AppRet controllerExceptionHandler(Exception e) { + logger.info("捕获到异常 : ", e); + return AppRet.fail(e); } } diff --git a/src/main/java/a8k/a8k_can_protocol/A8kEcode.java b/src/main/java/a8k/a8k_can_protocol/A8kEcode.java index 4f64aa6..2d81daa 100644 --- a/src/main/java/a8k/a8k_can_protocol/A8kEcode.java +++ b/src/main/java/a8k/a8k_can_protocol/A8kEcode.java @@ -21,6 +21,7 @@ public enum A8kEcode { RecycleBinOverflow(15, "回收仓满"),// MotorLostStep(16, "电机丢步"),// ActionOvertime(17, "动作执行超时"),// + CodeException(18, "代码异常"),// // diff --git a/src/main/java/a8k/appbean/ecode/AppRet.java b/src/main/java/a8k/appbean/ecode/AppRet.java index 8025b47..f0b0242 100644 --- a/src/main/java/a8k/appbean/ecode/AppRet.java +++ b/src/main/java/a8k/appbean/ecode/AppRet.java @@ -11,6 +11,7 @@ public class AppRet { AppRetEcodeInfo ecode; T data; long timestamp;//接口请求时间 + String traceInfo; public AppRet() { this.timestamp = System.currentTimeMillis(); @@ -32,6 +33,10 @@ public class AppRet { return data; } + public String getTraceInfo() { + return traceInfo; + } + public static AppRet success(T data) { AppRet r = new AppRet<>(); @@ -73,12 +78,27 @@ public class AppRet { return r; } - public static AppRet fail(HardwareException e) { - AppRet r = new AppRet<>(); - r.suc = false; - r.ecode = new AppRetEcodeInfo(e.getErrorCode(), e.getModuleId(), e.getCmdId()); - r.data = null; + public static AppRet fail(Exception e) { + StringBuilder traceSb = new StringBuilder(); + for (var trace : e.getStackTrace()) { + traceSb.append(trace.toString()).append("\n"); + } + String trace = traceSb.toString(); + AppRet r = new AppRet<>(); + + if (e instanceof HardwareException hexcep) { + r.suc = false; + r.ecode = new AppRetEcodeInfo(hexcep.getErrorCode(), hexcep.getModuleId(), hexcep.getCmdId()); + r.data = null; + r.traceInfo = trace; + } else { + r.suc = false; + r.ecode = new AppRetEcodeInfo(A8kEcode.CodeException, null, null); + r.data = null; + r.traceInfo = trace; + } return r; } + } diff --git a/src/main/java/a8k/controller/TmpTestController.java b/src/main/java/a8k/controller/TmpTestController.java index 89bd84b..d01a122 100644 --- a/src/main/java/a8k/controller/TmpTestController.java +++ b/src/main/java/a8k/controller/TmpTestController.java @@ -63,7 +63,7 @@ public class TmpTestController { @PostMapping("/api/zhaohe_test/initialize_device") @ResponseBody - public AppRet initialize_device() { + public AppRet initialize_device() throws HardwareException, InterruptedException { return deviceInitializationCtrlService.initializeDevice(); // return "OK"; } diff --git a/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java b/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java index 3c37f4a..cd3fe59 100644 --- a/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java +++ b/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java @@ -35,7 +35,7 @@ public class DeviceInitializationCtrlService { @PostMapping("/api/DeviceInitializationCtrlService/initializeDevice") @ResponseBody - public AppRet initializeDevice() { + public AppRet initializeDevice() throws HardwareException, InterruptedException { logger.info("Initializing device ..."); boolean initSuc = false; try { @@ -71,15 +71,9 @@ public class DeviceInitializationCtrlService { // 清空耗材 initSuc = true; -// throw new RuntimeException("test"); + // throw new RuntimeException("test"); - } catch (HardwareException e) { - logger.error("HardwareException ", e); - throw new RuntimeException(e); - } catch (InterruptedException e) { - logger.error("InterruptedException", e); - throw new RuntimeException(e); } finally { if (!initSuc) { try { @@ -121,6 +115,7 @@ public class DeviceInitializationCtrlService { //试管平移通道是否有障碍 if (canBus.getIOState(IOId.THChInterPPS) || canBus.getIOState(IOId.THChOuterPPS)) { logger.warn("THChInterPPS or THChOuterPPS is trigger"); + // throw new HardwareException(MId.A8kIdCardReaderBoard, A8kEcode.PlateStuckDetectorSensorTrigger); return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger); } diff --git a/zhaohe_app.db b/zhaohe_app.db index 472e049..7e8293b 100644 Binary files a/zhaohe_app.db and b/zhaohe_app.db differ