From 95d2e6747e0327ff261064d3fb34c7ce71a9a671 Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 2 Feb 2021 09:43:58 +0800 Subject: [PATCH] :bug: https://github.com/rymcu/forest/issues/30 --- .../forest/config/BaseExceptionHandler.java | 44 +++++++++++-------- .../core/exception/TransactionException.java | 13 ------ .../forest/enumerate/TransactionEnum.java | 6 +++ .../service/impl/SponsorServiceImpl.java | 2 +- .../impl/TransactionRecordServiceImpl.java | 4 +- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/rymcu/forest/config/BaseExceptionHandler.java b/src/main/java/com/rymcu/forest/config/BaseExceptionHandler.java index 5d60bf3..a73714f 100644 --- a/src/main/java/com/rymcu/forest/config/BaseExceptionHandler.java +++ b/src/main/java/com/rymcu/forest/config/BaseExceptionHandler.java @@ -2,8 +2,10 @@ package com.rymcu.forest.config; import com.alibaba.fastjson.support.spring.FastJsonJsonView; import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.exception.TransactionException; import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.ResultCode; +import com.rymcu.forest.enumerate.TransactionCode; import com.rymcu.forest.web.api.exception.BaseApiException; import org.apache.shiro.authz.UnauthenticatedException; import org.apache.shiro.authz.UnauthorizedException; @@ -25,7 +27,7 @@ import java.util.Map; * 全局异常处理器 * * @author ronger - * */ + */ @RestControllerAdvice public class BaseExceptionHandler { @@ -33,10 +35,10 @@ public class BaseExceptionHandler { @SuppressWarnings("Duplicates") @ExceptionHandler(Exception.class) - public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){ - if(isAjax(request)){ + public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + if (isAjax(request)) { GlobalResult result = new GlobalResult(); - if (ex instanceof BaseApiException){ + if (ex instanceof BaseApiException) { result.setCode(401); result.setMessage("用户未登录"); logger.info("用户未登录"); @@ -48,7 +50,7 @@ public class BaseExceptionHandler { result.setCode(1000002); result.setMessage("用户无权限"); logger.info("用户无权限"); - }else if (ex instanceof ServiceException) { + } else if (ex instanceof ServiceException) { //业务失败的异常,如“账号或密码错误” result.setCode(((ServiceException) ex).getCode()); result.setMessage(ex.getMessage()); @@ -59,7 +61,10 @@ public class BaseExceptionHandler { } else if (ex instanceof ServletException) { result.setCode(ResultCode.FAIL.getCode()); result.setMessage(ex.getMessage()); - }else { + } else if (ex instanceof TransactionException) { + result.setCode(TransactionCode.InsufficientBalance.getCode()); + result.setMessage(ex.getMessage()); + } else { //系统内部异常,不返回给客户端,内部记录错误日志 result.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode()); String message; @@ -78,11 +83,11 @@ public class BaseExceptionHandler { } result.setSuccess(false); return result; - }else { + } else { ModelAndView mv = new ModelAndView(); FastJsonJsonView view = new FastJsonJsonView(); Map attributes = new HashMap(2); - if (ex instanceof BaseApiException){ + if (ex instanceof BaseApiException) { attributes.put("code", "401"); attributes.put("message", "用户未登录"); } else if (ex instanceof UnauthenticatedException) { @@ -93,18 +98,21 @@ public class BaseExceptionHandler { attributes.put("message", "用户无权限"); } else if (ex instanceof ServiceException) { //业务失败的异常,如“账号或密码错误” - attributes.put("code",((ServiceException) ex).getCode()); - attributes.put("message",ex.getMessage()); + attributes.put("code", ((ServiceException) ex).getCode()); + attributes.put("message", ex.getMessage()); logger.info(ex.getMessage()); } else if (ex instanceof NoHandlerFoundException) { - attributes.put("code",ResultCode.NOT_FOUND.getCode()); - attributes.put("message",ResultCode.NOT_FOUND.getMessage()); + attributes.put("code", ResultCode.NOT_FOUND.getCode()); + attributes.put("message", ResultCode.NOT_FOUND.getMessage()); } else if (ex instanceof ServletException) { - attributes.put("code",ResultCode.FAIL.getCode()); - attributes.put("message",ex.getMessage()); - }else { + attributes.put("code", ResultCode.FAIL.getCode()); + attributes.put("message", ex.getMessage()); + } else if (ex instanceof TransactionException) { + attributes.put("code", TransactionCode.InsufficientBalance.getCode()); + attributes.put("message", ex.getMessage()); + } else { //系统内部异常,不返回给客户端,内部记录错误日志 - attributes.put("code",ResultCode.INTERNAL_SERVER_ERROR.getCode()); + attributes.put("code", ResultCode.INTERNAL_SERVER_ERROR.getCode()); String message; if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; @@ -117,9 +125,9 @@ public class BaseExceptionHandler { message = ex.getMessage(); } logger.error(message, ex); - attributes.put("message","操作失败"); + attributes.put("message", "操作失败"); } - attributes.put("success",false); + attributes.put("success", false); view.setAttributesMap(attributes); mv.setView(view); return mv; diff --git a/src/main/java/com/rymcu/forest/core/exception/TransactionException.java b/src/main/java/com/rymcu/forest/core/exception/TransactionException.java index 16e66b2..5ecb5dd 100644 --- a/src/main/java/com/rymcu/forest/core/exception/TransactionException.java +++ b/src/main/java/com/rymcu/forest/core/exception/TransactionException.java @@ -19,17 +19,4 @@ public class TransactionException extends Exception { public int getCode() { return code; } - - public void setCode(int code) { - this.code = code; - } - - @Override - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } } diff --git a/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java b/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java index 2eb7202..e8eb801 100644 --- a/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java +++ b/src/main/java/com/rymcu/forest/enumerate/TransactionEnum.java @@ -1,5 +1,7 @@ package com.rymcu.forest.enumerate; +import java.util.Arrays; + /** * @author ronger */ @@ -18,6 +20,10 @@ public enum TransactionEnum { this.money = money; } + public static TransactionEnum findTransactionEnum(String dataType) { + return Arrays.stream(TransactionEnum.values()).filter(transactionEnum -> transactionEnum.getDataType().equals(dataType)).findFirst().orElse(TransactionEnum.ArticleSponsor); + } + public String getDataType() { return this.dataType; } diff --git a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java index 9401c38..e6555ff 100644 --- a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java @@ -41,7 +41,7 @@ public class SponsorServiceImpl extends AbstractService implements Spon map.put("success", false); map.put("message", "数据异常"); } else { - TransactionEnum result = TransactionEnum.valueOf(sponsor.getDataType()); + TransactionEnum result = TransactionEnum.findTransactionEnum(sponsor.getDataType()); BigDecimal money = BigDecimal.valueOf(result.getMoney()); sponsor.setSponsorshipMoney(money); User user = UserUtils.getCurrentUserByToken(); diff --git a/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java index 7925da4..94c16da 100644 --- a/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java @@ -1,12 +1,14 @@ package com.rymcu.forest.service.impl; import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.exception.TransactionException; import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.core.service.redis.RedisService; import com.rymcu.forest.dto.BankAccountDTO; import com.rymcu.forest.dto.TransactionRecordDTO; import com.rymcu.forest.entity.BankAccount; import com.rymcu.forest.entity.TransactionRecord; +import com.rymcu.forest.enumerate.TransactionCode; import com.rymcu.forest.mapper.TransactionRecordMapper; import com.rymcu.forest.service.BankAccountService; import com.rymcu.forest.service.TransactionRecordService; @@ -47,7 +49,7 @@ public class TransactionRecordServiceImpl extends AbstractService