feat: 优化系统处理 responsestatus 异常的逻辑
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt
index be0b03e..b09c084 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/PayApiApplication.kt
@@ -23,6 +23,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.boot.web.servlet.ServletComponentScan
+import org.springframework.boot.web.servlet.error.DefaultErrorAttributes
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.interceptor.KeyGenerator
@@ -32,6 +33,8 @@
import org.springframework.context.annotation.Configuration
import org.springframework.context.event.ContextStartedEvent
import org.springframework.context.event.EventListener
+import org.springframework.core.Ordered
+import org.springframework.core.annotation.Order
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.connection.RedisPassword
import org.springframework.data.redis.connection.RedisStandaloneConfiguration
@@ -90,6 +93,12 @@
fun lockProvider(connectionFactory: RedisConnectionFactory): LockProvider {
return RedisLockProvider(connectionFactory, "prod")
}
+
+ @Bean
+ @Order(Ordered.HIGHEST_PRECEDENCE)
+ fun getDefaultErrorAttributes(): DefaultErrorAttributes {
+ return DefaultErrorAttributes(false)
+ }
}
@Configuration
@@ -141,8 +150,10 @@
class RestTemplateProxyProperties {
@Value("\${type:}")
lateinit var type: String
+
@Value("\${host:}")
lateinit var host: String
+
@Value("\${port:0}")
var port: Int = 0
}
@@ -179,6 +190,7 @@
@Component
class TenantConfigListener {
private val log = KotlinLogging.logger { }
+
@EventListener
fun handleContextStarted(evt: ContextStartedEvent) {
log.info { "Service started check tenant information" }
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt
index 47182ef..a939feb 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt
@@ -5,7 +5,6 @@
import com.supwisdom.dlpay.exception.TransactionException
import com.supwisdom.dlpay.framework.ResponseBodyBuilder
import com.supwisdom.dlpay.framework.service.CommonService
-import com.supwisdom.dlpay.framework.util.TradeErrorCode
import mu.KotlinLogging
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
@@ -13,18 +12,20 @@
import org.aspectj.lang.annotation.Pointcut
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
+import org.springframework.boot.web.servlet.error.ErrorAttributes
+import org.springframework.core.annotation.AnnotationUtils
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Component
import org.springframework.transaction.TransactionSystemException
-import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice
+import org.springframework.web.context.request.ServletWebRequest
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
import java.lang.reflect.UndeclaredThrowableException
@@ -33,11 +34,21 @@
@RestControllerAdvice("com.supwisdom.dlpay.api")
-class RestControllerAdvice {
+class RestControllerAdvice(private val errorAttributes: ErrorAttributes) {
val logger = KotlinLogging.logger { }
+ protected fun getStatus(request: HttpServletRequest): HttpStatus {
+ val statusCode = request.getAttribute("javax.servlet.error.status_code")
+ ?: return HttpStatus.INTERNAL_SERVER_ERROR
+ return try {
+ HttpStatus.valueOf(statusCode as Int)
+ } catch (ex: java.lang.Exception) {
+ HttpStatus.INTERNAL_SERVER_ERROR
+ }
+ }
+
@ExceptionHandler
- fun handleException(ex: Exception, request: HttpServletRequest): ResponseEntity<Any> {
+ fun handleException(ex: Exception, request: HttpServletRequest): ResponseEntity<*> {
logger.error { "Request unhandler exception, url<${request.requestURI}>, ex<${ex.cause}>" }
val undeclared = if (ex is UndeclaredThrowableException) ex.undeclaredThrowable else ex
if (undeclared is RequestParamCheckException) {
@@ -48,8 +59,11 @@
.exception(undeclared.code(), undeclared, "业务处理错误"))
}
- return ResponseEntity.ok().body(ResponseBodyBuilder.create()
- .exception(TradeErrorCode.BUSINESS_DEAL_ERROR, undeclared, "业务处理报错"))
+ val body = this.errorAttributes.getErrorAttributes(ServletWebRequest(request),
+ false)
+ val responseStatus = AnnotationUtils.findAnnotation(ex.javaClass, ResponseStatus::class.java)
+ val status = responseStatus?.code ?: getStatus(request)
+ return ResponseEntity<Map<String, Any>>(body, status)
}
}
@@ -57,6 +71,7 @@
@ControllerAdvice("com.supwisdom.dlpay")
class MvcControllerAdvice {
private val logger = KotlinLogging.logger { }
+
@ExceptionHandler(ConstraintViolationException::class)
fun handleException(e: ConstraintViolationException): ResponseEntity<Any> {
logger.error { "unhandle exception : $e" }
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt
index 97e1782..baac656 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/framework/controller/security_controller.kt
@@ -128,7 +128,7 @@
.success(ApiLoginResponse(token.get().jwt,
appid, Instant.ofEpochSecond(token.get().expiration).toString())))
} else {
- ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()
+ ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("token verify error")
}
}
}