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;
}
结果显而易见,这种通过断言来抛出异常的方式让代码量减少了很多!
总结
写完了,弱弱的问下,可以帮我的开源项目点个赞码?