mirror of
https://github.moeyy.xyz/https://github.com/GeWuYou/forgeboot
synced 2025-10-28 04:51:08 +08:00
refactor(webmvc-exception):Refactoring the Web MVC exception handling module
- Removed international configuration and processing logic - Optimized error message copy - Simplified configuration property structure - Updated exception handling logic, unified error code and error message handling method - Added automatic configuration of modules
This commit is contained in:
parent
78ca098488
commit
7c914e23af
@ -1,18 +1,11 @@
|
|||||||
plugins{
|
plugins{
|
||||||
alias(libs.plugins.forgeboot.i18n.keygen)
|
|
||||||
alias(libs.plugins.kotlin.plugin.spring)
|
alias(libs.plugins.kotlin.plugin.spring)
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(project(Modules.Core.EXTENSION))
|
implementation(project(Modules.Core.EXTENSION))
|
||||||
api(project(Modules.I18n.STARTER))
|
|
||||||
api(project(Modules.TRACE.STARTER))
|
api(project(Modules.TRACE.STARTER))
|
||||||
implementation(project(Modules.Webmvc.DTO))
|
implementation(project(Modules.Webmvc.DTO))
|
||||||
compileOnly(libs.springBootStarter.validation)
|
compileOnly(libs.springBootStarter.validation)
|
||||||
compileOnly(libs.springBootStarter.web)
|
compileOnly(libs.springBootStarter.web)
|
||||||
kapt(libs.springBoot.configuration.processor)
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -3,12 +3,9 @@ package com.gewuyou.forgeboot.webmvc.exception.config
|
|||||||
import com.gewuyou.forgeboot.core.extension.log
|
import com.gewuyou.forgeboot.core.extension.log
|
||||||
import com.gewuyou.forgeboot.i18n.api.MessageResolver
|
import com.gewuyou.forgeboot.i18n.api.MessageResolver
|
||||||
import com.gewuyou.forgeboot.trace.api.RequestIdProvider
|
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.config.entities.WebMvcExceptionProperties
|
||||||
import com.gewuyou.forgeboot.webmvc.exception.handler.GlobalExceptionHandler
|
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.ConditionalOnMissingBean
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
@ -20,8 +17,8 @@ import org.springframework.core.annotation.Order
|
|||||||
* @since 2025-05-13 11:48:01
|
* @since 2025-05-13 11:48:01
|
||||||
* @author gewuyou
|
* @author gewuyou
|
||||||
*/
|
*/
|
||||||
@EnableConfigurationProperties(WebMvcExceptionI18nProperties::class, WebMvcExceptionProperties::class)
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@EnableConfigurationProperties(WebMvcExceptionProperties::class)
|
||||||
class WebMvcExceptionAutoConfiguration {
|
class WebMvcExceptionAutoConfiguration {
|
||||||
/**
|
/**
|
||||||
*默认消息解析器
|
*默认消息解析器
|
||||||
@ -47,31 +44,12 @@ class WebMvcExceptionAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "true")
|
fun globalExceptionHandler(
|
||||||
fun i18nGlobalExceptionHandler(
|
|
||||||
webMvcExceptionI18nProperties: WebMvcExceptionI18nProperties,
|
|
||||||
messageResolver: MessageResolver,
|
|
||||||
requestIdProvider: RequestIdProvider,
|
|
||||||
): I18nGlobalExceptionHandler {
|
|
||||||
log.info("本地化全局异常处理程序创建成功!")
|
|
||||||
return I18nGlobalExceptionHandler(
|
|
||||||
webMvcExceptionI18nProperties,
|
|
||||||
messageResolver,
|
|
||||||
requestIdProvider
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnProperty(name = ["forgeboot.webmvc.exception.i18n.enable"], havingValue = "false")
|
|
||||||
fun i18nGlobalExceptionHandler(
|
|
||||||
webMvcExceptionProperties: WebMvcExceptionProperties,
|
webMvcExceptionProperties: WebMvcExceptionProperties,
|
||||||
requestIdProvider: RequestIdProvider,
|
requestIdProvider: RequestIdProvider,
|
||||||
): GlobalExceptionHandler {
|
): GlobalExceptionHandler {
|
||||||
log.info("全局异常处理程序创建成功!")
|
log.info("Web MVC 异常处理器 已创建!")
|
||||||
return GlobalExceptionHandler(
|
return GlobalExceptionHandler(webMvcExceptionProperties, requestIdProvider)
|
||||||
webMvcExceptionProperties,
|
|
||||||
requestIdProvider
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -8,12 +8,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties
|
|||||||
* @author gewuyou
|
* @author gewuyou
|
||||||
* @since 2025-05-13 11:06:46
|
* @since 2025-05-13 11:06:46
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties("forgeboot.webmvc.exception.default")
|
@ConfigurationProperties("forgeboot.webmvc.exception")
|
||||||
class WebMvcExceptionProperties {
|
class WebMvcExceptionProperties {
|
||||||
companion object {
|
companion object {
|
||||||
const val ERROR_MESSAGE_ILLEGAL_PARAMETERS = "Illegal parameters, please check the input!"
|
const val ERROR_MESSAGE_INVALID_PARAMETERS = "Invalid parameters. Please verify your input."
|
||||||
const val ERROR_MESSAGE_INTERNAL_SERVER =
|
const val ERROR_MESSAGE_INTERNAL_SERVER = "Internal server error. Please report the request ID for troubleshooting."
|
||||||
"If there is an internal execution error, please report the request ID of this request!"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,14 +52,14 @@ class WebMvcExceptionProperties {
|
|||||||
*
|
*
|
||||||
* @param defaultValidationExceptionErrorMessage 默认验证异常的错误消息
|
* @param defaultValidationExceptionErrorMessage 默认验证异常的错误消息
|
||||||
*/
|
*/
|
||||||
var defaultValidationExceptionErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
|
var defaultValidationExceptionErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置默认验证异常的字段错误消息
|
* 设置默认验证异常的字段错误消息
|
||||||
*
|
*
|
||||||
* @param defaultValidationExceptionFieldErrorMessage 默认验证异常的字段错误消息
|
* @param defaultValidationExceptionFieldErrorMessage 默认验证异常的字段错误消息
|
||||||
*/
|
*/
|
||||||
var defaultValidationExceptionFieldErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
|
var defaultValidationExceptionFieldErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置默认无效参数异常的错误代码
|
* 设置默认无效参数异常的错误代码
|
||||||
@ -73,7 +73,7 @@ class WebMvcExceptionProperties {
|
|||||||
*
|
*
|
||||||
* @param defaultInvalidParameterErrorMessage 默认无效参数异常的错误消息
|
* @param defaultInvalidParameterErrorMessage 默认无效参数异常的错误消息
|
||||||
*/
|
*/
|
||||||
var defaultInvalidParameterErrorMessage: String = ERROR_MESSAGE_ILLEGAL_PARAMETERS
|
var defaultInvalidParameterErrorMessage: String = ERROR_MESSAGE_INVALID_PARAMETERS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置默认内部服务器错误异常的错误代码
|
* 设置默认内部服务器错误异常的错误代码
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class GlobalExceptionHandler(
|
|||||||
fun handleOtherException(e: Exception): R<String> {
|
fun handleOtherException(e: Exception): R<String> {
|
||||||
log.error("other exception:", e)
|
log.error("other exception:", e)
|
||||||
return R.failure(
|
return R.failure(
|
||||||
webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode.toString(),
|
webMvcExceptionProperties.otherGeneralExternalExceptionErrorCode,
|
||||||
webMvcExceptionProperties.otherGeneralExternalExceptionErrorMessage,
|
webMvcExceptionProperties.otherGeneralExternalExceptionErrorMessage,
|
||||||
null, requestIdProvider
|
null, requestIdProvider
|
||||||
)
|
)
|
||||||
@ -59,7 +59,7 @@ class GlobalExceptionHandler(
|
|||||||
// 返回字段错误
|
// 返回字段错误
|
||||||
for (fieldError in ex.bindingResult.fieldErrors) {
|
for (fieldError in ex.bindingResult.fieldErrors) {
|
||||||
return R.failure(
|
return R.failure(
|
||||||
HttpStatus.BAD_REQUEST.value().toString(),
|
HttpStatus.BAD_REQUEST.value(),
|
||||||
fieldError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionFieldErrorMessage,
|
fieldError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionFieldErrorMessage,
|
||||||
null,
|
null,
|
||||||
requestIdProvider
|
requestIdProvider
|
||||||
@ -68,14 +68,14 @@ class GlobalExceptionHandler(
|
|||||||
// 返回全局错误
|
// 返回全局错误
|
||||||
for (objectError in ex.bindingResult.globalErrors) {
|
for (objectError in ex.bindingResult.globalErrors) {
|
||||||
return R.failure(
|
return R.failure(
|
||||||
HttpStatus.BAD_REQUEST.value().toString(),
|
HttpStatus.BAD_REQUEST.value(),
|
||||||
objectError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
|
objectError.defaultMessage ?: webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
|
||||||
null,
|
null,
|
||||||
requestIdProvider
|
requestIdProvider
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return R.failure(
|
return R.failure(
|
||||||
webMvcExceptionProperties.defaultValidationExceptionErrorCode.toString(),
|
webMvcExceptionProperties.defaultValidationExceptionErrorCode,
|
||||||
webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
|
webMvcExceptionProperties.defaultValidationExceptionErrorMessage,
|
||||||
null,
|
null,
|
||||||
requestIdProvider
|
requestIdProvider
|
||||||
@ -95,12 +95,12 @@ class GlobalExceptionHandler(
|
|||||||
fun handleConstraintViolationException(ex: ConstraintViolationException): R<String> {
|
fun handleConstraintViolationException(ex: ConstraintViolationException): R<String> {
|
||||||
for (constraintViolation in ex.constraintViolations) {
|
for (constraintViolation in ex.constraintViolations) {
|
||||||
return R.failure(
|
return R.failure(
|
||||||
HttpStatus.BAD_REQUEST.value().toString(), constraintViolation.message,
|
HttpStatus.BAD_REQUEST.value(), constraintViolation.message,
|
||||||
null, requestIdProvider
|
null, requestIdProvider
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return R.failure(
|
return R.failure(
|
||||||
webMvcExceptionProperties.defaultInvalidParameterErrorCode.toString(),
|
webMvcExceptionProperties.defaultInvalidParameterErrorCode,
|
||||||
webMvcExceptionProperties.defaultInvalidParameterErrorMessage,
|
webMvcExceptionProperties.defaultInvalidParameterErrorMessage,
|
||||||
null,
|
null,
|
||||||
requestIdProvider
|
requestIdProvider
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
com.gewuyou.forgeboot.webmvc.exception.config.WebMvcExceptionAutoConfiguration
|
||||||
Loading…
x
Reference in New Issue
Block a user