feat(全局): 添加异常处理
This commit is contained in:
parent
57ab5d0df1
commit
48461031e7
35
src/main/java/com/wms/common/BaseResponse.java
Normal file
35
src/main/java/com/wms/common/BaseResponse.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package com.wms.common;
|
||||
|
||||
import com.wms.exception.ErrorCode;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 全局响应封装类
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
@Data
|
||||
public class BaseResponse<T> implements Serializable {
|
||||
|
||||
private int code;
|
||||
|
||||
private T data;
|
||||
|
||||
private String message;
|
||||
|
||||
public BaseResponse(int code, T data, String message) {
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public BaseResponse(int code, T data) {
|
||||
this(code, data, "");
|
||||
}
|
||||
|
||||
public BaseResponse(ErrorCode errorCode) {
|
||||
this(errorCode.getCode(), null, errorCode.getMessage());
|
||||
}
|
||||
}
|
||||
31
src/main/java/com/wms/exception/BusinessException.java
Normal file
31
src/main/java/com/wms/exception/BusinessException.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package com.wms.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 自定义业务异常
|
||||
*/
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private final int code;
|
||||
|
||||
public BusinessException(int code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(ErrorCode errorCode) {
|
||||
super(errorCode.getMessage());
|
||||
this.code = errorCode.getCode();
|
||||
}
|
||||
|
||||
public BusinessException(ErrorCode errorCode, String message) {
|
||||
super(message);
|
||||
this.code = errorCode.getCode();
|
||||
}
|
||||
|
||||
}
|
||||
52
src/main/java/com/wms/exception/ThrowUtils.java
Normal file
52
src/main/java/com/wms/exception/ThrowUtils.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package com.wms.exception;
|
||||
|
||||
|
||||
/**
|
||||
* 异常处理工具类
|
||||
*/
|
||||
public class ThrowUtils {
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param runtimeException 异常
|
||||
*/
|
||||
public static void throwIf(boolean condition, RuntimeException runtimeException) {
|
||||
if (condition) {
|
||||
throw runtimeException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public static void throwIf(boolean condition,Integer errorCode, String message) {
|
||||
throwIf(condition, new BusinessException(errorCode,message));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public static void throwIf(boolean condition, ErrorCode errorCode) {
|
||||
throwIf(condition, new BusinessException(errorCode.getCode(),errorCode.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件成立则抛异常
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param errorCode 错误码
|
||||
*/
|
||||
public static void throwIf(boolean condition, ErrorCode errorCode,String message) {
|
||||
throwIf(condition, new BusinessException( errorCode));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,20 +1,27 @@
|
|||
package com.wms.filter;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.wms.common.BaseResponse;
|
||||
import com.wms.common.ResultUtils;
|
||||
import com.wms.entity.app.container.ContainerApiLocalResponse;
|
||||
import com.wms.entity.app.mes.MesApiLocalResponse;
|
||||
import com.wms.exception.BusinessException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
@ResponseBody
|
||||
|
|
@ -38,4 +45,26 @@ public class GlobalExceptionHandler {
|
|||
return String.format("{\"code\": 999, \"message\": \"%s\"}", message);
|
||||
}
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(com.wms.filter.GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 拦截未知的运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public BaseResponse handleRuntimeException(RuntimeException e, HttpServletRequest request)
|
||||
{
|
||||
String requestURI = request.getRequestURI();
|
||||
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
||||
return ResultUtils.error(400, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务异常
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public BaseResponse handleBusinessException(BusinessException e, HttpServletRequest request)
|
||||
{
|
||||
log.error(e.getMessage(), e);
|
||||
return ResultUtils.error(400,e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user