v0.0.17 #17
@@ -13,7 +13,7 @@
|
||||
|
||||
<groupId>com.imyeyu.spring</groupId>
|
||||
<artifactId>timi-spring</artifactId>
|
||||
<version>0.0.16</version>
|
||||
<version>0.0.17</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
@@ -143,6 +143,16 @@
|
||||
<artifactId>timi-io</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.java</groupId>
|
||||
<artifactId>timi-java</artifactId>
|
||||
<version>0.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.lang</groupId>
|
||||
<artifactId>timi-lang</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@@ -231,7 +231,7 @@ public abstract class BaseSQLProvider {
|
||||
Ref.setFieldValue(entity, fc.field, uuid);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new TimiException(TimiCode.ERROR).msgKey("auto set field:%s value error".formatted(fc.fieldName));
|
||||
throw new TimiException(TimiCode.ERROR, "auto set field:%s value error".formatted(fc.fieldName));
|
||||
}
|
||||
if (entity instanceof Creatable creatableEntity && creatableEntity.getCreatedAt() == null) {
|
||||
creatableEntity.setCreatedAt(Time.now());
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiError;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.lang.message.MessageResolver;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -11,6 +14,7 @@ import jakarta.validation.ValidationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.converter.HttpMessageConversionException;
|
||||
import org.springframework.validation.BindException;
|
||||
@@ -30,10 +34,15 @@ public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
private final ObjectProvider<MessageResolver> messageResolverProvider;
|
||||
|
||||
/**
|
||||
* 创建全局异常处理器
|
||||
*
|
||||
* @param messageResolverProvider 消息解析器提供器
|
||||
*/
|
||||
public GlobalExceptionHandler() {
|
||||
public GlobalExceptionHandler(ObjectProvider<MessageResolver> messageResolverProvider) {
|
||||
this.messageResolverProvider = messageResolverProvider;
|
||||
}
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
@@ -55,7 +64,7 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD).msgKey("invalid.body");
|
||||
return fail(TimiCode.ARG_BAD, "invalid.body");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +83,7 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.request");
|
||||
return fail(TimiCode.REQUEST_BAD, "invalid.request");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,19 +92,20 @@ public class GlobalExceptionHandler {
|
||||
* @param e 异常
|
||||
* @return 异常返回
|
||||
*/
|
||||
@IgnoreGlobalReturn
|
||||
@ExceptionHandler({BindException.class, ValidationException.class, MethodArgumentNotValidException.class, TypeMismatchException.class})
|
||||
public TimiResponse<?> paramsException(Exception e) {
|
||||
if (e instanceof MethodArgumentNotValidException subE) {
|
||||
log.warn("request error", e);
|
||||
FieldError error = subE.getBindingResult().getFieldError();
|
||||
if (error != null) {
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD, "[%s] %s".formatted(error.getField(), error.getDefaultMessage()));
|
||||
return fail(TimiCode.ARG_BAD, "[%s] %s".formatted(error.getField(), error.getDefaultMessage()));
|
||||
}
|
||||
}
|
||||
if (env.startsWith("dev") || log.isDebugEnabled()) {
|
||||
log.error("request error", e);
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.arg");
|
||||
return fail(TimiCode.REQUEST_BAD, "invalid.arg");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,15 +124,81 @@ public class GlobalExceptionHandler {
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
// 一般异常
|
||||
return timiE.toResponse();
|
||||
return fail(timiE);
|
||||
}
|
||||
if (e instanceof TimiError timiE) {
|
||||
log.error(timiE.getMessage(), e);
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return fail(timiE.getCode(), timiE.getMessageCode(), timiE.getMessageArgs());
|
||||
}
|
||||
// 致命异常
|
||||
log.error("fatal error", e);
|
||||
if (ignoreCommittedBinaryResponse()) {
|
||||
return null;
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.ERROR).msgKey("service.error");
|
||||
return fail(TimiCode.ERROR, "service.error");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param e 异常
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiException e) {
|
||||
return fail(e.getCode(), e.getMessageCode(), e.getMessageArgs());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiCode code, String messageCode) {
|
||||
return fail(code, messageCode, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建失败响应
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
* @return 响应
|
||||
*/
|
||||
private TimiResponse<?> fail(TimiCode code, String messageCode, java.util.Map<String, Object> messageArgs) {
|
||||
TimiResponse<?> result = new TimiResponse<>(code).messageCode(messageCode, messageArgs);
|
||||
result.setMessage(resolveMessage(code, messageCode, messageArgs));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析消息
|
||||
*
|
||||
* @param code 代码
|
||||
* @param messageCode 消息代码
|
||||
* @param messageArgs 消息插值参数
|
||||
* @return 消息
|
||||
*/
|
||||
private String resolveMessage(TimiCode code, String messageCode, java.util.Map<String, Object> messageArgs) {
|
||||
if (TimiJava.isEmpty(messageCode)) {
|
||||
return code.toString();
|
||||
}
|
||||
try {
|
||||
MessageResolver messageResolver = messageResolverProvider.getIfAvailable();
|
||||
if (messageResolver == null) {
|
||||
return messageCode;
|
||||
}
|
||||
String message = messageResolver.resolve(messageCode, messageArgs, TimiSpring.getLanguage());
|
||||
return TimiJava.isEmpty(message) ? messageCode : message;
|
||||
} catch (Exception e) {
|
||||
log.error("message resolve error", e);
|
||||
return messageCode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import com.imyeyu.java.bean.LanguageMsgMapping;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.AOPLogInterceptor;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import lombok.Data;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -28,15 +24,11 @@ import java.util.Objects;
|
||||
* @author 夜雨
|
||||
* @version 2023-04-30 00:59
|
||||
*/
|
||||
@Data
|
||||
@RestControllerAdvice
|
||||
public class GlobalReturnHandler implements ResponseBodyAdvice<Object> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalReturnHandler.class);
|
||||
|
||||
/** 多语言头处理回调 */
|
||||
private CallbackArgReturn<LanguageMsgMapping<?>, String> multilingualHeader;
|
||||
|
||||
/**
|
||||
* 创建全局返回处理器
|
||||
*/
|
||||
@@ -59,23 +51,12 @@ public class GlobalReturnHandler implements ResponseBodyAdvice<Object> {
|
||||
{
|
||||
TimiResponse<?> result;
|
||||
if (body instanceof TimiResponse<?> timiResponse) {
|
||||
// 可能已被全局异常包装
|
||||
result = timiResponse;
|
||||
} else {
|
||||
result = new TimiResponse<>(TimiCode.SUCCESS, body);
|
||||
}
|
||||
try {
|
||||
if (multilingualHeader != null && TimiJava.isNotEmpty(result.getMsgKey())) {
|
||||
result.setMsg(multilingualHeader.handler(result));
|
||||
} else if (TimiJava.isEmpty(result.getMsg())) {
|
||||
result.setMsg(TimiCode.fromCode(result.getCode()).toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("multilingual response error", e);
|
||||
result.setMsg(TimiCode.fromCode(result.getCode()).toString());
|
||||
}
|
||||
if (30000 < result.getCode()) {
|
||||
log.warn("ID: {} Response -> Exception.{}.{}", TimiSpring.getSessionAttr(AOPLogInterceptor.REQUEST_ID), result.getCode(), result.getMsg());
|
||||
log.warn("ID: {} Response -> Exception.{}.{}", TimiSpring.getSessionAttr(AOPLogInterceptor.REQUEST_ID), result.getCode(), result.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user