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 Jan 12, 2023
1 parent 9ef666d commit fdc7f76
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 37 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 @@ -85,7 +86,6 @@
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.CmdbClientException;
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;
import com.tencent.bk.job.common.model.dto.ApplicationDTO;
Expand Down Expand Up @@ -264,10 +264,10 @@ public InstanceTopologyDTO getBizInstCompleteTopology(long bizId) {
return completeTopologyDTO;
}

public InstanceTopologyDTO getCachedBizInstCompleteTopology(long bizId) {
public InstanceTopologyDTO getCachedBizInstCompleteTopology(long bizId){
try {
return bizInstCompleteTopologyCache.get(bizId);
} catch (Throwable e) {
} catch (ExecutionException | UncheckedExecutionException e) {
throw new CmdbClientException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.esb.model.EsbResp;
import com.tencent.bk.job.common.exception.AlreadyExistsException;
import com.tencent.bk.job.common.exception.CmdbClientException;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.InvalidParamException;
Expand Down Expand Up @@ -193,15 +192,6 @@ ResponseEntity<?> handleResourceExhaustedException(HttpServletRequest request, R
return new ResponseEntity<>(EsbResp.buildCommonFailResp(ex), HttpStatus.OK);
}

@ExceptionHandler(CmdbClientException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
ResponseEntity<?> handleInternalException(HttpServletRequest request, CmdbClientException ex) {
String errorMsg = "Handle CmdbException, uri: " + request.getRequestURI();
log.error(errorMsg, ex);
return new ResponseEntity<>(EsbResp.buildCommonFailResp(ex.getErrorCode()), HttpStatus.OK);
}

@Override
@SuppressWarnings("all")
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.tencent.bk.job.common.annotation.InternalAPI;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.AlreadyExistsException;
import com.tencent.bk.job.common.exception.CmdbClientException;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.InvalidParamException;
Expand Down Expand Up @@ -174,13 +173,4 @@ ResponseEntity<?> handleResourceExhaustedException(HttpServletRequest request, R
return new ResponseEntity<>(InternalResponse.buildCommonFailResp(ex), HttpStatus.TOO_MANY_REQUESTS);
}

@ExceptionHandler(CmdbClientException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
InternalResponse<?> handleCmdbException(HttpServletRequest request, CmdbClientException ex) {
String errorMsg = "Handle CmdbException, uri: " + request.getRequestURI();
log.error(errorMsg, ex);
return InternalResponse.buildCommonFailResp(ex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.tencent.bk.job.common.annotation.WebAPI;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.AlreadyExistsException;
import com.tencent.bk.job.common.exception.CmdbClientException;
import com.tencent.bk.job.common.exception.FailedPreconditionException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.InvalidParamException;
Expand Down Expand Up @@ -184,13 +183,4 @@ ResponseEntity<?> handleResourceExhaustedException(HttpServletRequest request, R
return new ResponseEntity<>(Response.buildCommonFailResp(ex), HttpStatus.TOO_MANY_REQUESTS);
}

@ExceptionHandler(CmdbClientException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
Response<?> handleCmdbException(HttpServletRequest request, CmdbClientException ex) {
String errorMsg = "Handle CmdbException, uri: " + request.getRequestURI();
log.error(errorMsg, ex);
return Response.buildCommonFailResp(ex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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.exception.CmdbClientException;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.util.http.ExtHttpHelper;
import com.tencent.bk.job.common.util.http.HttpHelperFactory;
import com.tencent.bk.job.common.util.json.JsonMapper;
Expand Down Expand Up @@ -248,15 +248,15 @@ public <R> EsbResp<R> getEsbRespByReq(String method, String uri, EsbReq reqBody,
if (StringUtils.isBlank(respStr)) {
String errorMsg = method + " " + uri + ", error: " + "Response is blank";
log.error(errorMsg);
throw new CmdbClientException(errorMsg, ErrorCode.API_ERROR);
throw new InternalException(errorMsg, ErrorCode.API_ERROR);
} else {
log.debug("success|method={}|uri={}|reqStr={}|respStr={}", method, uri, reqStr, respStr);
}
EsbResp<R> esbResp = JSON_MAPPER.fromJson(respStr, typeReference);
if (esbResp == null) {
String errorMsg = method + " " + uri + ", error: " + "Response is blank after parse";
log.error(errorMsg);
throw new CmdbClientException(errorMsg, ErrorCode.API_ERROR);
throw new InternalException(errorMsg, ErrorCode.API_ERROR);
} else if (!esbResp.getResult()) {
log.warn(
"fail:esbResp code!=0|esbResp.requestId={}|esbResp.code={}|esbResp" +
Expand Down Expand Up @@ -284,7 +284,7 @@ public <R> EsbResp<R> getEsbRespByReq(String method, String uri, EsbReq reqBody,
+ "|reqStr=" + reqStr
+ "|respStr=" + respStr;
log.error(errorMsg, e);
throw new CmdbClientException("Fail to request esb api", e, ErrorCode.API_ERROR);
throw new InternalException("Fail to request esb api", e, ErrorCode.API_ERROR);
}
}
}

0 comments on commit fdc7f76

Please sign in to comment.