From 3c52f5723105a9c65bb24c4fd3ef12f1d85a2485 Mon Sep 17 00:00:00 2001 From: Akash Santra Date: Tue, 7 Apr 2026 23:57:44 +0530 Subject: [PATCH] fix: prevent negative wait time in rate limit error message (#28765) Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com> --- .../lib/checkRateLimitAndThrowError.test.ts | 17 +++++++++++++++++ packages/lib/checkRateLimitAndThrowError.ts | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/lib/checkRateLimitAndThrowError.test.ts b/packages/lib/checkRateLimitAndThrowError.test.ts index 0c20560b58..993335853a 100644 --- a/packages/lib/checkRateLimitAndThrowError.test.ts +++ b/packages/lib/checkRateLimitAndThrowError.test.ts @@ -67,4 +67,21 @@ describe("checkRateLimitAndThrowError", () => { await expect(checkRateLimitAndThrowError({ rateLimitingType, identifier })).resolves.not.toThrow(); }); + it("should not return negative wait time when reset is in the past", async () => { + vi.mocked(rateLimiter).mockReturnValue(() => { + return { + limit: 10, + remaining: -1, + reset: Date.now() - 5000, // past time + success: false, + } as RatelimitResponse; + }); + + const identifier = "test-identifier"; + const rateLimitingType = "core"; + + await expect( + checkRateLimitAndThrowError({ rateLimitingType, identifier }) + ).rejects.toThrow("0 seconds"); + }); }); diff --git a/packages/lib/checkRateLimitAndThrowError.ts b/packages/lib/checkRateLimitAndThrowError.ts index 35ada22389..73f01132be 100644 --- a/packages/lib/checkRateLimitAndThrowError.ts +++ b/packages/lib/checkRateLimitAndThrowError.ts @@ -13,7 +13,7 @@ export async function checkRateLimitAndThrowError({ const { success, reset } = response; if (!success) { const convertToSeconds = (ms: number) => Math.floor(ms / 1000); - const secondsToWait = convertToSeconds(reset - Date.now()); + const secondsToWait = Math.max(0, convertToSeconds(reset - Date.now())); throw new HttpError({ statusCode: 429, message: `Rate limit exceeded. Try again in ${secondsToWait} seconds.`,