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 11, 2023
1 parent 7856749 commit f5106a2
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import javax.validation.constraints.NotEmpty
@ApiModel("编译加速模式请求数据模型")
data class TurboEngineConfigModel(
@ApiModelProperty("编译加速模式代码")
@get:NotBlank(message = "模板代码不能为空", groups = [TurboEngineConfigGroup.Create::class])
@get:NotBlank(message = "{bizError.engineCodeNotBlank}", groups = [TurboEngineConfigGroup.Create::class])
val engineCode: String?,
@ApiModelProperty("编译加速模式名字")
@get:NotBlank(message = "模板名字不能为空", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
@get:NotBlank(message = "{bizError.engineNameNotBlank}", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
val engineName: String?,
@ApiModelProperty("描述")
val desc: String?,
@ApiModelProperty("spel表达式")
@get:NotBlank(message = "计算表达式不能为空", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
@get:NotBlank(message = "{bizError.expressionNotBlank}", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
val spelExpression: String?,
@ApiModelProperty("spel参数映射")
val spelParamMap: Map<String, Any?>?,
Expand All @@ -28,7 +28,7 @@ data class TurboEngineConfigModel(
val userManual: String? = null,
@ApiModelProperty("文档链接指引")
val docUrl: String? = null,
@get:NotEmpty(message = "编译加速历史显示字段值不能为空", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
@get:NotEmpty(message = "{bizError.displayFieldsNotEmpty}", groups = [TurboEngineConfigGroup.Create::class, TurboEngineConfigGroup.Update::class])
@ApiModelProperty("编译加速历史显示字段值")
val displayFields: List<TurboDisplayFieldModel>? = null,
@ApiModelProperty("是否推荐")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.validation.constraints.NotBlank
data class TurboEngineConfigPriorityModel(
@ApiModelProperty("引擎代码")
@get:NotBlank(
message = "{bizError.engineCodeNotBlank}"
message = "{bizError.engineCodeNotSelect}"
)
val engineCode: String,
@ApiModelProperty("优先级序号")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import javax.validation.constraints.NotBlank
@ApiModel("运行编译加速插件时入参")
data class TurboPlanInstanceModel(
@ApiModelProperty("项目id")
@get:NotBlank(message = "项目id不能为空")
@get:NotBlank(message = "{bizError.projectIdNotBlank}")
val projectId: String?,
@ApiModelProperty("编译加速方案id")
@get:NotBlank(message = "编译加速方案id不能为空")
@get:NotBlank(message = "{bizError.turboPlanIdNotBlank}")
val turboPlanId: String?,
@ApiModelProperty("流水线id")
@get:NotBlank(message = "流水线id不能为空")
@get:NotBlank(message = "{bizError.pipelineIdNotBlank}")
val pipelineId: String?,
@ApiModelProperty("流水线元素id")
@get:NotBlank(message = "流水线元素id不能为空")
@get:NotBlank(message = "{bizError.pipelineElementIdNotBlank}")
val pipelineElementId: String?,
@ApiModelProperty("流水线名称")
val pipelineName: String? = null,
@ApiModelProperty("流水线构建id")
@get:NotBlank(message = "流水线构建id不能为空")
@get:NotBlank(message = "{bizError.buildIdNotBlank}")
val buildId: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull
data class TurboPlanModel(
@ApiModelProperty("蓝盾项目id")
@get:NotBlank(
message = "项目id不能为空",
message = "{bizError.projectIdNotBlank}",
groups = [
TurboPlanGroup.Create::class,
TurboPlanGroup.UpdateDetail::class
Expand All @@ -20,7 +20,7 @@ data class TurboPlanModel(
val projectId: String?,
@ApiModelProperty("加速方案名称")
@get:NotBlank(
message = "方案名称不能为空",
message = "{bizError.planNameNotBlank}",
groups = [
TurboPlanGroup.Create::class,
TurboPlanGroup.UpdateDetail::class,
Expand All @@ -30,7 +30,7 @@ data class TurboPlanModel(
val planName: String?,
@ApiModelProperty("蓝盾模板代码")
@get:NotBlank(
message = "{bizError.engineCodeNotBlank}",
message = "{bizError.engineCodeNotSelect}",
groups = [
TurboPlanGroup.Create::class,
TurboPlanGroup.UpdateWhiteList::class,
Expand All @@ -42,14 +42,14 @@ data class TurboPlanModel(
val desc: String?,
@ApiModelProperty("配置参数值")
@get:NotNull(
message = "参数不能为空",
message = "{bizError.configParamNotNull}",
groups = [
TurboPlanGroup.UpdateParam::class,
TurboPlanGroup.UpdateAll::class
]
)
@get:NotEmpty(
message = "参数不能为空",
message = "{bizError.configParamNotEmpty}",
groups = [
TurboPlanGroup.UpdateParam::class,
TurboPlanGroup.UpdateAll::class
Expand All @@ -58,15 +58,15 @@ data class TurboPlanModel(
val configParam: Map<String, Any>?,
@ApiModelProperty("白名单")
@get:NotBlank(
message = "白名单不能为空",
message = "{bizError.whiteListNotBlank}",
groups = [
TurboPlanGroup.UpdateWhiteList::class
]
)
val whiteList: String?,
@ApiModelProperty("开启状态")
@get:NotNull(
message = "状态不能为空",
message = "{bizError.openStatusNotNull}",
groups = [
TurboPlanGroup.UpdateDetail::class,
TurboPlanGroup.UpdateAll::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import javax.validation.constraints.NotBlank
data class TurboRecordModel(
@ApiModelProperty("项目id")
@get:NotBlank(
message = "项目id不能为空",
message = "{bizError.projectIdNotBlank}",
groups = [
TurboRecordGroup.OpenApi::class
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,16 @@ class TurboEngineConfigService @Autowired constructor(
with(turboEnginConfigEntity) {
TurboEngineConfigVO(
engineCode = engineCode,
engineName = I18NUtil.getMessage("$engineCode.engineName") ?: engineName,
engineName = I18NUtil.getMessage("$engineCode.engineName"),
priorityNum = priorityNum,
desc = I18NUtil.getMessage("$engineCode.desc") ?: desc,
desc = I18NUtil.getMessage("$engineCode.desc"),
spelExpression = spelExpression,
spelParamMap = spelParamMap,
enabled = enabled,
userManual = translateEngineUserManual(engineCode, userManual ?: ""),
docUrl = docUrl,
recommend = recommend,
recommendReason = I18NUtil.getMessage("$engineCode.recommendReason") ?: recommendReason,
recommendReason = I18NUtil.getMessage("$engineCode.recommendReason"),
pluginTips = pluginTips,
paramConfig = paramConfig?.map {
ParamConfigModel(
Expand Down Expand Up @@ -573,15 +573,14 @@ class TurboEngineConfigService @Autowired constructor(
return turboEngineConfigList.map {
TurboEngineConfigVO(
engineCode = it.engineCode,
engineName = I18NUtil.getMessage("${it.engineCode}.engineName") ?: it.engineName,
engineName = I18NUtil.getMessage("${it.engineCode}.engineName"),
priorityNum = it.priorityNum,
userManual = translateEngineUserManual(it.engineCode, it.userManual ?: ""),
desc = I18NUtil.getMessage("${it.engineCode}.desc") ?: it.desc,
desc = I18NUtil.getMessage("${it.engineCode}.desc"),
paramConfig = it.paramConfig?.filter { param -> param.displayed }?.map { param ->
ParamConfigModel(
paramKey = param.paramKey,
paramName = I18NUtil.getMessage("${it.engineCode}.paramConfig.${param.paramKey}.paramName")
?: param.paramName,
paramName = I18NUtil.getMessage("${it.engineCode}.paramConfig.${param.paramKey}.paramName"),
paramType = param.paramType,
paramProps = param.paramProps,
paramEnum = param.paramEnum?.filter { paramEnumEntity ->
Expand All @@ -591,11 +590,11 @@ class TurboEngineConfigService @Autowired constructor(
ParamEnumModel(
paramValue = paramEnumEntity.paramValue,
paramName = I18NUtil.getMessage("${it.engineCode}.paramConfig.${param.paramKey
}.paramEnum.${paramEnumEntity.paramValue}") ?: paramEnumEntity.paramName,
}.paramEnum.${paramEnumEntity.paramValue}"),
visualRange = paramEnumEntity.visualRange
)
},
tips = param.tips,
tips = I18NUtil.getMessage("${it.engineCode}.paramConfig.${param.paramKey}.tips"),
displayed = param.displayed,
defaultValue = param.defaultValue,
required = param.required,
Expand All @@ -604,7 +603,7 @@ class TurboEngineConfigService @Autowired constructor(
)
},
recommend = it.recommend,
recommendReason = I18NUtil.getMessage("${it.engineCode}.recommendReason") ?: it.recommendReason,
recommendReason = I18NUtil.getMessage("${it.engineCode}.recommendReason"),
pluginTips = it.pluginTips,
updatedBy = it.updatedBy,
updatedDate = it.updatedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ class TurboPlanService @Autowired constructor(
} catch (e: Exception) {
e.printStackTrace()
logger.info("submit tbs data fail! error message: ${e.message}")
throw TurboException(errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL, errorMessage = "同步数据至加速后端失败")
throw TurboException(
errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL,
errorMessage = I18NUtil.getMessage("syncDataFail")
)
}
}

Expand Down Expand Up @@ -363,7 +366,10 @@ class TurboPlanService @Autowired constructor(
jsonBody = JsonUtil.toJson(DistccRequestBody(user, tbsJsonMap))
)
} catch (e: Exception) {
throw TurboException(errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL, errorMessage = "同步数据至加速后端失败")
throw TurboException(
errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL,
errorMessage = I18NUtil.getMessage("syncDataFail")
)
}
return true
}
Expand Down Expand Up @@ -403,7 +409,10 @@ class TurboPlanService @Autowired constructor(
jsonBody = JsonUtil.toJson(DistccRequestBody(user, tbsJsonMap))
)
} catch (e: Exception) {
throw TurboException(errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL, errorMessage = "同步数据至加速后端失败")
throw TurboException(
errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL,
errorMessage = I18NUtil.getMessage("syncDataFail")
)
}

return true
Expand All @@ -430,7 +439,10 @@ class TurboPlanService @Autowired constructor(
)
)
} catch (e: Exception) {
throw TurboException(errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL, errorMessage = "同步数据至加速后端失败")
throw TurboException(
errorCode = TURBO_THIRDPARTY_SYSTEM_FAIL,
errorMessage = I18NUtil.getMessage("syncDataFail")
)
}
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,20 @@ taskStatus.finish=Finished
2121005=Unauthorized, please join the project first.
2121006=Subclass check error

bizError.engineCodeNotBlank=Please select the acceleration mode first!
bizError.engineCodeNotSelect=Please select the acceleration mode first!
bizError.engineCodeNotBlank=Acceleration mode code cannot be empty
bizError.engineNameNotBlank=Acceleration mode name cannot be empty
bizError.expressionNotBlank=The calculation expression cannot be empty
bizError.displayFieldsNotEmpty=Compilation acceleration history display field value cannot be empty
bizError.projectIdNotBlank=Project ID cannot be empty
bizError.turboPlanIdNotBlank=Compilation acceleration plan id cannot be empty
bizError.planNameNotBlank=Plan name cannot be empty
bizError.pipelineIdNotBlank=Pipeline id cannot be empty
bizError.pipelineElementIdNotBlank=Pipeline element id cannot be empty
bizError.buildIdNotBlank=Pipeline build id cannot be empty
bizError.configParamNotNull=Parameter cannot be empty
bizError.configParamNotEmpty=Parameter cannot be empty
bizError.whiteListNotBlank=Whitelist cannot be empty
bizError.openStatusNotNull=Status cannot be empty

syncDataFail=Failed to synchronize data to the turbo server backend
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,20 @@ taskStatus.finish=\u6784\u5EFA\u5B8C\u6210
2121005=\u65E0\u6743\u9650\uFF0C\u8BF7\u5148\u52A0\u5165\u9879\u76EE
2121006=\u5B50\u7C7B\u68C0\u67E5\u9519\u8BEF

bizError.engineCodeNotBlank=\u8BF7\u5148\u9009\u62E9\u52A0\u901F\u6A21\u5F0F\uFF01
bizError.engineCodeNotSelect=\u8BF7\u5148\u9009\u62E9\u52A0\u901F\u6A21\u5F0F\uFF01
bizError.engineCodeNotBlank=\u6A21\u677F\u4EE3\u7801\u4E0D\u80FD\u4E3A\u7A7A
bizError.engineNameNotBlank=\u6A21\u677F\u540D\u5B57\u4E0D\u80FD\u4E3A\u7A7A
bizError.expressionNotBlank=\u8BA1\u7B97\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A
bizError.displayFieldsNotEmpty=\u7F16\u8BD1\u52A0\u901F\u5386\u53F2\u663E\u793A\u5B57\u6BB5\u503C\u4E0D\u80FD\u4E3A\u7A7A
bizError.projectIdNotBlank=\u9879\u76EEid\u4E0D\u80FD\u4E3A\u7A7A
bizError.turboPlanIdNotBlank=\u7F16\u8BD1\u52A0\u901F\u65B9\u6848id\u4E0D\u80FD\u4E3A\u7A7A
bizError.planNameNotBlank=\u65B9\u6848\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A
bizError.pipelineIdNotBlank=\u6D41\u6C34\u7EBFid\u4E0D\u80FD\u4E3A\u7A7A
bizError.pipelineElementIdNotBlank=\u6D41\u6C34\u7EBF\u5143\u7D20id\u4E0D\u80FD\u4E3A\u7A7A
bizError.buildIdNotBlank=\u6D41\u6C34\u7EBF\u6784\u5EFAid\u4E0D\u80FD\u4E3A\u7A7A
bizError.configParamNotNull=\u53C2\u6570\u4E0D\u80FD\u4E3A\u7A7A
bizError.configParamNotEmpty=\u53C2\u6570\u4E0D\u80FD\u4E3A\u7A7A
bizError.whiteListNotBlank=\u767D\u540D\u5355\u4E0D\u80FD\u4E3A\u7A7A
bizError.openStatusNotNull=\u72B6\u6001\u4E0D\u80FD\u4E3A\u7A7A

syncDataFail=\u540C\u6B65\u6570\u636E\u81F3\u52A0\u901F\u540E\u7AEF\u5931\u8D25
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ class ExceptionControllerAdvice {
@ResponseBody
@ExceptionHandler(ClientException::class)
fun clientExceptionHandler(clientException: ClientException): Response<Void> {
return Response.fail(TURBO_THIRDPARTY_SYSTEM_FAIL.toInt(), I18NUtil.getMessage(TURBO_THIRDPARTY_SYSTEM_FAIL)!!)
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(), I18NUtil.getMessage(TURBO_GENERAL_SYSTEM_FAIL)!!)
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(), I18NUtil.getMessage(TURBO_THIRDPARTY_SYSTEM_FAIL)!!)
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(), I18NUtil.getMessage(turboException.errorCode) ?:
turboException.message.orEmpty())
return Response.fail(turboException.errorCode.toInt(), turboException.message
?: I18NUtil.getMessage(turboException.errorCode))
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
Expand Down Expand Up @@ -74,7 +74,7 @@ class ExceptionControllerAdvice {
fun jsonMappingExceptionHandler(jsonMappingException: JsonMappingException): Response<Void> {
return Response.fail(
TURBO_PARAM_INVALID.toInt(),
I18NUtil.getMessage(TURBO_PARAM_INVALID)!!
I18NUtil.getMessage(TURBO_PARAM_INVALID)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import java.util.Locale

object I18NUtil {

fun getMessage(code: String): String? {
fun getMessage(code: String): String {
return getMessage(code, null)
}

fun getMessage(code: String, args: Array<Any>?): String? {
fun getMessage(code: String, args: Array<Any>?): String {
return getMessage(code, args, LocaleContextHolder.getLocale())
}

fun getMessage(code: String, args: Array<Any>?, locale: Locale): String? {
fun getMessage(code: String, args: Array<Any>?, locale: Locale): String {
val messageSource = SpringContextUtil.getBean(MessageSource::class.java)
return try {
messageSource.getMessage(code, args, locale)
} catch (_: NoSuchMessageException) {
null
"i18n error"
}
}
}

0 comments on commit f5106a2

Please sign in to comment.