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.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.rymcu.forest.core.exception.ServiceException; 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.GlobalResult;
import com.rymcu.forest.core.result.ResultCode; import com.rymcu.forest.core.result.ResultCode;
import com.rymcu.forest.enumerate.TransactionCode;
import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.web.api.exception.BaseApiException;
import org.apache.shiro.authz.UnauthenticatedException; import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException; import org.apache.shiro.authz.UnauthorizedException;
@ -25,7 +27,7 @@ import java.util.Map;
* 全局异常处理器 * 全局异常处理器
* *
* @author ronger * @author ronger
* */ */
@RestControllerAdvice @RestControllerAdvice
public class BaseExceptionHandler { public class BaseExceptionHandler {
@ -59,6 +61,9 @@ public class BaseExceptionHandler {
} else if (ex instanceof ServletException) { } else if (ex instanceof ServletException) {
result.setCode(ResultCode.FAIL.getCode()); result.setCode(ResultCode.FAIL.getCode());
result.setMessage(ex.getMessage()); result.setMessage(ex.getMessage());
} else if (ex instanceof TransactionException) {
result.setCode(TransactionCode.InsufficientBalance.getCode());
result.setMessage(ex.getMessage());
} else { } else {
//系统内部异常,不返回给客户端,内部记录错误日志 //系统内部异常,不返回给客户端,内部记录错误日志
result.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode()); result.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
@ -102,6 +107,9 @@ public class BaseExceptionHandler {
} else if (ex instanceof ServletException) { } else if (ex instanceof ServletException) {
attributes.put("code", ResultCode.FAIL.getCode()); attributes.put("code", ResultCode.FAIL.getCode());
attributes.put("message", ex.getMessage()); attributes.put("message", ex.getMessage());
} else if (ex instanceof TransactionException) {
attributes.put("code", TransactionCode.InsufficientBalance.getCode());
attributes.put("message", ex.getMessage());
} else { } else {
//系统内部异常,不返回给客户端,内部记录错误日志 //系统内部异常,不返回给客户端,内部记录错误日志
attributes.put("code", ResultCode.INTERNAL_SERVER_ERROR.getCode()); attributes.put("code", ResultCode.INTERNAL_SERVER_ERROR.getCode());

View File

@ -19,17 +19,4 @@ public class TransactionException extends Exception {
public int getCode() { public int getCode() {
return code; 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; package com.rymcu.forest.enumerate;
import java.util.Arrays;
/** /**
* @author ronger * @author ronger
*/ */
@ -18,6 +20,10 @@ public enum TransactionEnum {
this.money = money; 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() { public String getDataType() {
return this.dataType; return this.dataType;
} }

View File

@ -41,7 +41,7 @@ public class SponsorServiceImpl extends AbstractService<Sponsor> implements Spon
map.put("success", false); map.put("success", false);
map.put("message", "数据异常"); map.put("message", "数据异常");
} else { } else {
TransactionEnum result = TransactionEnum.valueOf(sponsor.getDataType()); TransactionEnum result = TransactionEnum.findTransactionEnum(sponsor.getDataType());
BigDecimal money = BigDecimal.valueOf(result.getMoney()); BigDecimal money = BigDecimal.valueOf(result.getMoney());
sponsor.setSponsorshipMoney(money); sponsor.setSponsorshipMoney(money);
User user = UserUtils.getCurrentUserByToken(); User user = UserUtils.getCurrentUserByToken();

View File

@ -1,12 +1,14 @@
package com.rymcu.forest.service.impl; package com.rymcu.forest.service.impl;
import com.rymcu.forest.core.exception.ServiceException; 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.AbstractService;
import com.rymcu.forest.core.service.redis.RedisService; import com.rymcu.forest.core.service.redis.RedisService;
import com.rymcu.forest.dto.BankAccountDTO; import com.rymcu.forest.dto.BankAccountDTO;
import com.rymcu.forest.dto.TransactionRecordDTO; import com.rymcu.forest.dto.TransactionRecordDTO;
import com.rymcu.forest.entity.BankAccount; import com.rymcu.forest.entity.BankAccount;
import com.rymcu.forest.entity.TransactionRecord; import com.rymcu.forest.entity.TransactionRecord;
import com.rymcu.forest.enumerate.TransactionCode;
import com.rymcu.forest.mapper.TransactionRecordMapper; import com.rymcu.forest.mapper.TransactionRecordMapper;
import com.rymcu.forest.service.BankAccountService; import com.rymcu.forest.service.BankAccountService;
import com.rymcu.forest.service.TransactionRecordService; import com.rymcu.forest.service.TransactionRecordService;
@ -47,7 +49,7 @@ public class TransactionRecordServiceImpl extends AbstractService<TransactionRec
transactionRecordMapper.insertSelective(transactionRecord); transactionRecordMapper.insertSelective(transactionRecord);
} }
} else { } else {
throw new Exception("余额不足"); throw new TransactionException(TransactionCode.InsufficientBalance);
} }
return transactionRecord; return transactionRecord;
} }