Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: 依赖的其他系统(CMDB、IAM等)接口异常时报错信息优化 #1430 #1691

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -84,6 +85,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 @@ -266,8 +268,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 @@ -371,7 +378,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 @@ -761,7 +768,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 @@ -804,11 +811,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 @@ -43,7 +43,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.http.client.methods.HttpPost;
Expand Down Expand Up @@ -95,11 +95,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 @@ -143,11 +143,11 @@ private List<BizSetInfo> searchBizSet(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 @@ -173,11 +173,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 @@ -216,11 +216,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 @@ -256,11 +256,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 @@ -280,11 +280,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);
}
}
}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前IAM_API_DATA_ERROR对应的错误码信息不准确,改为:IAM接口数据异常

} 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 @@ -403,7 +403,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