From da06a010974f9f4fe28820785eb3cdedf0ca5253 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 3 Jul 2024 00:39:20 +0100 Subject: [PATCH] fix WebRequest#getAsEntity --- .../cc/fascinated/bat/common/WebRequest.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/WebRequest.java b/src/main/java/cc/fascinated/bat/common/WebRequest.java index 5ff5841..d316e78 100644 --- a/src/main/java/cc/fascinated/bat/common/WebRequest.java +++ b/src/main/java/cc/fascinated/bat/common/WebRequest.java @@ -31,20 +31,26 @@ public class WebRequest { * @return the response */ public static T getAsEntity(String url, Class clazz) throws RateLimitException { - ResponseEntity responseEntity = CLIENT.get() - .uri(url) - .retrieve() - .onStatus(HttpStatusCode::isError, (request, response) -> { - }) // Don't throw exceptions on error - .toEntity(clazz); + try { + ResponseEntity responseEntity = CLIENT.get() + .uri(url) + .retrieve() + .onStatus(HttpStatusCode::isError, (request, response) -> { + }) // Don't throw exceptions on error + .toEntity(clazz); - if (responseEntity.getStatusCode().isError()) { + if (responseEntity.getStatusCode().isError()) { + return null; + } + if (responseEntity.getStatusCode().isSameCodeAs(HttpStatus.TOO_MANY_REQUESTS)) { + throw new RateLimitException("Rate limit reached"); + } + return responseEntity.getBody(); + } catch (RateLimitException e) { + throw e; + } catch (Exception e) { return null; } - if (responseEntity.getStatusCode().isSameCodeAs(HttpStatus.TOO_MANY_REQUESTS)) { - throw new RateLimitException("Rate limit reached"); - } - return responseEntity.getBody(); } /**