From 501be029c6b64c2b1ccdb04bfab59c273fb37a0f Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期四, 19 九月 2024 17:40:42 +0800 Subject: [PATCH] update 优化 sse 异常单独处理 避免出现异常报错问题 --- ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) diff --git a/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java index bd47c18..56c9e9f 100644 --- a/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java @@ -2,12 +2,14 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpStatus; +import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; import org.dromara.common.core.domain.R; import org.dromara.common.core.exception.ServiceException; +import org.dromara.common.core.exception.SseException; import org.dromara.common.core.exception.base.BaseException; import org.dromara.common.core.utils.StreamUtils; import org.springframework.context.support.DefaultMessageSourceResolvable; @@ -16,9 +18,12 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingPathVariableException; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.servlet.NoHandlerFoundException; + +import java.io.IOException; /** * 鍏ㄥ眬寮傚父澶勭悊鍣� @@ -48,6 +53,27 @@ log.error(e.getMessage()); Integer code = e.getCode(); return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); + } + + /** + * 璁よ瘉澶辫触 + */ + @ResponseStatus(org.springframework.http.HttpStatus.UNAUTHORIZED) + @ExceptionHandler(SseException.class) + public String handleNotLoginException(SseException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("璇锋眰鍦板潃'{}',璁よ瘉澶辫触'{}',鏃犳硶璁块棶绯荤粺璧勬簮", requestURI, e.getMessage()); + return e.getMessage(); + } + + /** + * servlet寮傚父 + */ + @ExceptionHandler(ServletException.class) + public R<Void> handleServletException(ServletException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("璇锋眰鍦板潃'{}',鍙戠敓鏈煡寮傚父.", requestURI, e); + return R.fail(e.getMessage()); } /** @@ -92,6 +118,20 @@ /** * 鎷︽埅鏈煡鐨勮繍琛屾椂寮傚父 */ + @ResponseStatus(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(IOException.class) + public void handleRuntimeException(IOException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + if (requestURI.contains("sse")) { + // sse 缁忓父鎬ц繛鎺ヤ腑鏂� 渚嬪鍏抽棴娴忚鍣� 鐩存帴灞忚斀 + return; + } + log.error("璇锋眰鍦板潃'{}',杩炴帴涓柇", requestURI, e); + } + + /** + * 鎷︽埅鏈煡鐨勮繍琛屾椂寮傚父 + */ @ExceptionHandler(RuntimeException.class) public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); -- Gitblit v1.9.3