异常统一返回
Assert.java
import com.ljm.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Slf4j
public abstract class Assert {
public static void notNull(Object obj, ResponseEnum responseEnum) {
if (obj == null) {
log.info("obj is null...............");
throw new BusinessException(responseEnum);
}
}
public static void isNull(Object object, ResponseEnum responseEnum) {
if (object != null) {
log.info("obj is not null......");
throw new BusinessException(responseEnum);
}
}
public static void isTrue(boolean expression, ResponseEnum responseEnum) {
if (!expression) {
log.info("fail...............");
throw new BusinessException(responseEnum);
}
}
public static void notEquals(Object m1, Object m2, ResponseEnum responseEnum) {
if (m1.equals(m2)) {
log.info("equals...............");
throw new BusinessException(responseEnum);
}
}
public static void equals(Object m1, Object m2, ResponseEnum responseEnum) {
if (!m1.equals(m2)) {
log.info("not equals...............");
throw new BusinessException(responseEnum);
}
}
public static void notEmpty(String s, ResponseEnum responseEnum) {
if (StringUtils.isEmpty(s)) {
log.info("is empty...............");
throw new BusinessException(responseEnum);
}
}
}
BusinessException.java
import com.ljm.result.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
private Integer code;
private String message;
public BusinessException(String message) {
this.message = message;
}
public BusinessException(String message, Integer code) {
this.message = message;
this.code = code;
}
public BusinessException(String message, Integer code, Throwable cause) {
super(cause);
this.message = message;
this.code = code;
}
public BusinessException(ResponseEnum resultCodeEnum) {
this.message = resultCodeEnum.getMessage();
this.code = resultCodeEnum.getCode();
}
public BusinessException(ResponseEnum resultCodeEnum, Throwable cause) {
super(cause);
this.message = resultCodeEnum.getMessage();
this.code = resultCodeEnum.getCode();
}
}
UnifiedExceptionHandler.java
import com.ljm.result.ResponseEnum;
import com.ljm.result.ResultOut;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@Component
@RestControllerAdvice
public class UnifiedExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ResultOut handleException(Exception e) {
log.error(e.getMessage(), e);
return ResultOut.error();
}
@ExceptionHandler(NullPointerException.class)
public ResultOut NullPointerGrammarException(NullPointerException e) {
log.error(e.getMessage(), e);
return ResultOut.setResult(ResponseEnum.NULL_POINT);
}
@ExceptionHandler(ArithmeticException.class)
public ResultOut ArithmeticGrammarException(ArithmeticException e) {
log.error(e.getMessage(), e);
return ResultOut.setResult(ResponseEnum.ARITHMETIC_BY);
}
@ExceptionHandler(BusinessException.class)
public ResultOut handleBusinessException(BusinessException e) {
log.error(e.getMessage(), e);
return ResultOut.error().message(e.getMessage()).code(e.getCode());
}
}
over