feat(exception): Added support for internationalization exception handling

- Added the WebMvcExceptionI18nProperties class to configure internationalization-related properties
- Modify the WebMvcExceptionProperties class and add the enable property to control whether internationalization is enabled
- The I18nGlobalExceptionHandler class is added to handle internationalization exceptions
- Modify the GlobalExceptionHandler class to support non-internationalization exception handling
- The I18nGlobalException and I18nInternalException classes have been added for internationalization exceptions
- Modify the InternalException class to simplify the structure of the internal exception
This commit is contained in:
gewuyou 2025-05-30 14:21:26 +08:00
parent ba88f3e9d3
commit 439e00552e
9 changed files with 417 additions and 148 deletions

View File

@ -1,13 +1,18 @@
package com.gewuyou.forgeboot.webmvc.exception.config
import com.gewuyou.forgeboot.core.extension.log
import com.gewuyou.forgeboot.i18n.api.MessageResolver
import com.gewuyou.forgeboot.trace.api.RequestIdProvider
import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionI18nProperties
import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionProperties
import com.gewuyou.forgeboot.webmvc.exception.handler.GlobalExceptionHandler
import com.gewuyou.forgeboot.webmvc.exception.handler.I18nGlobalExceptionHandler
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.annotation.Order
/**
*Web MVC 异常自动配置
@ -15,7 +20,7 @@ import org.springframework.context.annotation.Configuration
* @since 2025-05-13 11:48:01
* @author gewuyou
*/
@EnableConfigurationProperties(WebMvcExceptionProperties::class)
@EnableConfigurationProperties(WebMvcExceptionI18nProperties::class, WebMvcExceptionProperties::class)
@Configuration
class WebMvcExceptionAutoConfiguration {
/**
@ -25,6 +30,7 @@ class WebMvcExceptionAutoConfiguration {
* @author gewuyou
*/
@Bean
@Order(Int.MAX_VALUE)
@ConditionalOnMissingBean
fun defaultMessageResolver(): MessageResolver = MessageResolver { code, _ -> code }
@ -35,20 +41,37 @@ class WebMvcExceptionAutoConfiguration {
* @author gewuyou
*/
@Bean
@Order(Int.MAX_VALUE)
@ConditionalOnMissingBean
fun defaultRequestIdProvider(): RequestIdProvider = RequestIdProvider { "" }
@Bean
@ConditionalOnMissingBean
fun globalExceptionHandler(
webMvcExceptionProperties: WebMvcExceptionProperties,
@ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "true")
fun i18nGlobalExceptionHandler(
webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties,
messageResolver: MessageResolver,
requestIdProvider: RequestIdProvider,
): GlobalExceptionHandler {
return GlobalExceptionHandler(
webMvcExceptionProperties,
): I18nGlobalExceptionHandler {
log.info("本地化全局异常处理程序创建成功!")
return I18nGlobalExceptionHandler(
webMvcExceptionI18nProperties,
messageResolver,
requestIdProvider
)
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "false")
fun i18nGlobalExceptionHandler(
webMvcExceptionProperties: WebMvcExceptionProperties,
requestIdProvider: RequestIdProvider,
): GlobalExceptionHandler {
log.info("全局异常处理程序创建成功!")
return GlobalExceptionHandler(
webMvcExceptionProperties,
requestIdProvider
)
}
}

View File

@ -0,0 +1,79 @@
package com.gewuyou.forgeboot.webmvc.exception.config.entities
import com.gewuyou.forgeboot.webmvc.extension.i18n.I18nKeys
import org.springframework.boot.context.properties.ConfigurationProperties
/**
* Web Mvc异常属性
*
* @author gewuyou
* @since 2025-05-13 11:06:46
*/
@ConfigurationProperties("forgeboot.webmvc.exception.i18n")
class WebMvcExceptionI18nProperties {
/**
* 设置其他通用外部异常的错误代码
*
* @param otherGeneralExternalExceptionErrorCode 其他通用外部异常的错误代码
*/
var otherGeneralExternalExceptionErrorCode: Int = 500
/**
* 设置其他通用外部异常的错误消息
*
* @param otherGeneralExternalExceptionErrorMessage 其他通用外部异常的错误消息
*/
var otherGeneralExternalExceptionErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.INVALID_SERVER_ERROR
/**
* 设置默认验证异常的错误代码
*
* @param defaultValidationExceptionErrorCode 默认验证异常的错误代码
*/
var defaultValidationExceptionErrorCode: Int = 400
/**
* 设置默认验证异常的错误消息
*
* @param defaultValidationExceptionErrorMessage 默认验证异常的错误消息
*/
var defaultValidationExceptionErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
/**
* 设置默认验证异常的字段错误消息
*
* @param defaultValidationExceptionFieldErrorMessage 默认验证异常的字段错误消息
*/
var defaultValidationExceptionFieldErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
/**
* 设置默认无效参数异常的错误代码
*
* @param defaultInvalidParameterErrorCode 默认无效参数异常的错误代码
*/
var defaultInvalidParameterErrorCode: Int = 400
/**
* 设置默认无效参数异常的错误消息
*
* @param defaultInvalidParameterErrorMessage 默认无效参数异常的错误消息
*/
var defaultInvalidParameterErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
/**
* 设置默认内部服务器错误异常的错误代码
*
* @param defaultInternalServerErrorCode 默认内部服务器错误异常的错误代码
*/
var defaultInternalServerErrorCode: Int = 500
/**
* 设置默认内部服务器错误异常的错误消息
*
* @param defaultInternalServerErrorMessage 默认内部服务器错误异常的错误消息
*/
var defaultInternalServerErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.INVALID_SERVER_ERROR
}

View File

@ -1,6 +1,5 @@
package com.gewuyou.forgeboot.webmvc.exception.config.entities
import com.gewuyou.forgeboot.webmvc.extension.i18n.I18nKeys
import org.springframework.boot.context.properties.ConfigurationProperties
/**
@ -9,8 +8,21 @@ import org.springframework.boot.context.properties.ConfigurationProperties
* @author gewuyou
* @since 2025-05-13 11:06:46
*/
@ConfigurationProperties("forgeboot.webmvc.exception")
@ConfigurationProperties("forgeboot.webmvc.exception.default")
class WebMvcExceptionProperties {
companion object {
const val ERROR_MESSAGE_ILLEGAL_PARAMETERS = "Illegal parameters, please check the input!"
const val ERROR_MESSAGE_INTERNAL_SERVER =
"If there is an internal execution error, please report the request ID of this request!"
}
/**
* 是否启用国际化
*
* @param enable 是否启用国际化
*/
var enable: Boolean = false
/**
* 设置其他通用外部异常的错误代码
*
@ -24,7 +36,8 @@ class WebMvcExceptionProperties {
*
* @param otherGeneralExternalExceptionErrorMessage 其他通用外部异常的错误消息
*/
var otherGeneralExternalExceptionErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.INVALID_SERVER_ERROR
var otherGeneralExternalExceptionErrorMessage: String =
ERROR_MESSAGE_INTERNAL_SERVER
/**
* 设置默认验证异常的错误代码
@ -39,14 +52,14 @@ class WebMvcExceptionProperties {
*
* @param defaultValidationExceptionErrorMessage 默认验证异常的错误消息
*/
var defaultValidationExceptionErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
var defaultValidationExceptionErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
/**
* 设置默认验证异常的字段错误消息
*
* @param defaultValidationExceptionFieldErrorMessage 默认验证异常的字段错误消息
*/
var defaultValidationExceptionFieldErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
var defaultValidationExceptionFieldErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
/**
* 设置默认无效参数异常的错误代码
@ -60,7 +73,7 @@ class WebMvcExceptionProperties {
*
* @param defaultInvalidParameterErrorMessage 默认无效参数异常的错误消息
*/
var defaultInvalidParameterErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.ILLEGAL_PARAMETER
var defaultInvalidParameterErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
/**
* 设置默认内部服务器错误异常的错误代码
@ -75,5 +88,6 @@ class WebMvcExceptionProperties {
*
* @param defaultInternalServerErrorMessage 默认内部服务器错误异常的错误消息
*/
var defaultInternalServerErrorMessage: String = I18nKeys.Forgeboot.Webmvc.Exception.INVALID_SERVER_ERROR
var defaultInternalServerErrorMessage: String =
ERROR_MESSAGE_INTERNAL_SERVER
}

View File

@ -1,28 +1,14 @@
package com.gewuyou.forgeboot.webmvc.exception.core
import com.gewuyou.forgeboot.i18n.api.ResponseInformation
import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException
import com.gewuyou.forgeboot.webmvc.dto.ResponseInformation
/**
* 全局异常: 继承I18nBaseException如果其它模块需要抛出全局异常则继承此类
*全局异常
*
* @since 2025-05-30 13:25:17
* @author gewuyou
* @since 2024-11-23 16:45:10
*/
open class GlobalException : I18nBaseException {
/**
* 构造函数初始化全局异常
*
* @param responseInformation 响应信息包含错误代码和消息
*/
constructor(responseInformation: ResponseInformation) : super(responseInformation)
/**
* 构造函数初始化全局异常并包含原始异常
*
* @param responseInformation 响应信息包含错误代码和消息
* @param cause 原始异常
*/
constructor(responseInformation: ResponseInformation, cause: Throwable) : super(responseInformation, cause)
}
open class GlobalException(
val responseInformation: ResponseInformation,
cause: Throwable? = null
): RuntimeException(responseInformation.responseMessage(), cause)

View File

@ -0,0 +1,28 @@
package com.gewuyou.forgeboot.webmvc.exception.core
import com.gewuyou.forgeboot.i18n.api.I18nResponseInformation
import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException
/**
* i18n全局异常: 继承I18nBaseException如果其它模块需要抛出全局异常则继承此类
*
* @author gewuyou
* @since 2024-11-23 16:45:10
*/
open class I18nGlobalException : I18nBaseException {
/**
* 构造函数初始化全局异常
*
* @param i18nResponseInformation 响应信息包含错误代码和消息
*/
constructor(i18nResponseInformation: I18nResponseInformation) : super(i18nResponseInformation)
/**
* 构造函数初始化全局异常并包含原始异常
*
* @param i18nResponseInformation 响应信息包含错误代码和消息
* @param cause 原始异常
*/
constructor(i18nResponseInformation: I18nResponseInformation, cause: Throwable) : super(i18nResponseInformation, cause)
}

View File

@ -0,0 +1,71 @@
package com.gewuyou.forgeboot.webmvc.exception.core
import com.gewuyou.forgeboot.i18n.api.I18nInternalInformation
/**
* 内部异常
*
* @author gewuyou
* @since 2024-11-24 21:14:03
*/
open class I18nInternalException : RuntimeException {
/**
* 错误信息
*/
val errorMessage: String
/**
* 可选(国际化内部错误信息码)
*/
@Transient
val i18nInternalInformation: I18nInternalInformation?
/**
* 构造一个新的运行时异常其详细消息为`null`
* 原因尚未初始化可以随后通过调用[.initCause]进行初始化
*
* @param errorMessage 详细消息
*/
constructor(errorMessage: String) {
this.errorMessage = errorMessage
this.i18nInternalInformation = null
}
/**
* 使用指定的详细消息和原因构造新的运行时异常
*
* @param errorMessage 详细消息
* @param cause 异常的原因
*/
constructor(errorMessage: String, cause: Throwable?) : super(errorMessage, cause) {
this.errorMessage = errorMessage
this.i18nInternalInformation = null
}
/**
* 使用指定的详细消息和国际化内部错误信息码构造新的运行时异常
*
* @param errorMessage 详细消息
* @param i18nInternalInformation 国际化内部错误信息码
*/
constructor(errorMessage: String, i18nInternalInformation: I18nInternalInformation?) {
this.errorMessage = errorMessage
this.i18nInternalInformation = i18nInternalInformation
}
/**
* 使用指定的详细消息原因和国际化内部错误信息码构造新的运行时异常
*GlobalExceptionHandler
* @param errorMessage 详细消息
* @param cause 异常的原因
* @param i18nInternalInformation 国际化内部错误信息码
*/
constructor(errorMessage: String, cause: Throwable?, i18nInternalInformation: I18nInternalInformation?) : super(
errorMessage,
cause
) {
this.errorMessage = errorMessage
this.i18nInternalInformation = i18nInternalInformation
}
}

View File

@ -1,71 +1,12 @@
package com.gewuyou.forgeboot.webmvc.exception.core
import com.gewuyou.forgeboot.i18n.api.InternalInformation
/**
*内部异常
*
* @since 2025-05-30 14:16:01
* @author gewuyou
* @since 2024-11-24 21:14:03
*/
open class InternalException : RuntimeException {
/**
* 错误信息
*/
val errorMessage: String
/**
* 可选(国际化内部错误信息码)
*/
@Transient
val internalInformation: InternalInformation?
/**
* 构造一个新的运行时异常其详细消息为`null`
* 原因尚未初始化可以随后通过调用[.initCause]进行初始化
*
* @param errorMessage 详细消息
*/
constructor(errorMessage: String) {
this.errorMessage = errorMessage
this.internalInformation = null
}
/**
* 使用指定的详细消息和原因构造新的运行时异常
*
* @param errorMessage 详细消息
* @param cause 异常的原因
*/
constructor(errorMessage: String, cause: Throwable?) : super(errorMessage, cause) {
this.errorMessage = errorMessage
this.internalInformation = null
}
/**
* 使用指定的详细消息和国际化内部错误信息码构造新的运行时异常
*
* @param errorMessage 详细消息
* @param internalInformation 国际化内部错误信息码
*/
constructor(errorMessage: String, internalInformation: InternalInformation?) {
this.errorMessage = errorMessage
this.internalInformation = internalInformation
}
/**
* 使用指定的详细消息原因和国际化内部错误信息码构造新的运行时异常
*GlobalExceptionHandler
* @param errorMessage 详细消息
* @param cause 异常的原因
* @param internalInformation 国际化内部错误信息码
*/
constructor(errorMessage: String, cause: Throwable?, internalInformation: InternalInformation?) : super(
errorMessage,
cause
) {
this.errorMessage = errorMessage
this.internalInformation = internalInformation
}
}
class InternalException(
message: String = "内部未知异常",
cause: Throwable? = null
) : RuntimeException(message, cause)

View File

@ -2,14 +2,10 @@ package com.gewuyou.forgeboot.webmvc.exception.handler
import com.gewuyou.forgeboot.core.extension.log
import com.gewuyou.forgeboot.i18n.api.MessageResolver
import com.gewuyou.forgeboot.trace.api.RequestIdProvider
import com.gewuyou.forgeboot.webmvc.dto.R
import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionProperties
import com.gewuyou.forgeboot.webmvc.exception.core.GlobalException
import com.gewuyou.forgeboot.webmvc.exception.core.InternalException
import jakarta.validation.ConstraintViolationException
import org.springframework.http.HttpStatus
import org.springframework.web.bind.MethodArgumentNotValidException
@ -28,7 +24,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
@RestControllerAdvice
class GlobalExceptionHandler(
private val webMvcExceptionProperties: WebMvcExceptionProperties,
private val messageResolver: MessageResolver,
private val requestIdProvider: RequestIdProvider,
) {
/**
@ -44,9 +39,9 @@ class GlobalExceptionHandler(
fun handleOtherException(e: Exception): R<String> {
log.error("other exception:", e)
return R.failure(
webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode,
webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode.toString(),
webMvcExceptionProperties.otherGeneralExternalExceptionErrorMessage,
null, null, messageResolver, requestIdProvider
null, requestIdProvider
)
}
@ -64,31 +59,25 @@ class GlobalExceptionHandler(
// 返回字段错误
for (fieldError in ex.bindingResult.fieldErrors) {
return R.failure(
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.value().toString(),
fieldError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionFieldErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
// 返回全局错误
for (objectError in ex.bindingResult.globalErrors) {
return R.failure(
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.value().toString(),
objectError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
return R.failure(
webMvcExceptionProperties.defaultValidationExceptionErrorCode,
webMvcExceptionProperties.defaultValidationExceptionErrorCode.toString(),
webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
@ -106,16 +95,14 @@ class GlobalExceptionHandler(
fun handleConstraintViolationException(ex: ConstraintViolationException): R<String> {
for (constraintViolation in ex.constraintViolations) {
return R.failure(
HttpStatus.BAD_REQUEST.value(), constraintViolation.message,
null, null, messageResolver, requestIdProvider
HttpStatus.BAD_REQUEST.value().toString(), constraintViolation.message,
null, requestIdProvider
)
}
return R.failure(
webMvcExceptionProperties.defaultInvalidParameterErrorCode,
webMvcExceptionProperties.defaultInvalidParameterErrorCode.toString(),
webMvcExceptionProperties.defaultInvalidParameterErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
@ -132,34 +119,9 @@ class GlobalExceptionHandler(
@ExceptionHandler(GlobalException::class)
fun handleGlobalException(e: GlobalException): R<String> {
return R.failure(
e.errorCode, e.errorI18nMessageCode,
null, e.errorI18nMessageArgs, messageResolver, requestIdProvider
)
}
/**
* 内部异常处理器
*
* 用于处理内部异常InternalException记录异常信息并返回默认的内部服务器错误信息
*
* @param e 异常
* @return 返回的结果
*/
@ExceptionHandler(InternalException::class)
fun handleGlobalException(e: InternalException): R<String> {
log.error("内部异常: 异常信息: {}", e.errorMessage, e)
e.internalInformation?.responseI8nMessageCode?.let {
log.error(
"i18nMessage: {}", messageResolver.resolve(it, e.internalInformation.responseI8nMessageArgs)
)
}
return R.failure(
webMvcExceptionProperties.defaultInternalServerErrorCode,
webMvcExceptionProperties.defaultInternalServerErrorMessage,
null,
null,
messageResolver,
requestIdProvider
e.responseInformation.responseStateCode(),
e.responseInformation.responseMessage(),
null, requestIdProvider
)
}
}

View File

@ -0,0 +1,165 @@
package com.gewuyou.forgeboot.webmvc.exception.handler
import com.gewuyou.forgeboot.core.extension.log
import com.gewuyou.forgeboot.i18n.api.MessageResolver
import com.gewuyou.forgeboot.trace.api.RequestIdProvider
import com.gewuyou.forgeboot.webmvc.dto.I18nResult
import com.gewuyou.forgeboot.webmvc.exception.config.entities.WebMvcExceptionI18nProperties
import com.gewuyou.forgeboot.webmvc.exception.core.I18nGlobalException
import com.gewuyou.forgeboot.webmvc.exception.core.I18nInternalException
import jakarta.validation.ConstraintViolationException
import org.springframework.http.HttpStatus
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
/**
* i18n全局异常处理类
*
* 该类用于统一处理整个应用中抛出的各种异常以提供统一的错误响应格式
* 它通过使用@RestControllerAdvice注解来标识这是一个全局异常处理类
*
* @author gewuyou
* @since 2024-04-13 上午12:22:18
*/
@RestControllerAdvice
class I18nGlobalExceptionHandler(
private val webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties,
private val messageResolver: MessageResolver,
private val requestIdProvider: RequestIdProvider,
) {
/**
* 异常处理器
*
* 用于处理除特定异常外的所有其他异常
*
* @param e 异常
* @return 响应
* @since 2024/4/13 上午12:29
*/
@ExceptionHandler(Exception::class)
fun handleOtherException(e: Exception): I18nResult<String> {
log.error("other exception:", e)
return I18nResult.failure(
webMvcExceptionI18nProperties.otherGeneralExternalExceptionErrorCode,
webMvcExceptionI18nProperties.otherGeneralExternalExceptionErrorMessage,
null, null, messageResolver, requestIdProvider
)
}
/**
* 处理 @Valid @Validated 校验失败抛出的 MethodArgumentNotValidException 异常
*
* 该方法首先尝试返回字段错误信息如果没有字段错误则尝试返回全局错误信息
* 如果都没有则返回默认的校验异常信息
*
* @param ex 异常
* @return 响应信息
*/
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidationExceptions(ex: MethodArgumentNotValidException): I18nResult<String> {
// 返回字段错误
for (fieldError in ex.bindingResult.fieldErrors) {
return I18nResult.failure(
HttpStatus.BAD_REQUEST.value(),
fieldError.defaultMessage ?: webMvcExceptionI18nProperties.defaultValidationExceptionFieldErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
// 返回全局错误
for (objectError in ex.bindingResult.globalErrors) {
return I18nResult.failure(
HttpStatus.BAD_REQUEST.value(),
objectError.defaultMessage ?: webMvcExceptionI18nProperties.defaultValidationExceptionErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
return I18nResult.failure(
webMvcExceptionI18nProperties.defaultValidationExceptionErrorCode,
webMvcExceptionI18nProperties.defaultValidationExceptionErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
/**
* 处理 JSR 303/JSR 380 校验失败抛出的 ConstraintViolationException 异常
*
* 该方法遍历约束违规信息并返回第一个错误信息如果存在多个错误
* 否则返回默认的无效参数错误信息
*
* @param ex 异常
* @return 响应信息
*/
@ExceptionHandler(ConstraintViolationException::class)
fun handleConstraintViolationException(ex: ConstraintViolationException): I18nResult<String> {
for (constraintViolation in ex.constraintViolations) {
return I18nResult.failure(
HttpStatus.BAD_REQUEST.value(), constraintViolation.message,
null, null, messageResolver, requestIdProvider
)
}
return I18nResult.failure(
webMvcExceptionI18nProperties.defaultInvalidParameterErrorCode,
webMvcExceptionI18nProperties.defaultInvalidParameterErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
/**
* 全局异常处理器
*
* 用于处理全局异常GlobalException返回错误代码和国际化消息代码
*
* @param e 异常
* @return 返回的结果
* @since 2024/4/13 下午1:56
*/
@ExceptionHandler(I18nGlobalException::class)
fun handleGlobalException(e: I18nGlobalException): I18nResult<String> {
return I18nResult.failure(
e.errorCode, e.errorI18nMessageCode,
null, e.errorI18nMessageArgs, messageResolver, requestIdProvider
)
}
/**
* 内部异常处理器
*
* 用于处理内部异常InternalException记录异常信息并返回默认的内部服务器错误信息
*
* @param e 异常
* @return 返回的结果
*/
@ExceptionHandler(I18nInternalException::class)
fun handleGlobalException(e: I18nInternalException): I18nResult<String> {
log.error("内部异常: 异常信息: {}", e.errorMessage, e)
e.i18nInternalInformation?.responseI8nMessageCode?.let {
log.error(
"i18nMessage: {}", messageResolver.resolve(it, e.i18nInternalInformation.responseI8nMessageArgs)
)
}
return I18nResult.failure(
webMvcExceptionI18nProperties.defaultInternalServerErrorCode,
webMvcExceptionI18nProperties.defaultInternalServerErrorMessage,
null,
null,
messageResolver,
requestIdProvider
)
}
}