/**
* 自定义异常类
*
* @author WangYaoLong
*/
public class BusinessException extends RuntimeException {
private final int code;
public BusinessException(int code, String message) {
super(message);
this.code = code;
}
public BusinessException(String msg){
this.code = ErrorCode.SUCCESS.getCode();
}
public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}
public BusinessException(ErrorCode errorCode, String message) {
super(message);
this.code = errorCode.getCode();
}
public int getCode() {
return code;
}
}
/**
* @author WangYaoLong
* create_time 2022/7/25 19:38
* comment 异常处理
*/
@Slf4j
@ControllerAdvice
public class DefaultExceptionHandler {
public DefaultExceptionHandler() {
}
@ExceptionHandler
@ResponseBody
public Result<Object> handleException(Exception ex) {
if (ex instanceof BusinessException) {
Result<Object> failed = Result.failed(((BusinessException) ex).getMessage());
failed.setCode(((BusinessException) ex).getCode());
log.error("", ex);
return failed;
} else if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException e = (MethodArgumentNotValidException) ex;
BindingResult bindingResult = e.getBindingResult();
StringBuilder sb = new StringBuilder();
for (FieldError fieldError : bindingResult.getFieldErrors()) {
sb.append(fieldError.getDefaultMessage()).append(" ");
}
if (CollectionUtils.isEmpty(bindingResult.getFieldErrors())) {
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError objectError : allErrors) {
sb.append(objectError.getDefaultMessage()).append(" ");
}
return Result.failed(sb.toString());
}
String field = Objects.requireNonNull(bindingResult.getFieldError()).getField();
log.error(sb.toString());
return Result.failed("[" + field + "]" + sb);
} else if (ex instanceof BindException) {
BindException bindException = (BindException) ex;
BindingResult bindingResult = bindException.getBindingResult();
String field = Objects.requireNonNull(bindingResult.getFieldError()).getField();
StringBuilder sb = new StringBuilder();
for (FieldError fieldError : bindingResult.getFieldErrors()) {
sb.append(fieldError.getDefaultMessage()).append(" ");
}
log.error(sb.toString());
return Result.failed("[" + field + "]" + sb);
} else if (ex instanceof MethodArgumentTypeMismatchException) {
Result<Object> failed = Result.failed();
failed.setCode(-1);
failed.setMsg("类型转换错误,请填写正确的请求参数");
log.error(ex.getMessage());
return failed;
} else if (ex instanceof ExcelAnalysisException) {
log.error(ex.getMessage());
Result<Object> failed = com.wang.dog.common.Result.failed();
failed.setCode(2);
failed.setMsg(ex.getMessage());
return failed;
} else if (ex instanceof AccessDeniedException) {
log.error(ex.getMessage());
Result<Object> failed = Result.failed();
failed.setCode(1);
failed.setMsg("权限不足,无法访问");
return failed;
} else {
log.error(ex.getMessage(), ex);
return Result.failed(ex.getMessage());
}
}
}
全局异常捕获类 GlobalExceptionHandler :
/**
* 全局异常处理器
*
* @author WangYaoLong
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public Result<?> businessExceptionHandler(BusinessException e) {
log.error("businessException: " + e.getMessage(), e);
return Result.failed(e.getCode(), e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public Result<?> runtimeExceptionHandler(RuntimeException e) {
log.error("runtimeException", e);
return Result.failed(ErrorCode.SYSTEM_ERROR, e.getMessage());
}
}