From 8cb548b9ec54698b968ae61e471c588eb7108fbc Mon Sep 17 00:00:00 2001 From: RJ Date: Fri, 7 Jul 2023 17:47:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20turbo=E5=90=8E=E5=8F=B0=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=9B=BD=E9=99=85=E5=8C=96-=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E5=9B=BD=E9=99=85=E5=8C=96?= =?UTF-8?q?=20#86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../turbo/controller/ServiceTurboController.kt | 10 ++++------ .../controller/UserCustomScheduleTaskController.kt | 8 +++----- .../controller/UserTurboDaySummaryController.kt | 7 +++---- .../turbo/controller/UserTurboPlanController.kt | 14 ++++++-------- .../turbo/controller/UserTurboRecordController.kt | 7 +++---- .../src/main/resources/i18n/message_en.properties | 7 +++++++ .../main/resources/i18n/message_zh_CN.properties | 7 +++++++ .../api/exception/UnauthorizedErrorException.kt | 8 ++++++++ .../common/api/exception/code/TurboMessageCode.kt | 6 +++++- .../devops/common/util/constants/TurboConstants.kt | 5 ----- .../web/handler/ExceptionControllerAdvice.kt | 12 +++++++----- 11 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/UnauthorizedErrorException.kt diff --git a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/ServiceTurboController.kt b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/ServiceTurboController.kt index cc02e1df..0be5e3fc 100644 --- a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/ServiceTurboController.kt +++ b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/ServiceTurboController.kt @@ -1,10 +1,8 @@ package com.tencent.devops.turbo.controller import com.tencent.devops.api.pojo.Response -import com.tencent.devops.common.api.exception.TurboException -import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER +import com.tencent.devops.common.api.exception.UnauthorizedErrorException import com.tencent.devops.common.api.pojo.Page -import com.tencent.devops.common.util.constants.NO_ADMIN_MEMBER_MESSAGE import com.tencent.devops.turbo.api.IServiceTurboController import com.tencent.devops.turbo.pojo.TurboRecordModel import com.tencent.devops.turbo.service.TurboAuthService @@ -34,7 +32,7 @@ class ServiceTurboController @Autowired constructor( ): Response> { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, userId)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success( turboPlanService.getTurboPlanByProjectIdAndCreatedDate(projectId, startTime, endTime, pageNum, pageSize)) @@ -51,7 +49,7 @@ class ServiceTurboController @Autowired constructor( ): Response> { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, userId)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success( turboRecordService.getTurboRecordHistoryList(pageNum, pageSize, sortField, sortType, turboRecordModel)) @@ -64,7 +62,7 @@ class ServiceTurboController @Autowired constructor( ): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, userId)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.getTurboPlanDetailByPlanId(planId)) } diff --git a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserCustomScheduleTaskController.kt b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserCustomScheduleTaskController.kt index 1f1f3da9..663e6424 100644 --- a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserCustomScheduleTaskController.kt +++ b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserCustomScheduleTaskController.kt @@ -1,8 +1,6 @@ package com.tencent.devops.turbo.controller -import com.tencent.devops.common.api.exception.TurboException -import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER -import com.tencent.devops.common.util.constants.NO_ADMIN_MEMBER_MESSAGE +import com.tencent.devops.common.api.exception.UnauthorizedErrorException import com.tencent.devops.turbo.api.IUserCustomScheduleTaskController import com.tencent.devops.turbo.pojo.CustomScheduleJobModel import com.tencent.devops.turbo.service.CustomScheduleJobService @@ -23,14 +21,14 @@ class UserCustomScheduleTaskController @Autowired constructor( customScheduleJobModel: CustomScheduleJobModel ): Boolean { if (!turboAuthService.validatePlatformMember(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return customScheduleJobService.customScheduledJobAdd(customScheduleJobModel) } override fun triggerCustomScheduleJob(user: String, projectId: String, jobName: String): String? { if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return customScheduleJobService.trigger(jobName) } diff --git a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboDaySummaryController.kt b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboDaySummaryController.kt index 682b2e98..45f4b21d 100644 --- a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboDaySummaryController.kt +++ b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboDaySummaryController.kt @@ -3,7 +3,6 @@ package com.tencent.devops.turbo.controller import com.tencent.devops.api.pojo.Response import com.tencent.devops.common.api.exception.TurboException import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER -import com.tencent.devops.common.util.constants.NO_ADMIN_MEMBER_MESSAGE import com.tencent.devops.turbo.api.IUserTurboDaySummaryController import com.tencent.devops.turbo.service.TurboAuthService import com.tencent.devops.turbo.service.TurboSummaryService @@ -23,7 +22,7 @@ class UserTurboDaySummaryController @Autowired constructor( override fun getOverviewStatRowData(projectId: String, user: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = "") } return Response.success(turboSummaryService.getOverviewStatRowData(projectId)) } @@ -38,7 +37,7 @@ class UserTurboDaySummaryController @Autowired constructor( ): Response> { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = "") } return Response.success(turboSummaryService.getTimeConsumingTrendData(dateType, projectId)) } @@ -53,7 +52,7 @@ class UserTurboDaySummaryController @Autowired constructor( ): Response> { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = "") } return Response.success(turboSummaryService.getCompileNumberTrendData(dateType, projectId)) } diff --git a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboPlanController.kt b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboPlanController.kt index 9f15beb9..11fdfd3c 100644 --- a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboPlanController.kt +++ b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboPlanController.kt @@ -1,10 +1,8 @@ package com.tencent.devops.turbo.controller import com.tencent.devops.api.pojo.Response -import com.tencent.devops.common.api.exception.TurboException -import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER +import com.tencent.devops.common.api.exception.UnauthorizedErrorException import com.tencent.devops.common.api.pojo.Page -import com.tencent.devops.common.util.constants.NO_ADMIN_MEMBER_MESSAGE import com.tencent.devops.turbo.api.IUserTurboPlanController import com.tencent.devops.turbo.pojo.TurboPlanModel import com.tencent.devops.turbo.service.TurboAuthService @@ -25,7 +23,7 @@ class UserTurboPlanController @Autowired constructor( override fun addNewTurboPlan(turboPlanModel: TurboPlanModel, projectId: String, user: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.addNewTurboPlan(turboPlanModel, user)) } @@ -33,7 +31,7 @@ class UserTurboPlanController @Autowired constructor( override fun getTurboPlanStatRowData(projectId: String, pageNum: Int?, pageSize: Int?, user: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.getTurboPlanStatRowData(projectId, pageNum, pageSize)) } @@ -41,7 +39,7 @@ class UserTurboPlanController @Autowired constructor( override fun getTurboPlanDetailByPlanId(planId: String, projectId: String, user: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.getTurboPlanDetailByPlanId(planId)) } @@ -49,7 +47,7 @@ class UserTurboPlanController @Autowired constructor( override fun putTurboPlanDetailNameAndOpenStatus(turboPlanModel: TurboPlanModel, planId: String, user: String, projectId: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.putTurboPlanDetailNameAndOpenStatus(turboPlanModel, planId, user)) } @@ -57,7 +55,7 @@ class UserTurboPlanController @Autowired constructor( override fun putTurboPlanConfigParam(turboPlanModel: TurboPlanModel, planId: String, user: String, projectId: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboPlanService.putTurboPlanConfigParam(turboPlanModel, planId, user)) } diff --git a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboRecordController.kt b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboRecordController.kt index 0229840f..6d799c8b 100644 --- a/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboRecordController.kt +++ b/src/backend/turbo/biz-turbo/src/main/kotlin/com/tencent/devops/turbo/controller/UserTurboRecordController.kt @@ -2,10 +2,9 @@ package com.tencent.devops.turbo.controller import com.tencent.devops.api.pojo.Response import com.tencent.devops.common.api.exception.TurboException -import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER +import com.tencent.devops.common.api.exception.UnauthorizedErrorException import com.tencent.devops.common.api.exception.code.TURBO_PARAM_INVALID import com.tencent.devops.common.api.pojo.Page -import com.tencent.devops.common.util.constants.NO_ADMIN_MEMBER_MESSAGE import com.tencent.devops.common.web.utils.I18NUtil import com.tencent.devops.turbo.api.IUserTurboRecordController import com.tencent.devops.turbo.enums.EnumDistccTaskStatus @@ -45,7 +44,7 @@ class UserTurboRecordController @Autowired constructor( ): Response> { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } return Response.success(turboRecordService.getTurboRecordHistoryList(pageNum, pageSize, sortField, sortType, turboRecordModel)) } @@ -72,7 +71,7 @@ class UserTurboRecordController @Autowired constructor( override fun getTurboDisplayInfoById(turboRecordId: String, projectId: String, user: String): Response { // 判断是否是管理员 if (!turboAuthService.getAuthResult(projectId, user)) { - throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE) + throw UnauthorizedErrorException() } val turboRecordEntity = turboRecordService.findByRecordId(turboRecordId) diff --git a/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_en.properties b/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_en.properties index 47c41ab5..203ce07e 100644 --- a/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_en.properties +++ b/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_en.properties @@ -66,3 +66,10 @@ taskStatus.starting=Starting taskStatus.running=Running taskStatus.failed=Failed taskStatus.finish=Finish + +2121001=Internal server error +2121002=Failed to call the third-party interface. Please query the log +2121003=No data found, please check the parameters +2121004=Request parameter error +2121005=Unauthorized, please join the project first. +2121006=Subclass check error diff --git a/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_zh_CN.properties b/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_zh_CN.properties index 2b3a49eb..011f7d4b 100644 --- a/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_zh_CN.properties +++ b/src/backend/turbo/boot-turbo/src/main/resources/i18n/message_zh_CN.properties @@ -66,3 +66,10 @@ taskStatus.starting=正在开始 taskStatus.running=正在构建 taskStatus.failed=构建失败 taskStatus.finish=构建完成 + +2121001=内部服务错误 +2121002=调用第三方接口失败,请查询日志 +2121003=没有查询到数据,请检查参数 +2121004=请求参数异常 +2121005=无权限,请先加入项目 +2121006=子类检查错误 diff --git a/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/UnauthorizedErrorException.kt b/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/UnauthorizedErrorException.kt new file mode 100644 index 00000000..58016849 --- /dev/null +++ b/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/UnauthorizedErrorException.kt @@ -0,0 +1,8 @@ +package com.tencent.devops.common.api.exception + +import com.tencent.devops.common.api.exception.code.IS_NOT_ADMIN_MEMBER + +class UnauthorizedErrorException( + val errorCode: String = IS_NOT_ADMIN_MEMBER, + errorMessage: String = "" +) : RuntimeException(errorMessage) diff --git a/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/code/TurboMessageCode.kt b/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/code/TurboMessageCode.kt index ce18933c..015a0c42 100644 --- a/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/code/TurboMessageCode.kt +++ b/src/backend/turbo/common-turbo/common-turbo-api/src/main/kotlin/com/tencent/devops/common/api/exception/code/TurboMessageCode.kt @@ -1,8 +1,12 @@ package com.tencent.devops.common.api.exception.code +/** + * 如果需要自定义错误码,必须同步添加到i18n资源文件 + * 若想不到异常分类,且需要自定义error message,请使用通用异常(不建议) + */ const val TURBO_GENERAL_SYSTEM_FAIL = "2121001" // 通用异常 const val TURBO_THIRDPARTY_SYSTEM_FAIL = "2121002" // 调用第三方系统失败 const val TURBO_NO_DATA_FOUND = "2121003" // 没有查询到对应数据 const val TURBO_PARAM_INVALID = "2121004" // 参数异常 -const val IS_NOT_ADMIN_MEMBER = "2300017" // 非管理员,操作失败 +const val IS_NOT_ADMIN_MEMBER = "2121005" // 非管理员,操作失败 const val SUB_CLASS_CHECK_ERROR = "2121006" // 子类检查错误 diff --git a/src/backend/turbo/common-turbo/common-turbo-util/src/main/kotlin/com/tencent/devops/common/util/constants/TurboConstants.kt b/src/backend/turbo/common-turbo/common-turbo-util/src/main/kotlin/com/tencent/devops/common/util/constants/TurboConstants.kt index f76cae07..f2b2c039 100644 --- a/src/backend/turbo/common-turbo/common-turbo-util/src/main/kotlin/com/tencent/devops/common/util/constants/TurboConstants.kt +++ b/src/backend/turbo/common-turbo/common-turbo-util/src/main/kotlin/com/tencent/devops/common/util/constants/TurboConstants.kt @@ -4,8 +4,3 @@ package com.tencent.devops.common.util.constants * 系统默认管理员id */ const val SYSTEM_ADMIN = "Turbo" - -/** - * 无权限错误提示信息 - */ -const val NO_ADMIN_MEMBER_MESSAGE = "无权限,请先加入项目!" diff --git a/src/backend/turbo/common-turbo/common-turbo-web/src/main/kotlin/com/tencent/devops/common/web/handler/ExceptionControllerAdvice.kt b/src/backend/turbo/common-turbo/common-turbo-web/src/main/kotlin/com/tencent/devops/common/web/handler/ExceptionControllerAdvice.kt index 59fc9c83..39c7cc3a 100644 --- a/src/backend/turbo/common-turbo/common-turbo-web/src/main/kotlin/com/tencent/devops/common/web/handler/ExceptionControllerAdvice.kt +++ b/src/backend/turbo/common-turbo/common-turbo-web/src/main/kotlin/com/tencent/devops/common/web/handler/ExceptionControllerAdvice.kt @@ -9,6 +9,7 @@ import com.tencent.devops.common.api.exception.TurboException import com.tencent.devops.common.api.exception.code.TURBO_GENERAL_SYSTEM_FAIL import com.tencent.devops.common.api.exception.code.TURBO_PARAM_INVALID import com.tencent.devops.common.api.exception.code.TURBO_THIRDPARTY_SYSTEM_FAIL +import com.tencent.devops.common.web.utils.I18NUtil import org.springframework.http.HttpStatus import org.springframework.validation.BindException import org.springframework.web.bind.MethodArgumentNotValidException @@ -23,25 +24,26 @@ class ExceptionControllerAdvice { @ResponseBody @ExceptionHandler(ClientException::class) fun clientExceptionHandler(clientException: ClientException): Response { - return Response.fail(TURBO_THIRDPARTY_SYSTEM_FAIL.toInt(), "内部服务异常") + return Response.fail(TURBO_THIRDPARTY_SYSTEM_FAIL.toInt(), I18NUtil.getMessage(TURBO_THIRDPARTY_SYSTEM_FAIL)!!) } @ResponseBody @ExceptionHandler(OperationException::class) fun operationExceptionHandler(operationException: OperationException): Response { - return Response.fail(TURBO_GENERAL_SYSTEM_FAIL.toInt(), operationException.message!!) + return Response.fail(TURBO_GENERAL_SYSTEM_FAIL.toInt(), I18NUtil.getMessage(TURBO_GENERAL_SYSTEM_FAIL)!!) } @ResponseBody @ExceptionHandler(RemoteServiceException::class) fun remoteServiceExceptionHandler(remoteServiceException: RemoteServiceException): Response { - return Response.fail(TURBO_THIRDPARTY_SYSTEM_FAIL.toInt(), remoteServiceException.errorMessage) + return Response.fail(TURBO_THIRDPARTY_SYSTEM_FAIL.toInt(), I18NUtil.getMessage(TURBO_THIRDPARTY_SYSTEM_FAIL)!!) } @ResponseBody @ExceptionHandler(TurboException::class) fun turboExceptionHandler(turboException: TurboException): Response { - return Response.fail(turboException.errorCode.toInt(), turboException.message.orEmpty()) + return Response.fail(turboException.errorCode.toInt(), I18NUtil.getMessage(turboException.errorCode) ?: + turboException.message.orEmpty()) } @ResponseStatus(value = HttpStatus.BAD_REQUEST) @@ -72,7 +74,7 @@ class ExceptionControllerAdvice { fun jsonMappingExceptionHandler(jsonMappingException: JsonMappingException): Response { return Response.fail( TURBO_PARAM_INVALID.toInt(), - "请求参数错误" + I18NUtil.getMessage(TURBO_PARAM_INVALID)!! ) } }