From 0cfec407a084b430d88b6086795de91049c2a42f Mon Sep 17 00:00:00 2001 From: lepdou Date: Tue, 10 May 2022 19:16:16 +0800 Subject: [PATCH] fix custom ratelimit reject httpcode --- .../ratelimit/filter/QuotaCheckReactiveFilter.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 9a0db9a77..fc0a1e9f3 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -40,6 +40,7 @@ import reactor.core.publisher.Mono; import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.CollectionUtils; @@ -118,7 +119,15 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) { ServerHttpResponse response = exchange.getResponse(); - response.setRawStatusCode(polarisRateLimitProperties.getRejectHttpCode()); + HttpStatus httpStatus; + try { + httpStatus = HttpStatus.valueOf(polarisRateLimitProperties.getRejectHttpCode()); + } + catch (IllegalArgumentException e) { + LOG.error("Illegal custom reject http code, will fallback to default http code 429 [TOO_MANY_REQUESTS]"); + httpStatus = HttpStatus.TOO_MANY_REQUESTS; + } + response.setStatusCode(httpStatus); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); DataBuffer dataBuffer = response.bufferFactory().allocateBuffer() .write(rejectTips.getBytes(StandardCharsets.UTF_8));