Skip to content

Commit

Permalink
perf: 补全file-gateway与file-worker对未知异常的处理 TencentBlueKing#2363
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonwan committed Aug 23, 2023
1 parent 92e6f1b commit a265415
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 6 deletions.
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);
}

}
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 {
}
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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +40,7 @@
@Api(tags = {"job-file-worker:api:FileTask"})
@RequestMapping("/worker/api/filetask")
@RestController
@InternalAPI
@WorkerAPI
public interface FileTaskResource {

// 子路径与gateway转发请求子路径保持一致
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down
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.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);
}

}

0 comments on commit a265415

Please sign in to comment.