add GlobalExceptionHandler msg
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
package com.imyeyu.spring.util;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.validation.ValidationException;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.validation.ValidationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
@ -41,7 +41,7 @@ public class GlobalExceptionHandler {
|
||||
if (env.contains("dev") || log.isDebugEnabled()) {
|
||||
log.error("conversion error", e);
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD);
|
||||
return new TimiResponse<>(TimiCode.ARG_BAD).msgKey("invalid.body");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,7 +56,7 @@ public class GlobalExceptionHandler {
|
||||
if (env.contains("dev") || log.isDebugEnabled()) {
|
||||
log.error("header error", e);
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD);
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.header");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,7 +77,7 @@ public class GlobalExceptionHandler {
|
||||
if (env.startsWith("dev") || log.isDebugEnabled()) {
|
||||
log.error("request error", e);
|
||||
}
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD);
|
||||
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.arg");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,7 +89,6 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public TimiResponse<?> error(Throwable e) {
|
||||
if (e instanceof TimiException timiE) {
|
||||
// TODO 400 以下即使是开发环境也不算异常
|
||||
if (env.startsWith("dev") || log.isDebugEnabled()) {
|
||||
log.error(timiE.getMessage(), e);
|
||||
}
|
||||
@ -98,6 +97,6 @@ public class GlobalExceptionHandler {
|
||||
}
|
||||
// 致命异常
|
||||
log.error("fatal error", e);
|
||||
return new TimiResponse<>(TimiCode.ERROR);
|
||||
return new TimiResponse<>(TimiCode.ERROR).msgKey("service.error");
|
||||
}
|
||||
}
|
||||
|
||||
4
src/main/resources/lang/zhCN.lang
Normal file
4
src/main/resources/lang/zhCN.lang
Normal file
@ -0,0 +1,4 @@
|
||||
invalid.arg=无效的参数
|
||||
invalid.body=无效的请求体
|
||||
invalid.header=无效的请求头
|
||||
service.error=服务错误
|
||||
Reference in New Issue
Block a user