Upgrade SysLogInterceptor

This commit is contained in:
2023-11-14 17:33:39 +08:00
parent e681d9d7b7
commit a1844973fa
3 changed files with 11 additions and 2 deletions

View File

@@ -13,8 +13,11 @@ class ResponseResult<T> private constructor(
@Schema(description = "数据") val data: T?
) : Serializable {
companion object {
fun <T> build(code: Int, success: Boolean, msg: String, data: T?) =
ResponseResult(code, success, msg, data)
fun <T> build(code: ResponseCode, success: Boolean, msg: String, data: T?) =
ResponseResult(code.code, success, msg, data)
build(code.code, success, msg, data)
fun <T> success(code: ResponseCode = ResponseCode.SYSTEM_OK, msg: String = "success", data: T? = null) =
build(code, true, msg, data)

View File

@@ -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)
}
}
}

View File

@@ -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
}
}