package com.dy.pmsGlobal.aop;
|
|
import com.dy.common.webUtil.BaseResponse;
|
import com.dy.common.webUtil.BaseResponseUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
@ResponseBody
|
@RestControllerAdvice
|
@Slf4j
|
public class ExceptionHandlerAdvice {
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
public BaseResponse<?> handleValidationExceptions(MethodArgumentNotValidException ex) {
|
log.error("[handleValidationExceptions]", ex);
|
StringBuilder sb = new StringBuilder();
|
ex.getBindingResult().getAllErrors().forEach(error -> {
|
// String fieldName = ((org.springframework.validation.FieldError) error).getField();
|
String errorMessage = error.getDefaultMessage();
|
sb.append(errorMessage).append(";");
|
});
|
return BaseResponseUtils.buildException(sb.substring(0, sb.length() - 1));
|
}
|
/**
|
* 处理系统异常,兜底处理所有异常
|
*/
|
@ExceptionHandler(value = Exception.class)
|
public BaseResponse<?> defaultExceptionHandler(Throwable ex) {
|
log.error("[defaultExceptionHandler]", ex);
|
// 返回 ERROR
|
return BaseResponseUtils.buildException(ex.getMessage());
|
}
|
|
}
|