Skip to content

Commit 649af3e

Browse files
authored
🎨 错误码中文含义匹配优化
1 parent 31efa1a commit 649af3e

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/error/WxCpErrorMsgEnum.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.common.error;
22

3+
import com.google.common.collect.Maps;
34
import lombok.Getter;
45

6+
import java.util.Map;
7+
58
/**
69
* <pre>
710
* 企业微信全局错误码.
@@ -1072,23 +1075,23 @@ public enum WxCpErrorMsgEnum {
10721075
/**
10731076
* 提交审批单请求参数错误
10741077
*/
1075-
CODE_301025(301025,"提交审批单请求参数错误"),
1078+
CODE_301025(301025, "提交审批单请求参数错误"),
10761079
/**
10771080
* 不允许更新该用户的userid.
10781081
*/
10791082
CODE_301036(301036, "不允许更新该用户的userid"),
10801083
/**
10811084
* 无审批应用权限,或者提单者不在审批应用/自建应用的可见范围
10821085
*/
1083-
CODE_301055(301055,"无审批应用权限,或者提单者不在审批应用/自建应用的可见范围"),
1086+
CODE_301055(301055, "无审批应用权限,或者提单者不在审批应用/自建应用的可见范围"),
10841087
/**
10851088
* 审批应用已停用
10861089
*/
1087-
CODE_301056(301056,"审批应用已停用"),
1090+
CODE_301056(301056, "审批应用已停用"),
10881091
/**
10891092
* 通用错误码,提交审批单内部接口失败
10901093
*/
1091-
CODE_301057(301057,"通用错误码,提交审批单内部接口失败"),
1094+
CODE_301057(301057, "通用错误码,提交审批单内部接口失败"),
10921095
/**
10931096
* 批量导入任务的文件中userid有重复.
10941097
*/
@@ -1114,24 +1117,26 @@ public enum WxCpErrorMsgEnum {
11141117
*/
11151118
CODE_2000002(2000002, "CorpId参数无效;指定的CorpId不存在");
11161119

1117-
private int code;
1118-
private String msg;
1120+
private final int code;
1121+
private final String msg;
11191122

11201123
WxCpErrorMsgEnum(int code, String msg) {
11211124
this.code = code;
11221125
this.msg = msg;
11231126
}
11241127

1128+
static final Map<Integer, String> valueMap = Maps.newHashMap();
1129+
1130+
static {
1131+
for (WxCpErrorMsgEnum value : WxCpErrorMsgEnum.values()) {
1132+
valueMap.put(value.code, value.msg);
1133+
}
1134+
}
1135+
11251136
/**
11261137
* 通过错误代码查找其中文含义..
11271138
*/
11281139
public static String findMsgByCode(int code) {
1129-
for (WxCpErrorMsgEnum value : WxCpErrorMsgEnum.values()) {
1130-
if (value.code == code) {
1131-
return value.msg;
1132-
}
1133-
}
1134-
1135-
return null;
1140+
return valueMap.getOrDefault(code, null);
11361141
}
11371142
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/error/WxMaErrorMsgEnum.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.common.error;
22

3+
import com.google.common.collect.Maps;
34
import lombok.Getter;
45

6+
import java.util.Map;
7+
58
/**
69
* 微信小程序错误码
710
*
@@ -664,16 +667,18 @@ public enum WxMaErrorMsgEnum {
664667
this.msg = msg;
665668
}
666669

670+
static final Map<Integer, String> valueMap = Maps.newHashMap();
671+
672+
static {
673+
for (WxMaErrorMsgEnum value : WxMaErrorMsgEnum.values()) {
674+
valueMap.put(value.code, value.msg);
675+
}
676+
}
677+
667678
/**
668679
* 通过错误代码查找其中文含义.
669680
*/
670681
public static String findMsgByCode(int code) {
671-
for (WxMaErrorMsgEnum value : WxMaErrorMsgEnum.values()) {
672-
if (value.code == code) {
673-
return value.msg;
674-
}
675-
}
676-
677-
return null;
682+
return valueMap.getOrDefault(code, null);
678683
}
679684
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/error/WxMpErrorMsgEnum.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.common.error;
22

3+
import com.google.common.collect.Maps;
34
import lombok.Getter;
45

6+
import java.util.Map;
7+
58
/**
69
* <pre>
710
* 微信公众平台全局返回码.
@@ -648,24 +651,26 @@ public enum WxMpErrorMsgEnum {
648651
*/
649652
CODE_45084(45084, "没有设置 speed 参数");
650653

651-
private int code;
652-
private String msg;
654+
private final int code;
655+
private final String msg;
653656

654657
WxMpErrorMsgEnum(int code, String msg) {
655658
this.code = code;
656659
this.msg = msg;
657660
}
658661

662+
static final Map<Integer, String> valueMap = Maps.newHashMap();
663+
664+
static {
665+
for (WxMpErrorMsgEnum value : WxMpErrorMsgEnum.values()) {
666+
valueMap.put(value.code, value.msg);
667+
}
668+
}
669+
659670
/**
660671
* 通过错误代码查找其中文含义..
661672
*/
662673
public static String findMsgByCode(int code) {
663-
for (WxMpErrorMsgEnum value : WxMpErrorMsgEnum.values()) {
664-
if (value.code == code) {
665-
return value.msg;
666-
}
667-
}
668-
669-
return null;
674+
return valueMap.getOrDefault(code, null);
670675
}
671676
}

0 commit comments

Comments
 (0)