自定义断言 判断条件抛出异常优化代码

1.自定义工具 

2.自定义异常处理
3.全局异常处理

效果图

案例一

案例二

再也不用写 

if (availableByUid == null) {
    throw new IllegalArgumentException("该月卡限额已用完");
}

以后直接写

 SysUtils.isNotNullExp(availableByUid,"该月卡限额已用完");

自定义工具

import lombok.experimental.UtilityClass;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 系统工具类
 * </p>

 * @author: Aiyex
 */
@UtilityClass
public class SysUtils {
    /**
     * 判断object是否为空,集合会校验size
     * @param objs
     * @return boolean
     */
    public boolean isNull(Object... objs) {
        for (Object obj : objs) {
            if (isEmpty(obj)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 不是空的 抛出异常
     */
    public void isNullExp(Object obj,String msg){
        if (isNull(obj)) {
            return;
        }
        throw new ServiceException(msg);
    }

    /**
     * 是空的 抛出异常
     */
    public void isNotNullExp(Object obj,String msg){
        if (!isNull(obj)) {
            return;
        }
        throw new ServiceException(msg);
    }

    /**
     * 非true 抛出异常
     */
    public void isTrueExp(Boolean isTrue,String msg){
        if (isTrue) {
            return;
        }
        throw new ServiceException(msg);
    }
    /**
     * true 抛出异常
     */
    public void isFalseExp(Boolean isTrue,String msg){
        if (!isTrue) {
            return;
        }
        throw new ServiceException(msg);
    }


    /**
     * 判断object是否不为空,集合会校验size
     * @param obj
     * @return boolean
     */
    public boolean isNotNull(Object... obj) {
        return !isNull(obj);
    }

    /**
     * 对象非空判断
     * @param obj
     * @return boolean
     */
    public boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 对象空判断
     * @param obj
     * @return boolean
     */
    public boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }

        return false;
    }
}

注意 ServiceException 是自己定义的异常处理类

自定义异常处理

/**
 * 业务异常
 *
 * @author Aiyex
 */
public final class ServiceException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 错误明细,内部调试错误
     *
     */
    private String detailMessage;

    /**
     * 空构造方法,避免反序列化问题
     */
    public ServiceException() {
    }

    public ServiceException(String message) {
        this.message = message;
    }

    public ServiceException(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    public String getDetailMessage()
    {
        return detailMessage;
    }

    @Override
    public String getMessage()
    {
        return message;
    }

    public Integer getCode()
    {
        return code;
    }

    public ServiceException setMessage(String message) {
        this.message = message;
        return this;
    }

    public ServiceException setDetailMessage(String detailMessage) {
        this.detailMessage = detailMessage;
        return this;
    }
}

 全局异常处理

/**
 * 全局异常处理器
 * @author Aiyex
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);




    /**
     * 业务异常  --- 主要是这个
     */
    @ExceptionHandler(ServiceException.class)
    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
        log.error(e.getMessage(), e);
        Integer code = e.getCode();
        return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

   

    
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值