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

Conversation

liuliaozhong
Copy link
Collaborator

No description provided.

@liuliaozhong liuliaozhong requested a review from jsonwan January 12, 2023 07:03
} catch (ExecutionException e) {
throw new InternalException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
} catch (Throwable e) {
throw new CmdbClientException(e, ErrorCode.CMDB_API_DATA_ERROR, null);
Copy link
Collaborator

@jsonwan jsonwan Jan 12, 2023

Choose a reason for hiding this comment

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

这里异常捕获的范围太大了,会将真实的异常全部视为CMDB异常,比如因代码bug原因导致的空指针异常,只要把外部缓存层包装的内部cause异常放出来就可以了

Copy link
Collaborator

Choose a reason for hiding this comment

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

这里的问题没解决,还是会把所有的异常都转为Cmdb异常,有个e.getCause()可以用,老代码的逻辑不一定是合理的,可以批判性地思考下

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

确实,没考虑周全,Throwable是异常超类,会把所有异常都捕获,都当成CMDB异常

@ExceptionHandler(CmdbClientException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
ResponseEntity<?> handleInternalException(HttpServletRequest request, CmdbClientException ex) {
Copy link
Collaborator

@jsonwan jsonwan Jan 12, 2023

Choose a reason for hiding this comment

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

该方法可以省略,处理逻辑跟其父类是一样的,将来有特殊的处理逻辑时再添加

@@ -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.InternalException;
import com.tencent.bk.job.common.exception.CmdbClientException;
Copy link
Collaborator

@jsonwan jsonwan Jan 12, 2023

Choose a reason for hiding this comment

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

这个类是个抽象类,不止是调用CMDB的API,还可能是通过ESB调用其他系统,这个改成Cmdb专有异常是不对的,可以考虑改为EsbClientException

@@ -183,4 +184,13 @@ ResponseEntity<?> handleResourceExhaustedException(HttpServletRequest request, R
return new ResponseEntity<>(Response.buildCommonFailResp(ex), HttpStatus.TOO_MANY_REQUESTS);
}

@ExceptionHandler(CmdbClientException.class)
Copy link
Collaborator

@jsonwan jsonwan Jan 12, 2023

Choose a reason for hiding this comment

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

该方法可以省略,处理逻辑跟其父类是一样的,将来有特殊的处理逻辑时再添加

@@ -173,4 +174,13 @@ ResponseEntity<?> handleResourceExhaustedException(HttpServletRequest request, R
return new ResponseEntity<>(InternalResponse.buildCommonFailResp(ex), HttpStatus.TOO_MANY_REQUESTS);
}

@ExceptionHandler(CmdbClientException.class)
Copy link
Collaborator

Choose a reason for hiding this comment

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

该方法可以省略,处理逻辑跟其父类是一样的,将来有特殊的处理逻辑时再添加

Copy link
Collaborator

@jsonwan jsonwan left a comment

Choose a reason for hiding this comment

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

3类问题需要处理下

Copy link
Collaborator

@jsonwan jsonwan left a comment

Choose a reason for hiding this comment

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

第1个问题需要再处理下

@liuliaozhong
Copy link
Collaborator Author

3类问题需要处理下

多处地方我都没考虑周全,犯低级错误,针对修复意见定好好修改

Copy link
Collaborator

@jsonwan jsonwan left a comment

Choose a reason for hiding this comment

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

待验证通过

} catch (ExecutionException | UncheckedExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) e;
Copy link
Collaborator

Choose a reason for hiding this comment

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

e应当改为cause

@@ -263,11 +264,16 @@ public InstanceTopologyDTO getBizInstCompleteTopology(long bizId) {
return completeTopologyDTO;
}

public InstanceTopologyDTO getCachedBizInstCompleteTopology(long bizId) {
public InstanceTopologyDTO getCachedBizInstCompleteTopology(long bizId){
Copy link
Collaborator

Choose a reason for hiding this comment

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

代码格式规范里面是有空格的,确认下提交前是否对代码进行了格式化,如果有,看下是idea的格式设置问题还是误删除了空格

Copy link
Collaborator

Choose a reason for hiding this comment

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

多名开发者之间的代码格式规范需要保持一致,否则容易引起不必要的代码改动和合并冲突

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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为什么会引入CMDB的异常,检查下是否格式化代码,去除无效引用

throw new PermissionDeniedException(authResult);
}
} catch (InternalIamException e) {
throw e;
Copy link
Collaborator

Choose a reason for hiding this comment

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

捕获异常后又立即抛出,是否是多余的代码?

*/
@Getter
@ToString
public class InternalUserInfoException extends InternalException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

UserInfo这个名称不太合适,这个异常的目的是要指出依赖的哪一个第三方系统出了问题,叫UserManage更合适

if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new InternalUserInfoException("Query userinfo from paas fail", e,
Copy link
Collaborator

Choose a reason for hiding this comment

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

逻辑不对,会将其他类型异常全部当做InternalUserInfoException,这里应该抛内部服务器错误的异常

String errorMsg = "Fail to load EsbChannel from cache";
logger.error(errorMsg, e);
throw new InternalException(errorMsg, e, ErrorCode.CMSI_MSG_CHANNEL_DATA_ERROR);
throw new InternalCmsiException(errorMsg, e, ErrorCode.CMSI_MSG_CHANNEL_DATA_ERROR);
Copy link
Collaborator

Choose a reason for hiding this comment

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

逻辑不对,会将其他类型异常全部当做InternalCmsiException,这里参考其他缓存异常的处理方式

if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new InternalCmdbException(e, ErrorCode.INTERNAL_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.

逻辑不对,会将其他类型异常全部当做InternalCmdbException,这里应该抛内部服务器错误的异常

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这样的话,原本CmdbException异常也变成内部服务器异常了
image
这样行不行呢?

Copy link
Collaborator

Choose a reason for hiding this comment

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

已在群内沟通

@@ -179,6 +183,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接口数据异常

Copy link
Collaborator

@jsonwan jsonwan left a comment

Choose a reason for hiding this comment

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

几个问题需要处理下

try {
return bizInstCompleteTopologyCache.get(bizId);
} catch (ExecutionException | UncheckedExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new InternalCmdbException(e, ErrorCode.INTERNAL_ERROR, null);
throw new InternalException(e, ErrorCode.CMDB_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.

errorCode应该是ErrorCode.INTERNAL_ERROR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

已修改

if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new InternalException(errorMsg, e, ErrorCode.CMSI_MSG_CHANNEL_DATA_ERROR);
Copy link
Collaborator

Choose a reason for hiding this comment

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

errorCode应该是ErrorCode.INTERNAL_ERROR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

已修改

Copy link
Collaborator

@jsonwan jsonwan left a comment

Choose a reason for hiding this comment

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

2处需要继续改下

@jsonwan jsonwan merged commit 36eb109 into TencentBlueKing:3.5.x Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants