feat(0): [java]-[wechat-ferry-mvn]-自定义异常类

This commit is contained in:
chandler 2024-11-23 09:57:33 +08:00
parent c0e8c4cc0e
commit 0db55a9016
2 changed files with 159 additions and 0 deletions

View File

@ -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 <T extends IResponse> BizException(String msg) {
super(msg);
this.response = ResponseCodeEnum.FAILED;
this.arg = null;
}
/**
* 业务异常构造器
*
* @param msg 异常信息
* @param args 异常参数
* @date 2021/11/24 23:58
*/
public <T extends IResponse> 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 <T extends IResponse> BizException(T t, Object... args) {
super(Arrays.toString(args));
this.response = t;
this.arg = args;
}
public <T extends IResponse> BizException(BizException e) {
this.response = e.getResponse();
this.arg = e.getArg();
}
public <T extends IResponse> BizException(ResponseCodeEnum t, String msg) {
super(msg);
this.response = t;
this.arg = null;
}
}

View File

@ -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<Object> 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<Object> 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<Object> handleBizException(BizException e) {
// 打印错误
e.printStackTrace();
// 获取错误码
String message = MessageFormat.format(e.getMessage(), e.getArg());
return new TResponse<>(ResponseCodeEnum.FAILED, message);
}
}