From 0db55a90163bff557a2e666e7ff75eba017ab747 Mon Sep 17 00:00:00 2001 From: chandler <1915724901@qq.com> Date: Sat, 23 Nov 2024 09:57:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(0):=20[java]-[wechat-ferry-mvn]-=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=BC=82=E5=B8=B8=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/ferry/exception/BizException.java | 80 +++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 79 ++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/BizException.java create mode 100644 clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/GlobalExceptionHandler.java diff --git a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/BizException.java b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/BizException.java new file mode 100644 index 0000000..2de4bba --- /dev/null +++ b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/BizException.java @@ -0,0 +1,80 @@ +package com.wechat.ferry.exception; + +import java.util.Arrays; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.wechat.ferry.entity.IResponse; +import com.wechat.ferry.enums.ResponseCodeEnum; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 业务异常类 + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class BizException extends RuntimeException { + + /** + * 返回接口 + */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private final IResponse response; + + /** + * 返回参数 + */ + @JsonInclude(JsonInclude.Include.NON_EMPTY) + private transient Object[] arg; + + /** + * 业务异常构造器 + * + * @param msg 异常信息 + * @date 2021/11/24 23:58 + */ + public BizException(String msg) { + super(msg); + this.response = ResponseCodeEnum.FAILED; + this.arg = null; + } + + /** + * 业务异常构造器 + * + * @param msg 异常信息 + * @param args 异常参数 + * @date 2021/11/24 23:58 + */ + public BizException(String msg, Object... args) { + super(msg); + this.response = ResponseCodeEnum.FAILED; + this.arg = args; + } + + /** + * 业务异常构造器 + * + * @param t 异常响应码 + * @param args 异常参数 + * @date 2021/11/24 23:59 + */ + public BizException(T t, Object... args) { + super(Arrays.toString(args)); + this.response = t; + this.arg = args; + } + + public BizException(BizException e) { + this.response = e.getResponse(); + this.arg = e.getArg(); + } + + public BizException(ResponseCodeEnum t, String msg) { + super(msg); + this.response = t; + this.arg = null; + } + +} diff --git a/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/GlobalExceptionHandler.java b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..1d1e4a4 --- /dev/null +++ b/clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/exception/GlobalExceptionHandler.java @@ -0,0 +1,79 @@ +package com.wechat.ferry.exception; + +import java.text.MessageFormat; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.core.annotation.Order; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.wechat.ferry.entity.TResponse; +import com.wechat.ferry.enums.ResponseCodeEnum; + +import lombok.extern.slf4j.Slf4j; + +/** + * 全局统一异常 + * + * @author Simith + * @date 2021/11/23 23:20 + */ +@Slf4j +@Order(-1) +// 表示当前类为全局异常处理器 +@RestControllerAdvice +public class GlobalExceptionHandler { + + /** + * 通用异常-系统级别未知异常 + * + * @param e 异常信息 + * @return TResponse + * @author Simith + * @date 2021/11/23 23:22 + */ + @ExceptionHandler(Exception.class) + public TResponse handleException(Exception e) { + log.error("全局异常信息 ex={}", e.getMessage(), e); + // 打印堆栈信息 + e.printStackTrace(); + String message = ResponseCodeEnum.FAILED.getMsg() + ":" + e.getMessage(); + return new TResponse<>(ResponseCodeEnum.FAILED, message); + } + + /** + * 参数异常 + * + * @author chandler + * @date 2023/4/3 23:26 + * @param request 请求入参 + * @param e 异常消息 + * @return TResponse 返回体 + */ + @ExceptionHandler(value = {MethodArgumentNotValidException.class}) + public TResponse handleValidationException(HttpServletRequest request, MethodArgumentNotValidException e) { + log.error("[请求体参数校验不通过]", e); + String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage(); + return new TResponse<>(ResponseCodeEnum.PARAM_ERROR, message); + } + + /** + * 自定义错误异常 + * + * @param e 异常信息 + * @return TResponse + * @author Simith + * @date 2021/11/23 23:43 + */ + @ExceptionHandler(BizException.class) + public TResponse handleBizException(BizException e) { + // 打印错误 + e.printStackTrace(); + // 获取错误码 + String message = MessageFormat.format(e.getMessage(), e.getArg()); + return new TResponse<>(ResponseCodeEnum.FAILED, message); + } + +}