Skip to content

Commit 7057b8a

Browse files
committed
为WxError加入builder模式支持
1 parent dca8e30 commit 7057b8a

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import java.io.Serializable;
66

77
/**
8-
* 微信错误码说明
9-
* http://mp.weixin.qq.com/wiki/index.php?title=全局返回码说明
8+
* 微信错误码说明,请阅读: <a href="http://mp.weixin.qq.com/wiki/10/6380dc743053a91c544ffd2b7c959166.html">全局返回码说明</a>
109
* @author Daniel Qian
1110
*
1211
*/
@@ -54,4 +53,30 @@ public String toString() {
5453
return "微信错误: errcode=" + errorCode + ", errmsg=" + errorMsg + "\njson:" + json;
5554
}
5655

56+
public static Builder newBuilder(){
57+
return new Builder();
58+
}
59+
60+
public static class Builder{
61+
private int errorCode;
62+
private String errorMsg;
63+
64+
public Builder setErrorCode(int errorCode) {
65+
this.errorCode = errorCode;
66+
return this;
67+
}
68+
69+
public Builder setErrorMsg(String errorMsg) {
70+
this.errorMsg = errorMsg;
71+
return this;
72+
}
73+
74+
public WxError build(){
75+
WxError wxError = new WxError();
76+
wxError.setErrorCode(this.errorCode);
77+
wxError.setErrorMsg(this.errorMsg);
78+
return wxError;
79+
}
80+
81+
}
5782
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,8 @@ public String qrCodePictureUrl(String ticket) throws WxErrorException {
483483
try {
484484
return String.format(url, URLEncoder.encode(ticket, Charsets.UTF_8.name()));
485485
} catch (UnsupportedEncodingException e) {
486-
WxError error = new WxError();
487-
error.setErrorCode(-1);
488-
error.setErrorMsg(e.getMessage());
486+
WxError error = WxError.newBuilder().setErrorCode(-1)
487+
.setErrorMsg(e.getMessage()).build();
489488
throw new WxErrorException(error);
490489
}
491490
}

0 commit comments

Comments
 (0)