From a1844973fad4fbcec262869213b4a158fd810787 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Tue, 14 Nov 2023 17:33:39 +0800 Subject: [PATCH] Upgrade SysLogInterceptor --- .../kotlin/top/fatweb/api/entity/common/ResponseResult.kt | 5 ++++- src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt | 2 +- .../kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt b/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt index 022c4b9..fb5452e 100644 --- a/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt +++ b/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt @@ -13,8 +13,11 @@ class ResponseResult private constructor( @Schema(description = "数据") val data: T? ) : Serializable { companion object { + fun build(code: Int, success: Boolean, msg: String, data: T?) = + ResponseResult(code, success, msg, data) + fun build(code: ResponseCode, success: Boolean, msg: String, data: T?) = - ResponseResult(code.code, success, msg, data) + build(code.code, success, msg, data) fun success(code: ResponseCode = ResponseCode.SYSTEM_OK, msg: String = "success", data: T? = null) = build(code, true, msg, data) diff --git a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt index 6e0b0d9..102aba0 100644 --- a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt @@ -84,7 +84,7 @@ class ExceptionHandler { else -> { logger.error(e.localizedMessage, e) - ResponseResult.fail(ResponseCode.SYSTEM_ERROR, data = null) + ResponseResult.fail(ResponseCode.SYSTEM_ERROR, e.localizedMessage, null) } } } diff --git a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt index e128bd6..b9a1452 100644 --- a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt +++ b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt @@ -10,6 +10,7 @@ import org.springframework.http.server.ServerHttpResponse import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.servlet.HandlerInterceptor import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice +import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult import top.fatweb.api.entity.system.SysLog import top.fatweb.api.service.system.ISysLogService @@ -100,6 +101,11 @@ class SysLogInterceptor( response: ServerHttpResponse ): Any? { resultThreadLocal.set(body) + + if (body is ResponseResult<*> && body.code == ResponseCode.SYSTEM_ERROR.code) { + return ResponseResult.build(body.code, body.success, "fail", body.data) + } + return body } } \ No newline at end of file