|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.util.concurrent.Callable; |
|
|
import java.util.concurrent.Callable; |
|
|
|
|
|
|
|
|
|
|
|
import cn.com.taiji.common.web.util.WebTools; |
|
|
import cn.com.taiji.core.manager.cache.RedisCacheManager; |
|
|
import cn.com.taiji.core.manager.cache.RedisCacheManager; |
|
|
|
|
|
import cn.com.taiji.core.model.comm.protocol.*; |
|
|
import feign.RetryableException; |
|
|
import feign.RetryableException; |
|
|
import org.apache.http.NoHttpResponseException; |
|
|
import org.apache.http.NoHttpResponseException; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
|
import cn.com.taiji.common.validation.MyViolationException; |
|
|
import cn.com.taiji.common.validation.MyViolationException; |
|
|
import cn.com.taiji.core.manager.comm.client.IOFunction; |
|
|
import cn.com.taiji.core.manager.comm.client.IOFunction; |
|
|
import cn.com.taiji.core.manager.comm.client.feign.FeignClientManager; |
|
|
import cn.com.taiji.core.manager.comm.client.feign.FeignClientManager; |
|
|
import cn.com.taiji.core.model.comm.protocol.AbstractSignTypeRequest; |
|
|
|
|
|
import cn.com.taiji.core.model.comm.protocol.AbstractSignTypeResponse; |
|
|
|
|
|
import cn.com.taiji.core.model.comm.protocol.SignJsonResponse; |
|
|
|
|
|
import cn.com.taiji.core.model.comm.protocol.valid.FieldMsgBuilder; |
|
|
import cn.com.taiji.core.model.comm.protocol.valid.FieldMsgBuilder; |
|
|
import cn.com.taiji.core.model.comm.protocol.valid.GlyServiceError; |
|
|
import cn.com.taiji.core.model.comm.protocol.valid.GlyServiceError; |
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder; |
|
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
public abstract class AbstractCommManager extends RedisCacheManager { |
|
|
public abstract class AbstractCommManager extends RedisCacheManager { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = f.apply(req); |
|
|
res = f.apply(req); |
|
|
return res; |
|
|
return res; |
|
|
} catch (ApiRequestException e) { |
|
|
} catch (ApiRequestException e) { |
|
|
|
|
|
convertApiRequestException(e, req.getServiceCommand().getIfCode()); |
|
|
logger.error("", e); |
|
|
logger.error("", e); |
|
|
if (e.getErrCode() == FileProtocolSystemError.SIGN_FAILED.getCode()) { |
|
|
|
|
|
throw new ManagerException(e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
SignJsonResponse jsonRes = toJsonResponse(e); |
|
|
|
|
|
assert jsonRes != null; |
|
|
|
|
|
if (e.getErrCode() == GlyServiceError.FORMAT_VALIDATE_ERR.getCode()) { |
|
|
|
|
|
throw toFieldErrorException(jsonRes, FieldMsgConvertor.defaultConvertor);// 页面上会显示到字段旁边 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == GlyServiceError.BUSINESS_VALIDATE_ERR.getCode()) { |
|
|
|
|
|
logger.error("BUSINESS_ERROR:ifCode:{},reqId{} \n {}", req.getServiceCommand().getIfCode(), |
|
|
|
|
|
req.getReqId(), jsonRes); |
|
|
|
|
|
throw new ManagerException(jsonRes.getErrorMsg());// 业务校验出错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == FileProtocolSystemError.UNKNOWN_ERROR.getCode()) { |
|
|
|
|
|
logger.error("UNKNOWN_ERROR:ifCode:{},reqId{} \n {}", req.getServiceCommand().getIfCode(), |
|
|
|
|
|
req.getReqId(), jsonRes); |
|
|
|
|
|
throw new ManagerException("网络错误!");// 实际是服务端报错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == FileProtocolSystemError.IO_ERROR.getCode()) { |
|
|
|
|
|
logger.error("IO_ERROR:ifCode:{},reqId{}", req.getServiceCommand().getIfCode(), req.getReqId()); |
|
|
|
|
|
throw new ManagerException("网络错误...");// 实际是服务端报错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == 10000) {//中台接口失败 |
|
|
|
|
|
logger.error("中台接口失败:ifCode:{},reqId{}", req.getServiceCommand().getIfCode(), req.getReqId()); |
|
|
|
|
|
throw new ManagerException("网络错误。");// 实际是服务端报错 |
|
|
|
|
|
} |
|
|
|
|
|
throw new ManagerException("未知错误:" + jsonRes.getErrorMsg()); |
|
|
|
|
|
|
|
|
return null; |
|
|
} catch (NoHttpResponseException e) { |
|
|
} catch (NoHttpResponseException e) { |
|
|
logger.error("NoHttpResponseException:ifCode:{},reqId{}", req.getServiceCommand().getIfCode(), |
|
|
logger.error("NoHttpResponseException:ifCode:{},reqId{}", req.getServiceCommand().getIfCode(), |
|
|
req.getReqId(), e); |
|
|
req.getReqId(), e); |
|
|
|
|
|
|
|
|
throw new ManagerException("服务不可用异常!"); |
|
|
throw new ManagerException("服务不可用异常!"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int location = -1; |
|
|
|
|
|
private void convertApiRequestException(ApiRequestException e, String ifCode) throws ManagerException { |
|
|
|
|
|
String uri = getRequestUri(); |
|
|
|
|
|
logger.info("调用接口Api业务失败,Controller的url:{}", uri); |
|
|
|
|
|
StackTraceElement[] stackTrace = e.getStackTrace(); |
|
|
|
|
|
if (stackTrace != null) { |
|
|
|
|
|
if (location == -1) { |
|
|
|
|
|
location = getMethodLocation(stackTrace); |
|
|
|
|
|
} |
|
|
|
|
|
logger.info("调接口位置:{}", stackTrace[location]); |
|
|
|
|
|
} else { |
|
|
|
|
|
logger.error("", e); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == FileProtocolSystemError.SIGN_FAILED.getCode()) { |
|
|
|
|
|
throw new ManagerException(e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
SignJsonResponse jsonRes = toJsonResponse(e); |
|
|
|
|
|
assert jsonRes != null; |
|
|
|
|
|
if (e.getErrCode() == GlyServiceError.FORMAT_VALIDATE_ERR.getCode()) { |
|
|
|
|
|
logger.info("错误类别:FORMAT_ERROR(703),ifCode:{},\n响应内容:{}", ifCode, jsonRes); |
|
|
|
|
|
throw new FormatException(jsonRes.getErrorMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == GlyServiceError.BUSINESS_VALIDATE_ERR.getCode() || e.getErrCode() == 801) { |
|
|
|
|
|
logger.info("错误类别:BUSINESS_ERROR(704),ifCode:{},\n响应内容:{}", ifCode, jsonRes); |
|
|
|
|
|
throw new ManagerException(jsonRes.getErrorMsg());// 业务校验出错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == GlyServiceError.PERSIST_ERR.getCode() || e.getErrCode() == 801) { |
|
|
|
|
|
logger.info("错误类别:PERSIST_ERR(705),ifCode:{},\n响应内容:{}", ifCode, jsonRes); |
|
|
|
|
|
throw new ManagerException(jsonRes.getErrorMsg());// 持久化出错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == 808) { |
|
|
|
|
|
logger.info("错误类别:TOKEN_INVALID(808),ifCode:{}\n响应内容:{}", ifCode, jsonRes); |
|
|
|
|
|
throw new TokenInvalidException(e.getMessage());// 实际是服务端报错 |
|
|
|
|
|
} |
|
|
|
|
|
if (e.getErrCode() == 996) {// 中台接口失败 |
|
|
|
|
|
logger.info("错误类别:中台内部失败(996),ifCode:{}\n响应内容:{}", ifCode, jsonRes); |
|
|
|
|
|
throw new ManagerException("网络错误。");// 实际是服务端报错 |
|
|
|
|
|
} |
|
|
|
|
|
logger.info("错误类别:未知错误,errorCode:{},ifCode:{}\n响应内容:{}", e.getErrCode(), ifCode, jsonRes); |
|
|
|
|
|
throw new ManagerException("未知错误:" + jsonRes.getErrorMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
private String getRequestUri() { |
|
|
|
|
|
try { |
|
|
|
|
|
ServletRequestAttributes requestAttributes = |
|
|
|
|
|
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
|
|
|
|
assert requestAttributes != null; |
|
|
|
|
|
HttpServletRequest request = requestAttributes.getRequest(); |
|
|
|
|
|
return WebTools.getUri(request); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
private int getMethodLocation(StackTraceElement[] stackTrace) { |
|
|
|
|
|
for (int i = 0; i < stackTrace.length; i++) { |
|
|
|
|
|
if (!stackTrace[i].getClassName().contains("AbstractCommManager") |
|
|
|
|
|
&& !stackTrace[i].getClassName().contains("FeignClientManager")) { |
|
|
|
|
|
return i; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
/** |
|
|
/** |
|
|
* 当不想自己捕获异常时、当你不对异常做什么处理时可调用该方法 |
|
|
* 当不想自己捕获异常时、当你不对异常做什么处理时可调用该方法 |
|
|
* |
|
|
* |