Skip to content

Commit

Permalink
Refactor: [编码规范]自定义异常类型(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed Apr 16, 2021
1 parent 3634e3a commit c4d5519
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.walkers.planes.fundhelper.config;

/**
* 自定义异常类
*
* @author planeswalker23
*/
public class FundHelperException extends RuntimeException {

public FundHelperException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
public class FundHelperExceptionHandler {

/**
* 拦截捕捉业务性自定义异常 {@link RuntimeException}
* 拦截捕捉业务性自定义异常 {@link FundHelperException}
*
* @param e 业务性自定义异常
* @return {@link Response}
*/
@ExceptionHandler(value = RuntimeException.class)
public Response<String> windfallExceptionHandler(RuntimeException e) {
@ExceptionHandler(value = FundHelperException.class)
public Response<String> windfallExceptionHandler(FundHelperException e) {
log.warn(e.getMessage());
return Response.failed(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
forTest();
}
SessionUtil.getLoginUser();
} catch (RuntimeException e) {
} catch (FundHelperException e) {
log.error("访问{{}}页面出错,原因是:{}", request.getServletPath(), e.getMessage());
response.sendRedirect(request.getContextPath() + "/");
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.walkers.planes.fundhelper.entity.dict;

import io.walkers.planes.fundhelper.config.FundHelperException;

/**
* Dict of Fund Type
*
Expand Down Expand Up @@ -53,6 +55,6 @@ public static FundTypeDict containsValue(String typeName) {
return type;
}
}
throw new RuntimeException(String.format(MessageDict.FUND_TYPE_NOT_SUPPORT, typeName));
throw new FundHelperException(String.format(MessageDict.FUND_TYPE_NOT_SUPPORT, typeName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.walkers.planes.fundhelper.config.FundDataSource;
import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.dao.FundValueDao;
import io.walkers.planes.fundhelper.entity.dict.FundTypeDict;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
Expand Down Expand Up @@ -65,7 +66,7 @@ public FundModel fetchFundByCode(String code) {
result.setEstablishDate(establishDateNode.outerHtml().replace(":", ""));
} catch (Exception e) {
log.error("获取基金详情失败,路由为:{},原因: {}", path, e.getMessage(), e);
throw new RuntimeException(String.format(MessageDict.DOWNLOAD_FUND_VALUE_FAILED, code));
throw new FundHelperException(String.format(MessageDict.DOWNLOAD_FUND_VALUE_FAILED, code));
}
FundTypeDict fundType = FundTypeDict.containsValue(result.getType());
return FundModel.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.walkers.planes.fundhelper.service;

import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.dao.OptionalFundRelationDao;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
import io.walkers.planes.fundhelper.entity.model.FundModel;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void addOptionalFundRelation(FundModel fund) {
OptionalFundRelationModel alreadyAdd = optionalFundRelationDao.selectOne(fund.getId(), virtualUser.getId());
if (alreadyAdd != null) {
log.warn("虚拟用户{{}}已将基金{{}}加入自选", virtualUser.getAccount(), fund.getCode());
throw new RuntimeException(String.format(MessageDict.FUND_ALREADY_EXIST_IN_OPTIONAL, fund.getCode()));
throw new FundHelperException(String.format(MessageDict.FUND_ALREADY_EXIST_IN_OPTIONAL, fund.getCode()));
}

OptionalFundRelationModel optionalFundRelation = OptionalFundRelationModel.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.walkers.planes.fundhelper.service;

import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.dao.VirtualUserDao;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
import io.walkers.planes.fundhelper.entity.model.VirtualUserModel;
Expand Down Expand Up @@ -33,7 +34,7 @@ public VirtualUserModel login(VirtualUserModel virtualUser) {
userResult = virtualUser;
}
if (!userResult.getPassword().equals(virtualUser.getPassword())) {
throw new RuntimeException(MessageDict.WRONG_PASSWORD);
throw new FundHelperException(MessageDict.WRONG_PASSWORD);
}
// 基于 session 保存登录状态
SessionUtil.updateAttribute("loginUser", userResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
import io.walkers.planes.fundhelper.entity.dict.TimeFormatDict;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static String toJson(Object object) {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("JSON序列化失败,目标类型 {},源数据 {},错误原因:{}", String.class.getName(), object, e.getMessage(), e);
throw new RuntimeException(MessageDict.JSON_FORMAT_FAILED);
throw new FundHelperException(MessageDict.JSON_FORMAT_FAILED);
}
}

Expand All @@ -54,7 +55,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
return objectMapper.readValue(json, clazz);
} catch (IOException e) {
log.error("JSON序列化失败,目标类型 {},源数据 {},错误原因:{}", clazz.getName(), json, e.getMessage(), e);
throw new RuntimeException(MessageDict.JSON_FORMAT_FAILED);
throw new FundHelperException(MessageDict.JSON_FORMAT_FAILED);
}
}

Expand All @@ -72,7 +73,7 @@ public static <T> List<T> parseArray(String json, Class<? extends T> clazz) {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
log.error("JSON序列化失败,目标类型 {} 数组,源数据 {},错误原因:{}", clazz.getName(), json, e.getMessage(), e);
throw new RuntimeException(MessageDict.JSON_FORMAT_FAILED);
throw new FundHelperException(MessageDict.JSON_FORMAT_FAILED);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.walkers.planes.fundhelper.util;

import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
import io.walkers.planes.fundhelper.entity.model.VirtualUserModel;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -35,7 +36,7 @@ private static HttpSession getSession() {
public static VirtualUserModel getLoginUser() {
VirtualUserModel user = (VirtualUserModel) getSession().getAttribute("loginUser");
if (user == null) {
throw new RuntimeException(MessageDict.NOT_LOGIN);
throw new FundHelperException(MessageDict.NOT_LOGIN);
}
return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.walkers.planes.fundhelper.util;

import io.walkers.planes.fundhelper.config.FundHelperException;
import io.walkers.planes.fundhelper.entity.dict.DateTypeDict;
import io.walkers.planes.fundhelper.entity.dict.MessageDict;
import io.walkers.planes.fundhelper.entity.dict.TimeFormatDict;
Expand Down Expand Up @@ -40,7 +41,7 @@ public static java.util.Date string2JavaUtilDate(String timeString) {
time = SQL_DATE_FORMAT_THREAD_LOCAL.get().parse(timeString.substring(0, 10));
} catch (Exception e) {
log.error("日期格式化失败,源数据为:{},错误原因:{}", timeString, e);
throw new RuntimeException(MessageDict.DATE_FORMAT_FAILED);
throw new FundHelperException(MessageDict.DATE_FORMAT_FAILED);
}
return time;
}
Expand Down

0 comments on commit c4d5519

Please sign in to comment.