yinyitong-zhongyuyuan-dll-s.../src/main/java/com/dpkj/common/exception/ControllerAdvice.java

140 lines
5.1 KiB
Java
Raw Normal View History

2025-02-07 11:03:05 +08:00
package com.dpkj.common.exception;
import com.dpkj.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* controller控制器异常处理接口实现类
* <p>其它module可以集成此类进行controller层的异常处理</p>
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
*/
@Slf4j
@Component
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class ControllerAdvice {
/**
* controller方法中的参数校验失败但是前提是要使用
* #@Validated注解开启参数校验
*
* @param e 异常
* @return Result
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public Result<String> bindingException(MethodArgumentNotValidException e) {
// 获得所有校验出错的返回集
BindingResult bindingResult = e.getBindingResult();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
// 循环获得所有校验异常的字段
Map<String, String> fieldMap = new HashMap<>();
for (FieldError fieldError : fieldErrors) {
fieldMap.put(fieldError.getField(), fieldError.getDefaultMessage());
}
String errMsg = fieldMap.values().toString().replaceAll("]", "").replaceAll("\\[", "");
log.warn(errMsg);
// 返回给前端
return Result.error(errMsg);
}
/**
* 处理空指针异常
* @param nullPointerException 空指针异常
* @return Result
*/
@ExceptionHandler(value = NullPointerException.class)
public Result<String> nullPointException(NullPointerException nullPointerException) {
2025-03-26 13:01:53 +08:00
log.error("空指针异常类型: {},信息: {}", nullPointerException.getClass(), nullPointerException);
2025-02-07 11:03:05 +08:00
return Result.error(ErrorEnum.NULL_POINTER_EXCEPTION);
}
/**
* 所有的运行时异常抛出异常
* @param throwable 异常
* @return Result
*/
@ExceptionHandler(value = Throwable.class)
public Result<String> handleException(Throwable throwable) {
log.error("异常类型: {}, {}, 信息为: {}", throwable.getCause(), throwable.getClass(), throwable.getMessage());
if (throwable instanceof RRException){
RRException rrException = (RRException) throwable;
return Result.error(rrException.getCode(), rrException.getMsg());
}
return Result.error(ErrorEnum.RUNTIME_EXCEPTION);
}
/**
* http信息无可读
* @param e 异常
* @return Result
*/
@ExceptionHandler(value = HttpMessageNotReadableException.class)
public Result<String> httpMessageNotReadAbleException(HttpMessageNotReadableException e){
2025-03-26 13:01:53 +08:00
log.warn("异常类型: {} 无可读信息: ", e.getClass(), e);
2025-02-07 11:03:05 +08:00
return Result.error(ErrorEnum.HTTP_MESSAGE_NOT_READABLE_EXCEPTION);
}
/**
* 运行时异常
* @param e 运行异常对象
* @return Result
*/
@ExceptionHandler(value = RuntimeException.class)
public Result<String> runtimeException(RuntimeException e){
2025-03-26 13:01:53 +08:00
log.error("运行时异常:", e);
2025-02-07 11:03:05 +08:00
if (e instanceof RRException){
RRException rrException = (RRException) e;
return Result.error(rrException.getCode(), rrException.getMsg());
}
return Result.error(ErrorEnum.RUNTIME_EXCEPTION);
}
/**
* 请求不支持
* @return Result
*/
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public Result<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
2025-03-26 13:01:53 +08:00
log.warn("暂不支持该请求: ", e);
2025-02-07 11:03:05 +08:00
return Result.error("暂不支持此请求方式");
}
/**
* 参数类型错误
* @return Result
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public Result<String> methodArgument(MethodArgumentTypeMismatchException e){
2025-03-26 13:01:53 +08:00
log.warn("参数发生错误: ", e);
2025-02-07 11:03:05 +08:00
return Result.error("参数发生错误");
}
/**
* 缺少请求参数
* @param e 缺少请求参数异常
* @return Result
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public Result<String> exception(MissingServletRequestParameterException e){
2025-03-26 13:01:53 +08:00
log.warn("缺少请求参数: ", e);
2025-02-07 11:03:05 +08:00
return Result.error("缺少请求参数");
}
}