自定义 过分请求异常

This commit is contained in:
GYJ
2024-12-28 17:39:19 +08:00
parent 0f12ed5161
commit 3b5c8f0af4
4 changed files with 30 additions and 5 deletions

View File

@@ -0,0 +1,10 @@
package com.sqx.common.exception;
/**
* @author GYJoker
*/
public class CzgException extends Exception {
public CzgException(String message) {
super(message);
}
}

View File

@@ -5,6 +5,8 @@ import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -68,6 +70,12 @@ public class SqxExceptionHandler {
return Result.error();
}
@ExceptionHandler(CzgException.class)
public ResponseEntity<Void> handleCzgException() {
// 响应状态码为 444 表示无法处理的异常
return new ResponseEntity<>(HttpStatus.TOO_MANY_REQUESTS);
}
private void logErrorInfo(WebRequest webRequest) {
if (webRequest instanceof ServletWebRequest) {
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;

View File

@@ -1,4 +1,5 @@
package com.sqx.modules.callback;
import com.sqx.common.exception.CzgException;
import com.sqx.modules.callback.service.UniAdCallbackRecordService;
import lombok.extern.slf4j.Slf4j;
@@ -31,4 +32,9 @@ public class UniCallBackController {
log.info("接收到uni-ad广告完播回调回调信息: {}", dto);
return ResponseEntity.ok(uniCallBackService.adCallBack(dto));
}
@GetMapping("/testException")
public String testException() throws CzgException {
throw new CzgException("测试异常");
}
}

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.annotation.Debounce;
import com.sqx.common.exception.CzgException;
import com.sqx.common.utils.*;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.service.UserService;
@@ -165,7 +166,7 @@ public class DiscSpinningController {
})
@ApiOperation("抽取大转盘")
@Debounce(interval = 3000, value = "#userId")
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) {
public Result draw(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam(required = false, defaultValue = "1") Integer source) throws CzgException {
double amount = 0;
Long sourceId = null;
Integer i = recordService.countDraw(userId);
@@ -174,11 +175,11 @@ public class DiscSpinningController {
//订单抽奖
int drawCount = Integer.parseInt(commonRepository.findOne(901).getValue());
if (i != null && i >= drawCount) {
return Result.error("当日可抽奖次数已超限");
throw new CzgException("当日可抽奖次数已超限");
}
Orders orders = ordersService.selectOrdersByDay(userId);
if (orders == null) {
return Result.error("无可抽奖机会");
throw new CzgException("无可抽奖机会");
}
amount = orders.getPayMoney().doubleValue();
sourceId = orders.getOrdersId();
@@ -218,14 +219,14 @@ public class DiscSpinningController {
redisUtils.set(redisKey, week, DateUtils.todayAfterSecond());
}
if (source == 1 && sourceId == null) {
throw new RuntimeException("异常请求");
throw new CzgException("异常请求");
}
if (DataLimitUtil.isAllowed(RedisKeys.getDateKey("spinning:draw:") + sourceId, 120)) {
DiscSpinningRecord draws = discSpinningService.draws(i == null ? 1 : i + 1, amount, sourceId, userId, source);
executor.schedule(() -> discSpinningService.receive1(draws), 4, TimeUnit.SECONDS);
return new Result().put("data", draws);
} else {
throw new RuntimeException("异常请求");
throw new CzgException("异常请求");
}
}