引入WxPayException,替代原有的异常处理类,并做相应的优化

This commit is contained in:
Binary Wang
2017-06-06 18:57:52 +08:00
parent ea13197844
commit 92c8a86199
9 changed files with 228 additions and 69 deletions

View File

@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.util.SignUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@@ -113,9 +114,13 @@ public abstract class WxPayBaseRequest {
/**
* 检查请求参数内容,包括必填参数以及特殊约束
*/
protected void checkFields() throws WxErrorException {
protected void checkFields() throws WxPayException {
//check required fields
BeanUtils.checkRequiredFields(this);
try {
BeanUtils.checkRequiredFields(this);
} catch (WxErrorException e) {
throw new WxPayException(e.getError().getErrorMsg());
}
//check other parameters
this.checkConstraints();
@@ -210,7 +215,7 @@ public abstract class WxPayBaseRequest {
*
* @param config 支付配置对象,用于读取相应系统配置信息
*/
public void checkAndSign(WxPayConfig config) throws WxErrorException {
public void checkAndSign(WxPayConfig config) throws WxPayException {
this.checkFields();
if (StringUtils.isBlank(getAppid())) {

View File

@@ -1,9 +1,9 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@@ -169,7 +169,7 @@ public class WxPayRefundRequest extends WxPayBaseRequest {
}
@Override
public void checkAndSign(WxPayConfig config) throws WxErrorException {
public void checkAndSign(WxPayConfig config) throws WxPayException {
if (StringUtils.isBlank(this.getOpUserId())) {
this.setOpUserId(config.getMchId());
}

View File

@@ -1,9 +1,9 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@@ -458,7 +458,7 @@ public class WxPayUnifiedOrderRequest extends WxPayBaseRequest {
}
@Override
public void checkAndSign(WxPayConfig config) throws WxErrorException {
public void checkAndSign(WxPayConfig config) throws WxPayException {
if (StringUtils.isBlank(this.getNotifyURL())) {
this.setNotifyURL(config.getNotifyUrl());
}

View File

@@ -1,13 +1,12 @@
package com.github.binarywang.wxpay.bean.result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.binarywang.wxpay.util.SignUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;
@@ -308,12 +307,12 @@ public abstract class WxPayBaseResult {
/**
* 校验返回结果签名
*/
public void checkResult(WxPayServiceImpl wxPayService) throws WxErrorException {
public void checkResult(WxPayServiceImpl wxPayService) throws WxPayException {
//校验返回结果签名
Map<String, String> map = toMap();
if (getSign() != null && !SignUtils.checkSign(map, wxPayService.getConfig().getMchKey())) {
this.getLogger().debug("校验结果签名失败,参数:{}", map);
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("参数格式校验错误!").build());
throw new WxPayException("参数格式校验错误!");
}
//校验结果是否成功
@@ -336,12 +335,9 @@ public abstract class WxPayBaseResult {
errorMsg.append(",错误详情:").append(getErrCodeDes());
}
WxError error = WxError.newBuilder()
.setErrorCode(-1)
.setErrorMsg(errorMsg.toString())
.build();
this.getLogger().error("\n结果业务代码异常返回結果{},\n{}", map, error);
throw new WxErrorException(error);
this.getLogger().error("\n结果业务代码异常返回結果{},\n{}",
map, errorMsg.toString());
throw WxPayException.from(this);
}
}
}