forked from TencentBlueKing/bk-job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: 补全file-gateway与file-worker对未知异常的处理 TencentBlueKing#2363
- Loading branch information
Showing
8 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...java/com/tencent/bk/job/common/web/exception/handler/RemoteExceptionControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.tencent.bk.job.common.web.exception.handler; | ||
|
||
import com.tencent.bk.job.common.annotation.RemoteAPI; | ||
import com.tencent.bk.job.common.constant.ErrorCode; | ||
import com.tencent.bk.job.common.esb.model.EsbResp; | ||
import com.tencent.bk.job.common.exception.InvalidParamException; | ||
import com.tencent.bk.job.common.util.I18nUtil; | ||
import com.tencent.bk.sdk.iam.constants.CommonResponseCode; | ||
import com.tencent.bk.sdk.iam.dto.callback.response.CallbackBaseResponseDTO; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* 处理FileWorker上报接口异常 | ||
*/ | ||
@ControllerAdvice(annotations = {RemoteAPI.class}) | ||
@Slf4j | ||
public class RemoteExceptionControllerAdvice extends ExceptionControllerAdviceBase { | ||
|
||
@ExceptionHandler(Throwable.class) | ||
@ResponseBody | ||
ResponseEntity<?> handleException(HttpServletRequest request, Throwable ex) { | ||
log.error("Handle Throwable", ex); | ||
CallbackBaseResponseDTO responseDTO = new CallbackBaseResponseDTO(); | ||
responseDTO.setCode(CommonResponseCode.SYSTEM_ERROR); | ||
responseDTO.setMessage(I18nUtil.getI18nMessage(String.valueOf(ErrorCode.INTERNAL_ERROR))); | ||
return new ResponseEntity<>(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@ExceptionHandler(InvalidParamException.class) | ||
@ResponseBody | ||
ResponseEntity<?> handleServiceException(HttpServletRequest request, InvalidParamException ex) { | ||
log.info("Handle InvalidParamException", ex); | ||
CallbackBaseResponseDTO responseDTO = new CallbackBaseResponseDTO(); | ||
responseDTO.setCode(CommonResponseCode.PARAMS_INVALID); | ||
responseDTO.setMessage(ex.getI18nMessage()); | ||
return new ResponseEntity<>(EsbResp.buildCommonFailResp(ex), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/RemoteAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. | ||
* | ||
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. | ||
* | ||
* License for BK-JOB蓝鲸智云作业平台: | ||
* -------------------------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
package com.tencent.bk.job.common.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface RemoteAPI { | ||
} |
36 changes: 36 additions & 0 deletions
36
src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/WorkerAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. | ||
* | ||
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. | ||
* | ||
* License for BK-JOB蓝鲸智云作业平台: | ||
* -------------------------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
package com.tencent.bk.job.common.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface WorkerAPI { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ava/com/tencent/bk/job/file/worker/exception/handler/WorkerExceptionControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.tencent.bk.job.file.worker.exception.handler; | ||
|
||
import com.tencent.bk.job.common.annotation.RemoteAPI; | ||
import com.tencent.bk.job.common.constant.ErrorCode; | ||
import com.tencent.bk.job.common.model.InternalResponse; | ||
import com.tencent.bk.job.common.model.error.ErrorType; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* 处理Worker接口异常 | ||
*/ | ||
@ControllerAdvice(annotations = {RemoteAPI.class}) | ||
@Slf4j | ||
public class WorkerExceptionControllerAdvice extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(Throwable.class) | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
InternalResponse<?> handleException(HttpServletRequest request, Throwable ex) { | ||
String exceptionInfo = "Handle Exception, uri: " + request.getRequestURI(); | ||
log.error(exceptionInfo, ex); | ||
return InternalResponse.buildCommonFailResp(ErrorType.INTERNAL, ErrorCode.INTERNAL_ERROR); | ||
} | ||
|
||
} |