非法参数的异常统一使用WxPayException

This commit is contained in:
Binary Wang 2017-07-31 17:42:17 +08:00
parent 0a06c4d1a1
commit 70d90c8b9b
11 changed files with 28 additions and 25 deletions

View File

@ -122,7 +122,7 @@ public abstract class WxPayBaseRequest {
/** /**
* 检查约束情况 * 检查约束情况
*/ */
protected abstract void checkConstraints(); protected abstract void checkConstraints() throws WxPayException;
public String getAppid() { public String getAppid() {
return this.appid; return this.appid;

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required; import me.chanjar.weixin.common.annotation.Required;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
@ -128,13 +129,13 @@ public class WxPayDownloadBillRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) { if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) {
throw new IllegalArgumentException("tar_type值如果存在只能为GZIP"); throw new WxPayException("tar_type值如果存在只能为GZIP");
} }
if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) { if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) {
throw new IllegalArgumentException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s", throw new WxPayException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
Arrays.toString(BILL_TYPE), this.getBillType())); Arrays.toString(BILL_TYPE), this.getBillType()));
} }
} }

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -64,10 +65,10 @@ public class WxPayOrderQueryRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) || if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) { (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一"); throw new WxPayException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
} }
} }
} }

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -91,9 +92,9 @@ public class WxPayOrderReverseRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) { if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no不能同时为空"); throw new WxPayException("transaction_id 和 out_trade_no不能同时为空");
} }
} }

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.bean.request; package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -135,12 +136,12 @@ public class WxPayRefundQueryRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo) if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)
&& StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) || && StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo) (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo)
&& StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) { && StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) {
throw new IllegalArgumentException("transaction_idout_trade_noout_refund_norefund_id 必须四选一"); throw new WxPayException("transaction_idout_trade_noout_refund_norefund_id 必须四选一");
} }
} }

View File

@ -271,16 +271,16 @@ public class WxPayRefundRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
if (StringUtils.isNotBlank(this.getRefundAccount())) { if (StringUtils.isNotBlank(this.getRefundAccount())) {
if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) { if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) {
throw new IllegalArgumentException(String.format("refund_account目前必须为%s其中之一,实际值:%s", throw new WxPayException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount())); Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount()));
} }
} }
if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) { if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个"); throw new WxPayException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
} }
} }

View File

@ -500,18 +500,18 @@ public class WxPayUnifiedOrderRequest extends WxPayBaseRequest {
} }
@Override @Override
protected void checkConstraints() { protected void checkConstraints() throws WxPayException {
// if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) { // if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) {
// throw new IllegalArgumentException(String.format("trade_type目前必须为%s其中之一,实际值:%s", // throw new WxPayException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
// Arrays.toString(TRADE_TYPES), this.getTradeType())); // Arrays.toString(TRADE_TYPES), this.getTradeType()));
// } // }
if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) { if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) {
throw new IllegalArgumentException("当 trade_type是'JSAPI'时未指定openid"); throw new WxPayException("当 trade_type是'JSAPI'时未指定openid");
} }
if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) { if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) {
throw new IllegalArgumentException("当 trade_type是'NATIVE'时未指定product_id"); throw new WxPayException("当 trade_type是'NATIVE'时未指定product_id");
} }
} }

View File

@ -316,8 +316,8 @@ public abstract class WxPayBaseResult {
} }
//校验结果是否成功 //校验结果是否成功
if (!"SUCCESS".equalsIgnoreCase(getReturnCode()) if (!StringUtils.equalsAny(StringUtils.trimToEmpty(getReturnCode()).toUpperCase(), "SUCCESS", "")
|| !"SUCCESS".equalsIgnoreCase(getResultCode())) { || !StringUtils.equalsAny(StringUtils.trimToEmpty(getResultCode()).toUpperCase(), "SUCCESS", "")) {
StringBuilder errorMsg = new StringBuilder(); StringBuilder errorMsg = new StringBuilder();
if (getReturnCode() != null) { if (getReturnCode() != null) {
errorMsg.append("返回代码:").append(getReturnCode()); errorMsg.append("返回代码:").append(getReturnCode());
@ -335,8 +335,7 @@ public abstract class WxPayBaseResult {
errorMsg.append(",错误详情:").append(getErrCodeDes()); errorMsg.append(",错误详情:").append(getErrCodeDes());
} }
this.getLogger().error("\n结果业务代码异常返回結果{},\n{}", this.getLogger().error("\n结果业务代码异常返回結果{},\n{}", map, errorMsg.toString());
map, errorMsg.toString());
throw WxPayException.from(this); throw WxPayException.from(this);
} }
} }

View File

@ -160,11 +160,11 @@ public class WxPayConfig {
public SSLContext initSSLContext() throws WxPayException { public SSLContext initSSLContext() throws WxPayException {
if (StringUtils.isBlank(mchId)) { if (StringUtils.isBlank(mchId)) {
throw new IllegalArgumentException("请确保商户号mchId已设置"); throw new WxPayException("请确保商户号mchId已设置");
} }
if (StringUtils.isBlank(this.keyPath)) { if (StringUtils.isBlank(this.keyPath)) {
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置"); throw new WxPayException("请确保证书文件地址keyPath已配置");
} }
InputStream inputStream; InputStream inputStream;

View File

@ -161,7 +161,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
@Override @Override
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException { public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException {
if (StringUtils.isBlank(outTradeNo)) { if (StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("out_trade_no不能为空"); throw new WxPayException("out_trade_no不能为空");
} }
WxPayOrderCloseRequest request = new WxPayOrderCloseRequest(); WxPayOrderCloseRequest request = new WxPayOrderCloseRequest();

View File

@ -139,7 +139,7 @@ public class WxPayServiceAbstractImplTest {
.unifiedOrder(WxPayUnifiedOrderRequest.newBuilder() .unifiedOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去") .body("我去")
.totalFee(1) .totalFee(1)
.spbillCreateIp("111111") .spbillCreateIp("11.1.11.1")
.notifyURL("111111") .notifyURL("111111")
.tradeType("JSAPI") .tradeType("JSAPI")
.openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid()) .openid(((XmlWxPayConfig) this.payService.getConfig()).getOpenid())