Browse Source

重构部分代码

tags/v0
zhaohe 10 months ago
parent
commit
340eb42dde
  1. BIN
      app.db
  2. 43
      src/main/java/a8k/type/appret/AppRet.java
  3. 85
      src/main/java/a8k/type/ecode/AppRetEcodeInfo.java
  4. 31
      src/main/java/a8k/type/ecode/ErrorContext.java

BIN
app.db

43
src/main/java/a8k/type/appret/AppRet.java

@ -1,6 +1,6 @@
package a8k.type.appret;
import a8k.type.ecode.AppRetEcodeInfo;
import a8k.type.ecode.ErrorContext;
import a8k.hardware.type.a8kcanprotocol.CmdId;
import a8k.hardware.type.a8kcanprotocol.MId;
import a8k.hardware.type.a8kcanprotocol.A8kEcode;
@ -11,24 +11,27 @@ public class AppRet<T> {
public AppRetType appRetType = AppRetType.NORMAL;
//错误信息
// 错误信息
@Getter
AppRetEcodeInfo ecode = null;
ErrorContext ecode = null;
// 显示给用户的内容
@Getter
String message = null;
//异常触发上下文
@Getter
String traceInfo = null;
//携带的对象
// 携带的对象
@Getter
String dataType;
@Getter
T data;
T data;
//显示给用户的内容
@Getter
String message = null;
// 接口请求时间
@Getter
long timestamp;//接口请求时间
long timestamp;
public AppRet() {
this.timestamp = System.currentTimeMillis();
@ -38,7 +41,6 @@ public class AppRet<T> {
return !AppRetType.FAILURE.equals(appRetType);
}
public static <T> AppRet<T> message(String message, T data) {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.MESSAGE;
@ -55,18 +57,16 @@ public class AppRet<T> {
return r;
}
public static <T> AppRet<T> success() {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.NORMAL;
return r;
}
public static <T> AppRet<T> fail(Integer errorCode, MId mid, CmdId cmd) {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.FAILURE;
r.ecode = new AppRetEcodeInfo(errorCode, mid, cmd);
r.ecode = new ErrorContext(errorCode, mid, cmd);
r.message = r.ecode.toDisPlayString();
return r;
}
@ -75,7 +75,7 @@ public class AppRet<T> {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.FAILURE;
r.ecode = new AppRetEcodeInfo(errorCode, mid, null);
r.ecode = new ErrorContext(errorCode, mid, null);
r.message = r.ecode.toDisPlayString();
return r;
}
@ -84,7 +84,7 @@ public class AppRet<T> {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.FAILURE;
r.ecode = new AppRetEcodeInfo(errorCode, null, null);
r.ecode = new ErrorContext(errorCode, null, null);
r.message = r.ecode.toDisPlayString();
return r;
}
@ -93,7 +93,7 @@ public class AppRet<T> {
AppRet<T> r = new AppRet<>();
r.appRetType = AppRetType.FAILURE;
r.ecode = new AppRetEcodeInfo(ret.getEcode().errorCode, ret.getEcode().mid, ret.getEcode().cmd);
r.ecode = new ErrorContext(ret.getEcode().code, ret.getEcode().relateMid, ret.getEcode().relateCmd);
r.data = null;
r.message = r.ecode.toDisPlayString();
return r;
@ -109,20 +109,19 @@ public class AppRet<T> {
r.appRetType = AppRetType.FAILURE;
if (e instanceof HardwareException hexcep) {
r.ecode = new AppRetEcodeInfo(hexcep.getErrorCode(), hexcep.getModuleId(), hexcep.getCmdId());
r.ecode = new ErrorContext(hexcep.getErrorCode(), hexcep.getModuleId(), hexcep.getCmdId());
r.traceInfo = trace;
r.message = r.ecode.toDisPlayString();
if (hexcep.getExtMessage() != null) {
r.message = r.message + "\n 额外信息:"+hexcep.getExtMessage();
r.message = r.message + "\n 额外信息:" + hexcep.getExtMessage();
}
} else {
r.ecode = new AppRetEcodeInfo(A8kEcode.CodeException.index, null, null);
r.ecode = new ErrorContext(A8kEcode.CodeException.index, null, null);
r.traceInfo = trace;
r.message = e.getMessage();
r.message = e.getMessage();
}
return r;
}
}

85
src/main/java/a8k/type/ecode/AppRetEcodeInfo.java

@ -1,85 +0,0 @@
package a8k.type.ecode;
import a8k.hardware.type.a8kcanprotocol.A8kEcode;
import a8k.hardware.type.a8kcanprotocol.CmdId;
import a8k.hardware.type.a8kcanprotocol.MId;
public class AppRetEcodeInfo {
public Integer errorCode;
public MId mid;
public CmdId cmd;
public AppRetEcodeInfo(Integer errorCode, MId mid, CmdId cmd) {
this.errorCode = errorCode;
this.mid = mid;
this.cmd = cmd;
}
// code
public Integer getCode() {
return errorCode;
}
public String getCodeName() {
return errorCode.toString();
}
// mid code
public Integer getMidCode() {
if (mid == null) {
return null;
}
return mid.toInt();
}
public String getMidChName() {
if (mid == null) {
return null;
}
return mid.chname;
}
public String getMidCodeName() {
if (mid == null) {
return null;
}
return mid.toString();
}
// cmd code
public Integer getCmdCode() {
if (cmd == null) {
return null;
}
return cmd.toInt();
}
public String getCmdChName() {
if (cmd == null) {
return null;
}
return cmd.chName;
}
public String getCmdCodeName() {
if (cmd == null) {
return null;
}
return cmd.toString();
}
public String toDisPlayString() {
String info;
info = String.format("错误: (%s)%d", A8kEcode.toDisPlayString(errorCode), errorCode);
if (mid != null) {
info += String.format("\n 相关模块: %s", mid.chname);
}
if (cmd != null) {
info += String.format("\n 相关指令: %s", cmd.chName);
}
return info;
}
}

31
src/main/java/a8k/type/ecode/ErrorContext.java

@ -0,0 +1,31 @@
package a8k.type.ecode;
import a8k.hardware.type.a8kcanprotocol.A8kEcode;
import a8k.hardware.type.a8kcanprotocol.CmdId;
import a8k.hardware.type.a8kcanprotocol.MId;
public class ErrorContext {
public Integer code;
public String codeName;
public MId relateMid;
public CmdId relateCmd;
public ErrorContext(Integer errorCode, MId mid, CmdId cmd) {
this.code = errorCode;
this.codeName = A8kEcode.toDisPlayString(errorCode);
this.relateMid = mid;
this.relateCmd = cmd;
}
public String toDisPlayString() {
String info;
info = String.format("错误: (%s)%d", A8kEcode.toDisPlayString(code), code);
if (relateMid != null) {
info += String.format("\n 相关模块: %s", relateMid.chname);
}
if (relateCmd != null) {
info += String.format("\n 相关指令: %s", relateCmd.chName);
}
return info;
}
}
Loading…
Cancel
Save