【微信支付】电商收付通相关接口抛出异常时加入状态码等信息

This commit is contained in:
f00lish 2020-12-08 13:35:58 +08:00 committed by GitHub
parent 2a45fe4ade
commit 34495cb655
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils;
@ -106,11 +107,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
@ -141,12 +142,12 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
e.printStackTrace();
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
@ -172,11 +173,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【异常信息】{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpPost.releaseConnection();
}
@ -198,11 +199,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
} else {
//有错误提示信息返回
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【异常信息】{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpGet.releaseConnection();
}
@ -223,11 +224,11 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
//有错误提示信息返回
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JsonObject jsonObject = GsonParser.parse(responseString);
throw new WxPayException(jsonObject.get("message").getAsString());
throw convertException(jsonObject);
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【异常信息】{}", url, e.getMessage());
throw new WxPayException(e.getMessage(), e);
throw (e instanceof WxPayException) ? (WxPayException)e : new WxPayException(e.getMessage(), e);
} finally {
httpGet.releaseConnection();
}
@ -291,4 +292,16 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
}
private WxPayException convertException(JsonObject jsonObject) {
//todo 这里考虑使用新的适用于V3的异常
JsonElement codeElement = jsonObject.get("code");
String code = codeElement == null ? null : codeElement.getAsString();
String message = jsonObject.get("message").getAsString();
WxPayException wxPayException = new WxPayException(message);
wxPayException.setErrCode(code);
wxPayException.setErrCodeDes(message);
return wxPayException;
}
}