Skip to content

Commit

Permalink
feat: turbo后台信息国际化-异常错误信息国际化 TencentBlueKing#86
Browse files Browse the repository at this point in the history
  • Loading branch information
eazence committed Jul 7, 2023
1 parent 6bf0225 commit 8cb548b
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,7 +32,7 @@ class ServiceTurboController @Autowired constructor(
): Response<Page<TurboPlanStatRowVO>> {
// 判断是否是管理员
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))
Expand All @@ -51,7 +49,7 @@ class ServiceTurboController @Autowired constructor(
): Response<Page<TurboRecordHistoryVO>> {
// 判断是否是管理员
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))
Expand All @@ -64,7 +62,7 @@ class ServiceTurboController @Autowired constructor(
): Response<TurboPlanDetailVO> {
// 判断是否是管理员
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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +22,7 @@ class UserTurboDaySummaryController @Autowired constructor(
override fun getOverviewStatRowData(projectId: String, user: String): Response<TurboOverviewStatRowVO> {
// 判断是否是管理员
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))
}
Expand All @@ -38,7 +37,7 @@ class UserTurboDaySummaryController @Autowired constructor(
): Response<List<TurboOverviewTrendVO>> {
// 判断是否是管理员
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))
}
Expand All @@ -53,7 +52,7 @@ class UserTurboDaySummaryController @Autowired constructor(
): Response<List<TurboOverviewTrendVO>> {
// 判断是否是管理员
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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,39 +23,39 @@ class UserTurboPlanController @Autowired constructor(
override fun addNewTurboPlan(turboPlanModel: TurboPlanModel, projectId: String, user: String): Response<String?> {
// 判断是否是管理员
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))
}

override fun getTurboPlanStatRowData(projectId: String, pageNum: Int?, pageSize: Int?, user: String): Response<TurboPlanPageVO> {
// 判断是否是管理员
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))
}

override fun getTurboPlanDetailByPlanId(planId: String, projectId: String, user: String): Response<TurboPlanDetailVO> {
// 判断是否是管理员
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))
}

override fun putTurboPlanDetailNameAndOpenStatus(turboPlanModel: TurboPlanModel, planId: String, user: String, projectId: String): Response<Boolean> {
// 判断是否是管理员
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))
}

override fun putTurboPlanConfigParam(turboPlanModel: TurboPlanModel, planId: String, user: String, projectId: String): Response<Boolean> {
// 判断是否是管理员
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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,7 +44,7 @@ class UserTurboRecordController @Autowired constructor(
): Response<Page<TurboRecordHistoryVO>> {
// 判断是否是管理员
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))
}
Expand All @@ -72,7 +71,7 @@ class UserTurboRecordController @Autowired constructor(
override fun getTurboDisplayInfoById(turboRecordId: String, projectId: String, user: String): Response<TurboRecordDisplayVO> {
// 判断是否是管理员
if (!turboAuthService.getAuthResult(projectId, user)) {
throw TurboException(errorCode = IS_NOT_ADMIN_MEMBER, errorMessage = NO_ADMIN_MEMBER_MESSAGE)
throw UnauthorizedErrorException()
}

val turboRecordEntity = turboRecordService.findByRecordId(turboRecordId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ taskStatus.starting=正在开始
taskStatus.running=正在构建
taskStatus.failed=构建失败
taskStatus.finish=构建完成

2121001=内部服务错误
2121002=调用第三方接口失败,请查询日志
2121003=没有查询到数据,请检查参数
2121004=请求参数异常
2121005=无权限,请先加入项目
2121006=子类检查错误
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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" // 子类检查错误
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ package com.tencent.devops.common.util.constants
* 系统默认管理员id
*/
const val SYSTEM_ADMIN = "Turbo"

/**
* 无权限错误提示信息
*/
const val NO_ADMIN_MEMBER_MESSAGE = "无权限,请先加入项目!"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,25 +24,26 @@ class ExceptionControllerAdvice {
@ResponseBody
@ExceptionHandler(ClientException::class)
fun clientExceptionHandler(clientException: ClientException): Response<Void> {
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<Void> {
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<Void> {
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<Void> {
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)
Expand Down Expand Up @@ -72,7 +74,7 @@ class ExceptionControllerAdvice {
fun jsonMappingExceptionHandler(jsonMappingException: JsonMappingException): Response<Void> {
return Response.fail(
TURBO_PARAM_INVALID.toInt(),
"请求参数错误"
I18NUtil.getMessage(TURBO_PARAM_INVALID)!!
)
}
}

0 comments on commit 8cb548b

Please sign in to comment.