@Data
public class ErrorResult implements Serializable {
private Integer code;
private String message;
private boolean success = false;
@JsonIgnore
private ResultCode resultCode;
public static ErrorResult error() {
ErrorResult result = new ErrorResult();
result.setResultCode(ResultCode.INTERNAL_ERROR);
return result;
}
public static ErrorResult error(String message) {
ErrorResult result = new ErrorResult();
result.setCode(ResultCode.INTERNAL_ERROR.code());
result.setMessage(message);
return result;
}
public static ErrorResult error(Integer code, String message) {
ErrorResult result = new ErrorResult();
result.setCode(code);
result.setMessage(message);
return result;
}
public static ErrorResult error(ResultCode resultCode, String message) {
ErrorResult result = new ErrorResult();
result.setResultCode(resultCode);
result.setMessage(message)
return result;
}
}
@Data
public class BaseException extends RuntimeException {
private static final int BASE_EXCEPTION_CODE = ResultCode.INTERNAL_ERROR.code();
private static final String BASE_EXCEPTION_MESSAGE = ResultCode.INTERNAL_ERROR.message();
private Integer code;
private String message;
public BaseException() {
super(BASE_EXCEPTION_MESSAGE);
this.code = BASE_EXCEPTION_CODE;
this.message = BASE_EXCEPTION_MESSAGE;
}
public BaseException(String message) {
super(message);
this.code = BASE_EXCEPTION_CODE;
this.message = message;
}
public BaseException(ResultCode resultCode) {
super(resultCode.message());
this.code = resultCode.code();
this.message = resultCode.message();
}
public BaseException(Throwable cause) {
super(cause);
this.code = BASE_EXCEPTION_CODE;
this.message = BASE_EXCEPTION_MESSAGE;
}
public BaseException(String message, Throwable cause) {
super(message, cause);
this.code = BASE_EXCEPTION_CODE;
this.message = message;
}
public BaseException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
}
public BaseException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
this.message = message;
}
}
public class BizException extends BaseException {
public BizException(ResultCode resultCode) {
super(resultCode);
}
}
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 统一处理自定义基础异常
*/
@ExceptionHandler(BaseException.class)
public ErrorResult baseException(BaseException e) {
if (StringUtils.isEmpty(e.getCode())) {
return ErrorResult.error(e.getMessage());
}
return ErrorResult.error(e.getCode(), e.getMessage());
}
/**
* 统一处理自定义业务异常
*/
@ExceptionHandler(BizException.class)
public ErrorResult bizException(BizException e) {
if (StringUtils.isEmpty(e.getCode())) {
return ErrorResult.error(e.getMessage());
}
return ErrorResult.error(e.getCode(), e.getMessage());
}
/**
* 统一处理非自定义异常外的所有异常
*/
@ExceptionHandler(Exception.class)
public ErrorResult handleException(Exception e) {
log.error(e.getMessage(), e);
return ErrorResult.error(e.getMessage());
}
/**
* 兼容Validation校验框架:忽略参数异常处理器
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public ApiResult<String> parameterMissingExceptionHandler(MissingServletRequestParameterException e) {
log.error(e.getMessage(), e);
return ErrorResult.error(PARAMETER_EXCEPTION, "请求参数 " + e.getParameterName() + " 不能为空");
}
/**
* 兼容Validation校验框架:缺少请求体异常处理器
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public ErrorResult parameterBodyMissingExceptionHandler(HttpMessageNotReadableException e) {
log.error(e.getMessage(), e);
return ErrorResult.error(PARAMETER_EXCEPTION, "参数体不能为空");
}
/**
* 兼容Validation校验框架:参数效验异常处理器
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ErrorResult parameterExceptionHandler(MethodArgumentNotValidException e) {
log.error(e.getMessage(), e);
// 获取异常信息
BindingResult exceptions = e.getBindingResult();
// 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
if (exceptions.hasErrors()) {
List<ObjectError> errors = exceptions.getAllErrors();
if (!errors.isEmpty()) {
// 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
FieldError fieldError = (FieldError) errors.get(0);
return ErrorResult.error(PARAMETER_EXCEPTION, fieldError.getDefaultMessage());
}
}
return ErrorResult.error(PARAMETER_EXCEPTION, "请求参数校验异常");
}
}