Spring Cloud 架构设计之优雅抛出异常

Spring Cloud 架构设计之如何优雅抛出异常

前言

近期,本人在开发一款互联网产品,项目地址https://github.com/yjjhkyq/doubi。同时,我也将通过连载的方式,将这款互联网产品的架构、技术细节等逐步记录下来,欢迎大家指正。

一、举例:用户名密码登陆接口

用户名密码登陆应该满足如下需求:

  • 根据用户输入的用户名查找用户信息,未查到则登陆不成功,并且给出错误信息
  • 如果用户没有被激活,则登陆不成功,并且给出错误信息
  • 如果用户密码不匹配,应该登陆不成功,并且给出错误信息

二、错误信息返回方式

  • 通过函数返回值返回错误信息(不推荐,原因就不解释了)
  • 通过抛出异常的方式给出错误信息(推荐

三、不推荐的实现方式

    @Override
    public String loginByPassword(LoginByPasswordAO userNamePasswordLoginAO) {
        Customer customer = getCustomer(0L, userNamePasswordLoginAO.getUserName(), userNamePasswordLoginAO.getPhone(), null);
        if(customer == null){
            throw new ApiException(UserResultCode.USER_NAME_OR_PWD_ERROR);
        }
        if (!customer.isActive()){
            throw new ApiException(UserResultCode.CUSTOMER_NOT_ACTIVE);
        }
        CustomerPassword customerPassword = getCustomerPassword(customer.getId());
        if (!passwordEncoderService.matches(userNamePasswordLoginAO.getPassword(), customerPassword.getPasswordSalt(), customerPassword.getPassword())){
            throw new ApiException(UserResultCode.USER_NAME_OR_PWD_ERROR);
        }
        String token = authenticationService.signIn(customer);
        return token;
    }

二、推荐实现方式

定义异常断言工具类

public abstract class ApiAssetUtil{

    public static void state(boolean expression) {
        if (!expression) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void state(boolean expression, IErrorCode errorCode) {
        if (!expression) {
            throw new ApiException(errorCode);
        }
    }

    public static void isTrue(boolean expression) {
        if (!expression) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void isTrue(boolean expression, IErrorCode errorCode) {
        if (!expression) {
            throw new ApiException(errorCode);
        }
    }

    public static void notTrue(boolean expression) {
        if (expression) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void notTrue(boolean expression, IErrorCode errorCode) {
        if (expression) {
            throw new ApiException(errorCode);
        }
    }

    public static void isNull(@Nullable Object object) {
        if (object != null) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void isNull(@Nullable Object object, IErrorCode errorCode) {
        if (object != null) {
            throw new ApiException(errorCode);
        }
    }

    public static void notNull(@Nullable Object object) {
        if (object == null) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void notNull(@Nullable Object object, IErrorCode errorCode) {
        if (object == null) {
            throw new ApiException(errorCode);
        }
    }

    public static void isStringEmpty(@Nullable String text) {
        if (!StringUtils.isEmpty(text)) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void isStringEmpty(@Nullable String text, IErrorCode errorCode) {
        if (!StringUtils.isEmpty(text)) {
            throw new ApiException(errorCode);
        }
    }

    public static void notStringEmpty(@Nullable String text) {
        if (StringUtils.isEmpty(text)) {
            throw new ApiException(ResultCode.FAILED);
        }
    }

    public static void notStringEmpty(@Nullable String text, IErrorCode errorCode) {
        if (StringUtils.isEmpty(text)) {
            throw new ApiException(errorCode);
        }
    }
}

优雅实现用户登陆接口

     @Override
    public String loginByPassword(LoginByPasswordAO userNamePasswordLoginAO) {
        Customer customer = getCustomer(0L, userNamePasswordLoginAO.getUserName(), userNamePasswordLoginAO.getPhone(), null);
        ApiAssetUtil.notNull(customer, UserResultCode.USER_NAME_OR_PWD_ERROR);
        ApiAssetUtil.isTrue(customer.isActive(), UserResultCode.CUSTOMER_NOT_ACTIVE);
        CustomerPassword customerPassword = getCustomerPassword(customer.getId());
        ApiAssetUtil.isTrue(passwordEncoderService.matches(userNamePasswordLoginAO.getPassword(), customerPassword.getPasswordSalt(), customerPassword.getPassword()), UserResultCode.USER_NAME_OR_PWD_ERROR);
        String token = authenticationService.signIn(customer);
        return token;
    }

结果显而易见,这种通过断言来抛出异常的方式让代码量减少了很多!

总结

写完了,弱弱的问下,可以帮我的开源项目点个赞码?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值