feat(webmvc): Add an international exception handling module

- Added exception-i18n module to handle international exceptions
- Reconstruct the original exception module and change it to exception-i18n
- Added WebMvcExceptionAutoConfiguration class to realize automatic configuration of international exceptions
- Added GlobalException and InternalException classes for global exception handling
- Added GlobalExceptionHandler class to realize international global exception handling
- Update the project structure and add necessary dependencies and configurations
This commit is contained in:
gewuyou 2025-05-31 22:42:04 +08:00
parent 567e7cc2b7
commit 78ca098488
12 changed files with 134 additions and 13 deletions

View File

@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary

View File

@ -0,0 +1,40 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Kotlin ###
.kotlin

View File

@ -0,0 +1,18 @@
plugins{
alias(libs.plugins.forgeboot.i18n.keygen)
alias(libs.plugins.kotlin.plugin.spring)
}
dependencies {
implementation(project(Modules.Core.EXTENSION))
api(project(Modules.I18n.STARTER))
api(project(Modules.TRACE.STARTER))
implementation(project(Modules.Webmvc.DTO))
compileOnly(libs.springBootStarter.validation)
compileOnly(libs.springBootStarter.web)
kapt(libs.springBoot.configuration.processor)
}
i18nKeyGen {
rootPackage.set("com.gewuyou.forgeboot.webmvc.extension.i18n")
readPath.set("src/main/resources/i18n/${project.name}")
level.set(3)
}

View File

@ -0,0 +1,59 @@
package com.gewuyou.forgeboot.webmvc.exception.i18n.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.i18n.config.entities.WebMvcExceptionI18nProperties
import com.gewuyou.forgeboot.webmvc.exception.i18n.handler.GlobalExceptionHandler
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
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 异常自动配置
*
* @since 2025-05-13 11:48:01
* @author gewuyou
*/
@EnableConfigurationProperties(WebMvcExceptionI18nProperties::class)
@Configuration
class WebMvcExceptionAutoConfiguration {
/**
*默认消息解析器
*
* @since 2025-05-03 16:21:43
* @author gewuyou
*/
@Bean
@Order(Int.MAX_VALUE)
@ConditionalOnMissingBean
fun defaultMessageResolver(): MessageResolver = MessageResolver { code, _ -> code }
/**
*默认请求ID提供商
*
* @since 2025-05-03 16:22:18
* @author gewuyou
*/
@Bean
@Order(Int.MAX_VALUE)
@ConditionalOnMissingBean
fun defaultRequestIdProvider(): RequestIdProvider = RequestIdProvider { "" }
@Bean
@ConditionalOnMissingBean
fun i18nGlobalExceptionHandler(
webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties,
messageResolver: MessageResolver,
requestIdProvider: RequestIdProvider,
): GlobalExceptionHandler {
log.info("本地化全局异常处理程序创建成功!")
return GlobalExceptionHandler(
webMvcExceptionI18nProperties,
messageResolver,
requestIdProvider
)
}
}

View File

@ -1,4 +1,4 @@
package com.gewuyou.forgeboot.webmvc.exception.config.entities
package com.gewuyou.forgeboot.webmvc.exception.i18n.config.entities
import com.gewuyou.forgeboot.webmvc.extension.i18n.I18nKeys
import org.springframework.boot.context.properties.ConfigurationProperties

View File

@ -1,4 +1,4 @@
package com.gewuyou.forgeboot.webmvc.exception.core
package com.gewuyou.forgeboot.webmvc.exception.i18n.core
import com.gewuyou.forgeboot.i18n.api.I18nResponseInformation
import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException
@ -10,7 +10,7 @@ import com.gewuyou.forgeboot.i18n.impl.exception.I18nBaseException
* @author gewuyou
* @since 2024-11-23 16:45:10
*/
open class I18nGlobalException : I18nBaseException {
open class GlobalException : I18nBaseException {
/**
* 构造函数初始化全局异常
*

View File

@ -9,7 +9,7 @@ import com.gewuyou.forgeboot.i18n.api.I18nInternalInformation
* @author gewuyou
* @since 2024-11-24 21:14:03
*/
open class I18nInternalException : RuntimeException {
open class InternalException : RuntimeException {
/**
* 错误信息
*/

View File

@ -1,13 +1,13 @@
package com.gewuyou.forgeboot.webmvc.exception.handler
package com.gewuyou.forgeboot.webmvc.exception.i18n.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 com.gewuyou.forgeboot.webmvc.exception.i18n.config.entities.WebMvcExceptionI18nProperties
import com.gewuyou.forgeboot.webmvc.exception.i18n.core.GlobalException
import com.gewuyou.forgeboot.webmvc.exception.core.InternalException
import jakarta.validation.ConstraintViolationException
@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
* @since 2024-04-13 上午12:22:18
*/
@RestControllerAdvice
class I18nGlobalExceptionHandler(
class GlobalExceptionHandler(
private val webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties,
private val messageResolver: MessageResolver,
private val requestIdProvider: RequestIdProvider,
@ -129,8 +129,8 @@ class I18nGlobalExceptionHandler(
* @return 返回的结果
* @since 2024/4/13 下午1:56
*/
@ExceptionHandler(I18nGlobalException::class)
fun handleGlobalException(e: I18nGlobalException): I18nResult<String> {
@ExceptionHandler(GlobalException::class)
fun handleGlobalException(e: GlobalException): I18nResult<String> {
return I18nResult.failure(
e.errorCode, e.errorI18nMessageCode,
null, e.errorI18nMessageArgs, messageResolver, requestIdProvider
@ -145,8 +145,8 @@ class I18nGlobalExceptionHandler(
* @param e 异常
* @return 返回的结果
*/
@ExceptionHandler(I18nInternalException::class)
fun handleGlobalException(e: I18nInternalException): I18nResult<String> {
@ExceptionHandler(InternalException::class)
fun handleGlobalException(e: InternalException): I18nResult<String> {
log.error("内部异常: 异常信息: {}", e.errorMessage, e)
e.i18nInternalInformation?.responseI8nMessageCode?.let {
log.error(

View File

@ -0,0 +1 @@
com.gewuyou.forgeboot.webmvc.exception.i18n.config.WebMvcExceptionAutoConfiguration