Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-java): keep the API error when Retry-After is out of range

Yudhi Armyndharis18e86147f10a
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/errors/OpenWAApiError.java b/sdk/java/src/main/java/com/rmyndharis/openwa/errors/OpenWAApiError.java
index 77b897192..2e0cc478b 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/errors/OpenWAApiError.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/errors/OpenWAApiError.java
@@ -113,7 +113,7 @@ public class OpenWAApiError extends OpenWAError {
try {
Instant at = ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant();
return Math.max(0L, (long) Math.ceil((at.toEpochMilli() - System.currentTimeMillis()) / 1000.0));
- } catch (DateTimeParseException e) {
+ } catch (DateTimeParseException | ArithmeticException e) {
return null;
}
}
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java
index f96e091ce..aa2289c82 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java
@@ -96,6 +96,9 @@ class ClientTest {
tx.respondRaw(503, new byte[0], Map.of("Retry-After", List.of("soon")));
assertNull(assertThrows(OpenWAApiError.class,
() -> client.requestVoid(HttpMethod.GET, "/api/sessions", null, null)).retryAfterSeconds());
+ tx.respondRaw(503, new byte[0], Map.of("Retry-After", List.of("Sun, 6 Nov 1994 08:49:37 GMT")));
+ assertEquals(0L, assertThrows(OpenWAApiError.class,
+ () -> client.requestVoid(HttpMethod.GET, "/api/sessions", null, null)).retryAfterSeconds());
tx.respond(502, "{\"statusCode\":502,\"message\":\"x\",\"code\":\"SESSION_LOGOUT_INCOMPLETE\"}");
assertEquals("SESSION_LOGOUT_INCOMPLETE", assertThrows(OpenWAApiError.class,
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/errors/ErrorsTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/errors/ErrorsTest.java
index 40368f755..4dba56608 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/errors/ErrorsTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/errors/ErrorsTest.java
@@ -2,8 +2,11 @@ package com.rmyndharis.openwa.errors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.List;
+import java.util.Map;
import org.junit.jupiter.api.Test;
class ErrorsTest {
@@ -80,4 +83,40 @@ class ErrorsTest {
OpenWAApiError e = OpenWAApiError.fromResponse(502, "", "", "GET /api/x");
assertEquals("OpenWA API 502 — GET /api/x", e.getMessage());
}
+
+ @Test
+ void unrepresentableRetryDateKeepsTheOriginalApiError() {
+ Map<String, List<String>> headers = Map.of("Retry-After", List.of("1 Jan 999999999 00:00:00 GMT"));
+ for (int status : new int[] {429, 503}) {
+ String body = "{\"message\":\"retry later\",\"error\":\"Busy\",\"code\":\"WAIT\"}";
+ OpenWAApiError e = OpenWAApiError.fromResponse(status, "", body, "GET /api/x", headers);
+ assertEquals(status == 429 ? OpenWARateLimitError.class : OpenWAServiceUnavailableError.class, e.getClass());
+ assertEquals(status, e.status());
+ assertEquals(body, e.body().toString());
+ assertEquals("WAIT", e.code());
+ assertEquals("Busy", e.errorKind());
+ assertEquals(headers.get("Retry-After"), e.headers().get("retry-after"));
+ assertNull(e.retryAfterSeconds());
+ }
+ }
+
+ @Test
+ void unrepresentableRetryDateKeepsTheRedirectError() {
+ OpenWAApiError e = OpenWAApiError.fromResponse(302, "Found", "", "GET /api/x",
+ Map.of("Retry-After", List.of("1 Jan 999999999 00:00:00 GMT")));
+ assertEquals(OpenWAApiError.class, e.getClass());
+ assertEquals(302, e.status());
+ assertTrue(e.getMessage().toLowerCase().contains("redirect"));
+ assertNull(e.retryAfterSeconds());
+ }
+
+ @Test
+ void bodyRetryWaitTakesPrecedenceOverUnrepresentableDate() {
+ OpenWAApiError e = OpenWAApiError.fromResponse(429, "",
+ "{\"message\":\"retry later\",\"code\":\"WAIT\",\"retryAfterSeconds\":1.2}", "GET /api/x",
+ Map.of("Retry-After", List.of("1 Jan 999999999 00:00:00 GMT")));
+ assertTrue(e instanceof OpenWARateLimitError);
+ assertEquals("WAIT", e.code());
+ assertEquals(2L, e.retryAfterSeconds());
+ }
}