v0.0.16
CI/CD / build-deploy (pull_request) Successful in 1m51s

This commit is contained in:
Timi
2026-07-31 15:11:38 +08:00
parent c2cb8771ce
commit 8736aa44c4
5 changed files with 252 additions and 13 deletions
@@ -3,7 +3,10 @@ package com.imyeyu.spring.util;
import com.imyeyu.java.bean.timi.TimiCode;
import com.imyeyu.java.bean.timi.TimiException;
import com.imyeyu.java.bean.timi.TimiResponse;
import com.imyeyu.spring.TimiSpring;
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,12 +45,16 @@ public class GlobalExceptionHandler {
* @param e 异常
* @return 异常返回
*/
@IgnoreGlobalReturn
@ExceptionHandler(HttpMessageConversionException.class)
public TimiResponse<?> conversionException(HttpMessageConversionException e) {
public Object conversionException(HttpMessageConversionException e) {
log.warn(e.getMessage());
if (env.contains("dev") || log.isDebugEnabled()) {
log.error("conversion error", e);
}
if (ignoreCommittedBinaryResponse()) {
return null;
}
return new TimiResponse<>(TimiCode.ARG_BAD).msgKey("invalid.body");
}
@@ -57,12 +64,16 @@ public class GlobalExceptionHandler {
* @param e 异常
* @return 异常返回
*/
@IgnoreGlobalReturn
@ExceptionHandler(ServletException.class)
public TimiResponse<?> headerException(ServletException e) {
public Object headerException(ServletException e) {
log.warn(e.getMessage());
if (env.contains("dev") || log.isDebugEnabled()) {
log.error("header error", e);
}
if (ignoreCommittedBinaryResponse()) {
return null;
}
return new TimiResponse<>(TimiCode.REQUEST_BAD).msgKey("invalid.request");
}
@@ -93,17 +104,46 @@ public class GlobalExceptionHandler {
* @param e 异常
* @return 异常返回
*/
@IgnoreGlobalReturn
@ExceptionHandler(Throwable.class)
public TimiResponse<?> error(Throwable e) {
public Object error(Throwable e) {
if (e instanceof TimiException timiE) {
if (!env.startsWith("prod") || log.isDebugEnabled()) {
log.error(timiE.getMessage(), e);
}
if (ignoreCommittedBinaryResponse()) {
return null;
}
// 一般异常
return timiE.toResponse();
}
// 致命异常
log.error("fatal error", e);
if (ignoreCommittedBinaryResponse()) {
return null;
}
return new TimiResponse<>(TimiCode.ERROR).msgKey("service.error");
}
/**
* 已进入二进制响应写出阶段时,不再尝试输出统一 JSON 包装,
* 否则只会制造二次异常并覆盖原始状态码。
*
* @return `true` 表示当前响应应跳过异常包装
*/
private boolean ignoreCommittedBinaryResponse() {
HttpServletResponse response = TimiSpring.getResponse();
if (response == null) {
return false;
}
if (response.isCommitted()) {
return true;
}
String contentType = response.getContentType();
if (contentType == null || contentType.isBlank()) {
return false;
}
String lowerContentType = contentType.toLowerCase();
return !lowerContentType.contains("json") && !lowerContentType.startsWith("text/");
}
}