Skip to content

Commit

Permalink
bugfix: 依赖的其他系统(CMDB、IAM等)接口异常时报错信息优化 TencentBlueKing#1430
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliaozhong committed Feb 2, 2023
1 parent 13e2312 commit 29e3980
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.tencent.bk.job.common.cc.config.CmdbConfig;
import com.tencent.bk.job.common.cc.exception.CmdbException;
import com.tencent.bk.job.common.cc.model.AppRoleDTO;
Expand Down Expand Up @@ -82,6 +83,7 @@
import com.tencent.bk.job.common.esb.model.EsbReq;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.esb.sdk.AbstractEsbSdkClient;
import com.tencent.bk.job.common.exception.InternalCmdbException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.gse.service.QueryAgentStatusClient;
import com.tencent.bk.job.common.metrics.CommonMetricNames;
Expand Down Expand Up @@ -235,8 +237,13 @@ public InstanceTopologyDTO getBizInstCompleteTopology(long bizId) {
public InstanceTopologyDTO getCachedBizInstCompleteTopology(long bizId) {
try {
return bizInstCompleteTopologyCache.get(bizId);
} catch (ExecutionException e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
} catch (ExecutionException | UncheckedExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new InternalException(e, ErrorCode.INTERNAL_ERROR, null);
}
}
}

Expand Down Expand Up @@ -340,7 +347,7 @@ public <R> EsbResp<R> requestCmdbApi(String method,
String errorMsg = "Fail to request CMDB data|method=" + method + "|uri=" + uri + "|reqStr=" + reqStr;
log.error(errorMsg, e);
status = "error";
throw new InternalException(e.getMessage(), e, ErrorCode.CMDB_API_DATA_ERROR);
throw new InternalCmdbException(e.getMessage(), e, ErrorCode.CMDB_API_DATA_ERROR);
} finally {
HttpMetricUtil.clearHttpMetric();
long end = System.nanoTime();
Expand Down Expand Up @@ -730,7 +737,7 @@ public List<ApplicationDTO> getAllBizApps() {
SearchAppResult data = esbResp.getData();
if (data == null) {
appList.clear();
throw new InternalException("Data is null", ErrorCode.CMDB_API_DATA_ERROR);
throw new InternalCmdbException("Data is null", ErrorCode.CMDB_API_DATA_ERROR);
}
List<BusinessInfoDTO> businessInfos = data.getInfo();
if (businessInfos != null && !businessInfos.isEmpty()) {
Expand Down Expand Up @@ -770,11 +777,11 @@ public ApplicationDTO getBizAppById(long bizId) {
});
SearchAppResult data = esbResp.getData();
if (data == null) {
throw new InternalException("data is null", ErrorCode.CMDB_API_DATA_ERROR);
throw new InternalCmdbException("data is null", ErrorCode.CMDB_API_DATA_ERROR);
}
List<BusinessInfoDTO> businessInfos = data.getInfo();
if (businessInfos == null || businessInfos.isEmpty()) {
throw new InternalException("data is null", ErrorCode.CMDB_API_DATA_ERROR);
throw new InternalCmdbException("data is null", ErrorCode.CMDB_API_DATA_ERROR);
}
return convertToAppInfo(businessInfos.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import com.tencent.bk.job.common.esb.model.EsbReq;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.esb.sdk.AbstractEsbSdkClient;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.InternalCmdbException;
import com.tencent.bk.job.common.util.http.HttpHelperFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -102,11 +102,11 @@ public int searchBizSetCount() {
new TypeReference<EsbResp<SearchBizSetResp>>() {
});
if (!resp.getResult()) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData().getCount();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand Down Expand Up @@ -153,11 +153,11 @@ private List<BizSetInfo> searchBizSet(BizSetFilter filter, int start, int limit)
new TypeReference<EsbResp<SearchBizSetResp>>() {
});
if (!resp.getResult() || resp.getData() == null) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData().getBizSetList();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand All @@ -183,11 +183,11 @@ private int searchBizCountInBusinessSet(long bizSetId) {
new TypeReference<EsbResp<SearchBizInBusinessSetResp>>() {
});
if (!resp.getResult()) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData().getCount();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand Down Expand Up @@ -226,11 +226,11 @@ private List<BizInfo> searchBizInBizSet(long bizSetId, int start, int limit) {
new TypeReference<EsbResp<SearchBizInBusinessSetResp>>() {
});
if (!resp.getResult() || resp.getData() == null) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData().getBizList();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand Down Expand Up @@ -266,11 +266,11 @@ public ResourceWatchResult<BizSetEventDetail> getBizSetEvents(Long startTime, St
},
HttpHelperFactory.getLongRetryableHttpHelper());
if (!resp.getResult()) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand All @@ -290,11 +290,11 @@ public ResourceWatchResult<BizSetRelationEventDetail> getBizSetRelationEvents(Lo
},
HttpHelperFactory.getLongRetryableHttpHelper());
if (!resp.getResult()) {
throw new InternalException(ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(ErrorCode.CMDB_API_DATA_ERROR, null);
}
return resp.getData();
} catch (Exception e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
throw new InternalCmdbException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
1214002=制品库中找不到节点:{0},请到制品库核实

#IAM接口返回数据结构异常
1215001=IAM接口返回数据结构异常
1215001=IAM接口数据异常

#第三方API请求错误
1216001=第三方API请求错误
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
1214002=制品库中找不到节点:{0},请到制品库核实

#IAM接口返回数据结构异常
1215001=IAM接口返回数据结构异常
1215001=IAM接口数据异常

#第三方API请求错误
1216001=第三方API请求错误
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
1214002=制品库中找不到节点:{0},请到制品库核实

#IAM接口返回数据结构异常
1215001=IAM接口返回数据结构异常
1215001=IAM接口数据异常

#第三方API请求错误
1216001=第三方API请求错误
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
package com.tencent.bk.job.common.iam.client;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.esb.model.EsbReq;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.esb.sdk.AbstractEsbSdkClient;
import com.tencent.bk.job.common.exception.InternalIamException;
import com.tencent.bk.job.common.iam.dto.AuthByPathReq;
import com.tencent.bk.job.common.iam.dto.BatchAuthByPathReq;
import com.tencent.bk.job.common.iam.dto.EsbIamAction;
Expand Down Expand Up @@ -179,6 +181,8 @@ private <R> EsbResp<R> requestIamApi(String method,
HttpMetricUtil.setHttpMetricName(CommonMetricNames.ESB_IAM_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", uri));
return getEsbRespByReq(method, uri, reqBody, typeReference);
} catch (Exception e) {
throw new InternalIamException(e, ErrorCode.IAM_API_DATA_ERROR, null);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package com.tencent.bk.job.common.iam.http;

import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InternalIamException;
import com.tencent.bk.job.common.metrics.CommonMetricNames;
import com.tencent.bk.job.common.util.http.ExtHttpHelper;
import com.tencent.bk.job.common.util.http.HttpHelperFactory;
Expand Down Expand Up @@ -58,6 +60,8 @@ public String doHttpGet(String uri) {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.IAM_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", uri));
return httpHelper.get(buildUrl(uri), buildAuthHeader());
} catch (Exception e) {
throw new InternalIamException(e, ErrorCode.IAM_API_DATA_ERROR, null);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand All @@ -71,7 +75,7 @@ public String doHttpPost(String uri, Object body) {
return httpHelper.post(buildUrl(uri), DEFAULT_CHARSET, JsonUtils.toJson(body), buildAuthHeader());
} catch (Exception e) {
log.error("Fail to request IAM", e);
return null;
throw new InternalIamException(e, ErrorCode.IAM_API_DATA_ERROR, null);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand All @@ -85,7 +89,7 @@ public String doHttpPut(String uri, Object body) {
return httpHelper.put(buildUrl(uri), DEFAULT_CHARSET, JsonUtils.toJson(body), buildAuthHeader());
} catch (Exception e) {
log.error("Fail to request IAM", e);
return null;
throw new InternalIamException(e, ErrorCode.IAM_API_DATA_ERROR, null);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand All @@ -97,6 +101,8 @@ public String doHttpDelete(String uri) {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.IAM_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", uri));
return httpHelper.delete(buildUrl(uri), buildAuthHeader());
} catch (Exception e) {
throw new InternalIamException(e, ErrorCode.IAM_API_DATA_ERROR, null);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public class ErrorCode {
// 制品库中找不到节点:{0},请到制品库核实
public static final int CAN_NOT_FIND_NODE_IN_ARTIFACTORY = 1214002;

// IAM接口返回数据结构异常- 一般是被网关防火墙重定向返回统一登录页面
// IAM接口数据异常- 一般是被网关防火墙重定向返回统一登录页面
public static final int IAM_API_DATA_ERROR = 1215001;

// 第三方API请求错误
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tencent.bk.job.common.exception;

import lombok.Getter;
import lombok.ToString;

/**
* 内部服务异常--调用CMDB异常
*/
@Getter
@ToString
public class InternalCmdbException extends InternalException {

public InternalCmdbException(Throwable cause, Integer errorCode, Object[] errorParams) {
super(cause, errorCode, errorParams);
}

public InternalCmdbException(String message, Throwable cause, Integer errorCode) {
super(message, cause, errorCode);
}

public InternalCmdbException(String message, Integer errorCode) {
super(message, errorCode);
}

public InternalCmdbException(Integer errorCode, Object[] errorParams) {
super(errorCode, errorParams);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tencent.bk.job.common.exception;

import lombok.Getter;
import lombok.ToString;

/**
* 内部服务异常--调用cmsi接口异常
*/
@Getter
@ToString
public class InternalCmsiException extends InternalException {

public InternalCmsiException(String message, Throwable cause, Integer errorCode) {
super(message, cause, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.tencent.bk.job.common.exception;

import lombok.Getter;
import lombok.ToString;

/**
* 内部服务异常--调用IAM异常
*/
@Getter
@ToString
public class InternalIamException extends InternalException {

public InternalIamException(Throwable cause, Integer errorCode, Object[] errorParams) {
super(cause, errorCode, errorParams);
}

public InternalIamException(String message, Integer errorCode) {
super(message, errorCode);
}

public InternalIamException(String message, Throwable cause, Integer errorCode) {
super(message, cause, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tencent.bk.job.common.exception;

import lombok.Getter;
import lombok.ToString;

/**
* 内部服务异常--用户管理异常
*/
@Getter
@ToString
public class InternalUserManageException extends InternalException {

public InternalUserManageException(String message, Throwable cause, Integer errorCode) {
super(message, cause, errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
package com.tencent.bk.job.common.paas.login;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.esb.model.EsbReq;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.esb.sdk.AbstractEsbSdkClient;
import com.tencent.bk.job.common.exception.InternalUserManageException;
import com.tencent.bk.job.common.metrics.CommonMetricNames;
import com.tencent.bk.job.common.model.dto.BkUserDTO;
import com.tencent.bk.job.common.paas.model.EsbUserDto;
Expand Down Expand Up @@ -80,6 +82,10 @@ private BkUserDTO getUserInfo(EsbReq esbReq) {
}
);
return convertToBkUserDTO(esbResp.getData());
} catch (Exception e) {
String errorMsg = "Get " + API_GET_USER_INFO + " error";
log.error(errorMsg, e);
throw new InternalUserManageException(errorMsg, e, ErrorCode.USER_MANAGE_API_ACCESS_ERROR);
} finally {
HttpMetricUtil.clearHttpMetric();
}
Expand Down
Loading

0 comments on commit 29e3980

Please sign in to comment.