From 1e2e641d3f08a805cd75122fc7d6346f4b5ca4e2 Mon Sep 17 00:00:00 2001 From: liuchen <23082234@qq.com> Date: Tue, 30 Jan 2024 12:54:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../win/mq/controller/ProduceController.java | 13 ++-- .../mq/handler/GlobalExceptionHandler.java | 61 +++++++++++++++++++ src/main/resources/logback-spring.xml | 2 +- 3 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/win/mq/handler/GlobalExceptionHandler.java diff --git a/src/main/java/com/win/mq/controller/ProduceController.java b/src/main/java/com/win/mq/controller/ProduceController.java index 7dcac2c..111dca5 100644 --- a/src/main/java/com/win/mq/controller/ProduceController.java +++ b/src/main/java/com/win/mq/controller/ProduceController.java @@ -13,6 +13,7 @@ import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -38,7 +39,7 @@ public class ProduceController { */ @PostMapping("/api") @SuppressWarnings("unchecked") - public CommonResult api(HttpServletRequest request, @RequestBody String body) { + public CommonResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { String interfaceName = request.getHeader("interface"); String sign = request.getHeader("sign"); String timeStr = request.getHeader("timestamp"); @@ -69,14 +70,8 @@ public class ProduceController { if(!activeProfile.contains("dev") && !StringUtils.equals(sign, computeSign.toUpperCase())) { return CommonResult.error(GlobalErrorCodeConstants.SIGN_ERROR); } - try { - Method method = this.getClass().getMethod(interfaceName, String.class); - return (CommonResult) method.invoke(this, body); - } catch (NoSuchMethodException e) { - return CommonResult.error(GlobalErrorCodeConstants.INTERFACE_ERROR); - } catch (Exception e) { - return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR); - } + Method method = this.getClass().getMethod(interfaceName, String.class); + return (CommonResult) method.invoke(this, body); } public CommonResult order(String message) { diff --git a/src/main/java/com/win/mq/handler/GlobalExceptionHandler.java b/src/main/java/com/win/mq/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..e4b463c --- /dev/null +++ b/src/main/java/com/win/mq/handler/GlobalExceptionHandler.java @@ -0,0 +1,61 @@ +package com.win.mq.handler; + +import com.win.mq.common.CommonResult; +import com.win.mq.exception.ServiceException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import static com.win.mq.exception.GlobalErrorCodeConstants.INTERFACE_ERROR; +import static com.win.mq.exception.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; + +/** + * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号 + * + * @author 闻荫源码 + */ +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + /** + * 接口未找到业务异常 NoSuchMethodException + */ + @ExceptionHandler(NoSuchMethodException.class) + public CommonResult noSuchMethodExceptionHandler(NoSuchMethodException e) { + log.error("[serviceExceptionHandler]", e); + return CommonResult.error(INTERFACE_ERROR.getCode(), INTERFACE_ERROR.getMsg()); + } + + /** + * 处理业务异常 ServiceException + * + * 例如说,商品库存不足,用户手机号已存在。 + */ + @ExceptionHandler(ServiceException.class) + public CommonResult serviceExceptionHandler(ServiceException e) { + log.error("[serviceExceptionHandler]", e); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + + /** + * 处理业务异常 ServiceException + * + * 例如说,商品库存不足,用户手机号已存在。 + */ + @ExceptionHandler(RuntimeException.class) + public CommonResult runtimeExceptionHandler(ServiceException e) { + log.error("[runtimeExceptionHandler]", e); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + + /** + * 处理系统异常,兜底处理所有的一切 + */ + @ExceptionHandler(Exception.class) + public CommonResult defaultExceptionHandler(Throwable e) { + log.error("[defaultExceptionHandler]", e); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + +} diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index c191ed4..8e6c574 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -87,7 +87,7 @@ - +