diff --git a/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/exception/handler/RemoteExceptionControllerAdvice.java b/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/exception/handler/RemoteExceptionControllerAdvice.java new file mode 100644 index 0000000000..28182b1f8d --- /dev/null +++ b/src/backend/commons/common-web/src/main/java/com/tencent/bk/job/common/web/exception/handler/RemoteExceptionControllerAdvice.java @@ -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); + } + +} diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/RemoteAPI.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/RemoteAPI.java new file mode 100644 index 0000000000..fabe38428a --- /dev/null +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/RemoteAPI.java @@ -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 { +} diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/WorkerAPI.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/WorkerAPI.java new file mode 100644 index 0000000000..aaf854d943 --- /dev/null +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/annotation/WorkerAPI.java @@ -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 { +} diff --git a/src/backend/job-file-gateway/api-job-file-gateway-worker/src/main/java/com/tencent/bk/job/file_gateway/api/remote/RemoteFileWorkerResource.java b/src/backend/job-file-gateway/api-job-file-gateway-worker/src/main/java/com/tencent/bk/job/file_gateway/api/remote/RemoteFileWorkerResource.java index c705a45b40..d2e704757d 100644 --- a/src/backend/job-file-gateway/api-job-file-gateway-worker/src/main/java/com/tencent/bk/job/file_gateway/api/remote/RemoteFileWorkerResource.java +++ b/src/backend/job-file-gateway/api-job-file-gateway-worker/src/main/java/com/tencent/bk/job/file_gateway/api/remote/RemoteFileWorkerResource.java @@ -24,6 +24,7 @@ package com.tencent.bk.job.file_gateway.api.remote; +import com.tencent.bk.job.common.annotation.RemoteAPI; import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.file_gateway.model.req.inner.HeartBeatReq; import com.tencent.bk.job.file_gateway.model.req.inner.OffLineAndReDispatchReq; @@ -41,6 +42,7 @@ @Api(tags = {"job-file-gateway:remote:FileWorker"}) @RequestMapping("/remote/fileWorker") @RestController +@RemoteAPI public interface RemoteFileWorkerResource { @ApiOperation(value = "Worker心跳", produces = "application/json") diff --git a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileResource.java b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileResource.java index 6b42f0ad77..59f864ce14 100644 --- a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileResource.java +++ b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileResource.java @@ -24,7 +24,7 @@ package com.tencent.bk.job.file.worker.api; -import com.tencent.bk.job.common.annotation.InternalAPI; +import com.tencent.bk.job.common.annotation.WorkerAPI; import com.tencent.bk.job.common.model.InternalResponse; import com.tencent.bk.job.file.worker.model.req.BaseReq; import com.tencent.bk.job.file.worker.model.req.ExecuteActionReq; @@ -42,7 +42,7 @@ @Api(tags = {"job-file-worker:api:File"}) @RequestMapping("/worker/api/file") @RestController -@InternalAPI +@WorkerAPI public interface FileResource { @ApiOperation(value = "测试文件源是否可用", produces = "application/json") diff --git a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileTaskResource.java b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileTaskResource.java index fcec53d992..177a1b8054 100644 --- a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileTaskResource.java +++ b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/FileTaskResource.java @@ -24,7 +24,7 @@ package com.tencent.bk.job.file.worker.api; -import com.tencent.bk.job.common.annotation.InternalAPI; +import com.tencent.bk.job.common.annotation.WorkerAPI; import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.file.worker.model.req.ClearTaskFilesReq; import com.tencent.bk.job.file.worker.model.req.DownloadFilesTaskReq; @@ -40,7 +40,7 @@ @Api(tags = {"job-file-worker:api:FileTask"}) @RequestMapping("/worker/api/filetask") @RestController -@InternalAPI +@WorkerAPI public interface FileTaskResource { // 子路径与gateway转发请求子路径保持一致 diff --git a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/OpResource.java b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/OpResource.java index 6fbe32e996..fc492817be 100644 --- a/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/OpResource.java +++ b/src/backend/job-file-worker-sdk/api-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/api/OpResource.java @@ -24,7 +24,7 @@ package com.tencent.bk.job.file.worker.api; -import com.tencent.bk.job.common.annotation.InternalAPI; +import com.tencent.bk.job.common.annotation.WorkerAPI; import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.file.worker.model.req.WorkerOffLineReq; import io.swagger.annotations.Api; @@ -41,7 +41,7 @@ @Api(tags = {"job-file-worker:api:OP"}) @RequestMapping("/worker/api/op") @RestController -@InternalAPI +@WorkerAPI public interface OpResource { @ApiOperation(value = "Worker下线", produces = "application/json") diff --git a/src/backend/job-file-worker-sdk/service-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/exception/handler/WorkerExceptionControllerAdvice.java b/src/backend/job-file-worker-sdk/service-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/exception/handler/WorkerExceptionControllerAdvice.java new file mode 100644 index 0000000000..7d7b989c29 --- /dev/null +++ b/src/backend/job-file-worker-sdk/service-job-file-worker-sdk/src/main/java/com/tencent/bk/job/file/worker/exception/handler/WorkerExceptionControllerAdvice.java @@ -0,0 +1,33 @@ +package com.tencent.bk.job.file.worker.exception.handler; + +import com.tencent.bk.job.common.annotation.WorkerAPI; +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 = {WorkerAPI.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); + } + +}