1
This commit is contained in:
@@ -293,10 +293,58 @@ public class JDProductService {
|
||||
String errorCode = response != null ? response.getCode() : "null";
|
||||
String errorMsg = response != null ? response.getMsg() : "null";
|
||||
Integer resultCode = response != null && response.getGetResult() != null ? response.getGetResult().getCode() : null;
|
||||
String resultMsg = response != null && response.getGetResult() != null ? response.getGetResult().getMsg() : null;
|
||||
log.error("礼金创建失败 - response.code={}, response.msg={}, result.code={}, result.msg={}, SKU={}, owner={}, amount={}, quantity={}",
|
||||
errorCode, errorMsg, resultCode, resultMsg, skuId, owner, amount, quantity);
|
||||
return null;
|
||||
|
||||
// 尝试解析response.getMsg()中的详细错误信息(JSON格式)
|
||||
String detailErrorMsg = errorMsg;
|
||||
Integer detailCode = resultCode;
|
||||
boolean parsedDetail = false;
|
||||
|
||||
try {
|
||||
if (errorMsg != null && errorMsg.startsWith("{") && errorMsg.contains("message")) {
|
||||
// 解析外层JSON
|
||||
JSONObject outerJson = JSON.parseObject(errorMsg);
|
||||
if (outerJson.containsKey("jd_union_open_coupon_gift_get_responce")) {
|
||||
JSONObject innerObj = outerJson.getJSONObject("jd_union_open_coupon_gift_get_responce");
|
||||
if (innerObj.containsKey("getResult")) {
|
||||
String getResultStr = innerObj.getString("getResult");
|
||||
// getResult是一个JSON字符串,需要再次解析
|
||||
if (getResultStr != null && getResultStr.startsWith("{")) {
|
||||
JSONObject resultJson = JSON.parseObject(getResultStr);
|
||||
if (resultJson.containsKey("message")) {
|
||||
detailErrorMsg = resultJson.getString("message");
|
||||
Integer parsedDetailCode = resultJson.getInteger("code");
|
||||
if (parsedDetailCode != null) {
|
||||
detailCode = parsedDetailCode;
|
||||
}
|
||||
parsedDetail = true;
|
||||
log.error("礼金创建失败 - 京东API错误: code={}, message={}, SKU={}, owner={}, amount={}, quantity={}",
|
||||
detailCode, detailErrorMsg, skuId, owner, amount, quantity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception parseEx) {
|
||||
// JSON解析失败,使用原始错误信息
|
||||
log.warn("解析错误信息失败,使用原始信息: {}", errorMsg);
|
||||
}
|
||||
|
||||
// 记录日志并抛出包含详细错误信息的异常
|
||||
if (!parsedDetail) {
|
||||
log.error("礼金创建失败 - response.code={}, response.msg={}, result.code={}, SKU={}, owner={}, amount={}, quantity={}",
|
||||
errorCode, errorMsg, resultCode, skuId, owner, amount, quantity);
|
||||
}
|
||||
|
||||
// 构造错误消息
|
||||
String finalErrorMsg;
|
||||
if (parsedDetail) {
|
||||
finalErrorMsg = String.format("礼金创建失败:%s (错误码:%d)", detailErrorMsg, detailCode);
|
||||
} else {
|
||||
finalErrorMsg = String.format("礼金创建失败:京东API返回错误 (response.code=%s, result.code=%s, msg=%s)",
|
||||
errorCode, resultCode != null ? resultCode : "null", detailErrorMsg);
|
||||
}
|
||||
|
||||
throw new Exception(finalErrorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user