Skip to content

Commit 8ab4af0

Browse files
committed
🎨 binarywang#1189 优化错误异常输出,移除冗余代码
1 parent eecd4be commit 8ab4af0

File tree

81 files changed

+789
-800
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+789
-800
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/WxType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
*/
1111
public enum WxType {
1212
/**
13-
* 企业微信
13+
* 企业微信.
1414
*/
1515
CP,
1616
/**
17-
* 微信公众号
17+
* 微信公众号.
1818
*/
1919
MP,
2020
/**
21-
* 微信小程序
21+
* 微信小程序.
2222
*/
2323
MiniApp,
2424
/**
25-
* 微信开放平台
25+
* 微信开放平台.
2626
*/
2727
Open,
2828
/**
29-
* 微信支付
29+
* 微信支付.
3030
*/
3131
Pay;
3232
}

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

Lines changed: 193 additions & 193 deletions
Large diffs are not rendered by default.

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

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

3-
import java.io.Serializable;
4-
5-
import org.apache.commons.lang3.StringUtils;
6-
73
import lombok.Builder;
84
import lombok.Data;
95
import me.chanjar.weixin.common.WxType;
106
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import java.io.Serializable;
1110

1211
/**
1312
* 微信错误码.
@@ -46,40 +45,50 @@ public static WxError fromJson(String json) {
4645

4746
public static WxError fromJson(String json, WxType type) {
4847
final WxError wxError = WxGsonBuilder.create().fromJson(json, WxError.class);
49-
if (StringUtils.isNotEmpty(wxError.getErrorMsg())) {
50-
wxError.setErrorMsgEn(wxError.getErrorMsg());
48+
if (wxError.getErrorCode() == 0 || type == null) {
49+
return wxError;
5150
}
5251

53-
if (type == null) {
54-
return wxError;
52+
if (StringUtils.isNotEmpty(wxError.getErrorMsg())) {
53+
wxError.setErrorMsgEn(wxError.getErrorMsg());
5554
}
5655

57-
if (type == WxType.MP) {
58-
final String msg = WxMpErrorMsgEnum.findMsgByCode(wxError.getErrorCode());
59-
if (msg != null) {
60-
wxError.setErrorMsg(msg);
56+
switch (type) {
57+
case MP: {
58+
final String msg = WxMpErrorMsgEnum.findMsgByCode(wxError.getErrorCode());
59+
if (msg != null) {
60+
wxError.setErrorMsg(msg);
61+
}
62+
break;
6163
}
62-
} else if (type == WxType.CP) {
63-
final String msg = WxCpErrorMsgEnum.findMsgByCode(wxError.getErrorCode());
64-
if (msg != null) {
65-
wxError.setErrorMsg(msg);
64+
case CP: {
65+
final String msg = WxCpErrorMsgEnum.findMsgByCode(wxError.getErrorCode());
66+
if (msg != null) {
67+
wxError.setErrorMsg(msg);
68+
}
69+
break;
6670
}
67-
} else if (type == WxType.MiniApp) {
71+
case MiniApp: {
6872
final String msg = WxMaErrorMsgEnum.findMsgByCode(wxError.getErrorCode());
6973
if (msg != null) {
7074
wxError.setErrorMsg(msg);
7175
}
76+
break;
77+
}
78+
default:
79+
return wxError;
7280
}
7381

7482
return wxError;
7583
}
7684

7785
@Override
7886
public String toString() {
79-
if (this.json != null) {
80-
return this.json;
87+
if (this.json == null) {
88+
return "错误代码:" + this.errorCode + ", 错误信息:" + this.errorMsg;
8189
}
82-
return "错误: Code=" + this.errorCode + ", Msg=" + this.errorMsg;
90+
91+
return "错误代码:" + this.errorCode + ", 错误信息:" + this.errorMsg + ",微信原始报文:" + this.json;
8392
}
8493

8594
}

0 commit comments

Comments
 (0)