This commit is contained in:
ronger 2021-02-02 09:43:58 +08:00
parent f43689a7e9
commit 95d2e6747e
5 changed files with 36 additions and 33 deletions

View File

@ -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<String, Object> 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;

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -41,7 +41,7 @@ public class SponsorServiceImpl extends AbstractService<Sponsor> 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();

View File

@ -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<TransactionRec
transactionRecordMapper.insertSelective(transactionRecord);
}
} else {
throw new Exception("余额不足");
throw new TransactionException(TransactionCode.InsufficientBalance);
}
return transactionRecord;
}