Skip to content

Commit e886ba0

Browse files
committed
非法参数的异常统一使用WxPayException
1 parent d6e1ad4 commit e886ba0

File tree

11 files changed

+28
-25
lines changed

11 files changed

+28
-25
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected void checkFields() throws WxPayException {
122122
/**
123123
* 检查约束情况
124124
*/
125-
protected abstract void checkConstraints();
125+
protected abstract void checkConstraints() throws WxPayException;
126126

127127
public String getAppid() {
128128
return this.appid;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDownloadBillRequest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.bean.request;
22

3+
import com.github.binarywang.wxpay.exception.WxPayException;
34
import com.thoughtworks.xstream.annotations.XStreamAlias;
45
import me.chanjar.weixin.common.annotation.Required;
56
import org.apache.commons.lang3.ArrayUtils;
@@ -128,13 +129,13 @@ public void setTarType(String tarType) {
128129
}
129130

130131
@Override
131-
protected void checkConstraints() {
132+
protected void checkConstraints() throws WxPayException {
132133
if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) {
133-
throw new IllegalArgumentException("tar_type值如果存在,只能为GZIP");
134+
throw new WxPayException("tar_type值如果存在,只能为GZIP");
134135
}
135136

136137
if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) {
137-
throw new IllegalArgumentException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
138+
throw new WxPayException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
138139
Arrays.toString(BILL_TYPE), this.getBillType()));
139140
}
140141
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderQueryRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.bean.request;
22

3+
import com.github.binarywang.wxpay.exception.WxPayException;
34
import com.thoughtworks.xstream.annotations.XStreamAlias;
45
import org.apache.commons.lang3.StringUtils;
56

@@ -64,10 +65,10 @@ public void setOutTradeNo(String outTradeNo) {
6465
}
6566

6667
@Override
67-
protected void checkConstraints() {
68+
protected void checkConstraints() throws WxPayException {
6869
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
6970
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) {
70-
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
71+
throw new WxPayException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
7172
}
7273
}
7374
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayOrderReverseRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.bean.request;
22

3+
import com.github.binarywang.wxpay.exception.WxPayException;
34
import com.thoughtworks.xstream.annotations.XStreamAlias;
45
import org.apache.commons.lang3.StringUtils;
56

@@ -91,9 +92,9 @@ public void setSignType(String signType) {
9192
}
9293

9394
@Override
94-
protected void checkConstraints() {
95+
protected void checkConstraints() throws WxPayException {
9596
if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) {
96-
throw new IllegalArgumentException("transaction_id 和 out_trade_no不能同时为空!");
97+
throw new WxPayException("transaction_id 和 out_trade_no不能同时为空!");
9798
}
9899
}
99100

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundQueryRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.bean.request;
22

3+
import com.github.binarywang.wxpay.exception.WxPayException;
34
import com.thoughtworks.xstream.annotations.XStreamAlias;
45
import org.apache.commons.lang3.StringUtils;
56

@@ -135,12 +136,12 @@ public void setRefundId(String refundId) {
135136
}
136137

137138
@Override
138-
protected void checkConstraints() {
139+
protected void checkConstraints() throws WxPayException {
139140
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)
140141
&& StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) ||
141142
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo)
142143
&& StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) {
143-
throw new IllegalArgumentException("transaction_id,out_trade_no,out_refund_no,refund_id 必须四选一");
144+
throw new WxPayException("transaction_id,out_trade_no,out_refund_no,refund_id 必须四选一");
144145
}
145146

146147
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayRefundRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,16 @@ public void checkAndSign(WxPayConfig config) throws WxPayException {
271271
}
272272

273273
@Override
274-
protected void checkConstraints() {
274+
protected void checkConstraints() throws WxPayException {
275275
if (StringUtils.isNotBlank(this.getRefundAccount())) {
276276
if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) {
277-
throw new IllegalArgumentException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
277+
throw new WxPayException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
278278
Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount()));
279279
}
280280
}
281281

282282
if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) {
283-
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
283+
throw new WxPayException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
284284
}
285285
}
286286

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayUnifiedOrderRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,18 @@ public void setSceneInfo(String sceneInfo) {
500500
}
501501

502502
@Override
503-
protected void checkConstraints() {
503+
protected void checkConstraints() throws WxPayException {
504504
// if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) {
505-
// throw new IllegalArgumentException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
505+
// throw new WxPayException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
506506
// Arrays.toString(TRADE_TYPES), this.getTradeType()));
507507
// }
508508

509509
if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) {
510-
throw new IllegalArgumentException("当 trade_type是'JSAPI'时未指定openid");
510+
throw new WxPayException("当 trade_type是'JSAPI'时未指定openid");
511511
}
512512

513513
if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) {
514-
throw new IllegalArgumentException("当 trade_type是'NATIVE'时未指定product_id");
514+
throw new WxPayException("当 trade_type是'NATIVE'时未指定product_id");
515515
}
516516
}
517517

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayBaseResult.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ public void checkResult(WxPayServiceAbstractImpl wxPayService) throws WxPayExcep
316316
}
317317

318318
//校验结果是否成功
319-
if (!"SUCCESS".equalsIgnoreCase(getReturnCode())
320-
|| !"SUCCESS".equalsIgnoreCase(getResultCode())) {
319+
if (!StringUtils.equalsAny(StringUtils.trimToEmpty(getReturnCode()).toUpperCase(), "SUCCESS", "")
320+
|| !StringUtils.equalsAny(StringUtils.trimToEmpty(getResultCode()).toUpperCase(), "SUCCESS", "")) {
321321
StringBuilder errorMsg = new StringBuilder();
322322
if (getReturnCode() != null) {
323323
errorMsg.append("返回代码:").append(getReturnCode());
@@ -335,8 +335,7 @@ public void checkResult(WxPayServiceAbstractImpl wxPayService) throws WxPayExcep
335335
errorMsg.append(",错误详情:").append(getErrCodeDes());
336336
}
337337

338-
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}",
339-
map, errorMsg.toString());
338+
this.getLogger().error("\n结果业务代码异常,返回結果:{},\n{}", map, errorMsg.toString());
340339
throw WxPayException.from(this);
341340
}
342341
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ public void setUseSandboxEnv(boolean useSandboxEnv) {
160160

161161
public SSLContext initSSLContext() throws WxPayException {
162162
if (StringUtils.isBlank(mchId)) {
163-
throw new IllegalArgumentException("请确保商户号mchId已设置");
163+
throw new WxPayException("请确保商户号mchId已设置");
164164
}
165165

166166
if (StringUtils.isBlank(this.keyPath)) {
167-
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置");
167+
throw new WxPayException("请确保证书文件地址keyPath已配置");
168168
}
169169

170170
InputStream inputStream;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
161161
@Override
162162
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException {
163163
if (StringUtils.isBlank(outTradeNo)) {
164-
throw new IllegalArgumentException("out_trade_no不能为空");
164+
throw new WxPayException("out_trade_no不能为空");
165165
}
166166

167167
WxPayOrderCloseRequest request = new WxPayOrderCloseRequest();

0 commit comments

Comments
 (0)