Skip to content

Commit 3c52f57

Browse files
Akash504-airomitg2
andauthored
fix: prevent negative wait time in rate limit error message (calcom#28765)
Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com>
1 parent cdeaca4 commit 3c52f57

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/lib/checkRateLimitAndThrowError.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,21 @@ describe("checkRateLimitAndThrowError", () => {
6767

6868
await expect(checkRateLimitAndThrowError({ rateLimitingType, identifier })).resolves.not.toThrow();
6969
});
70+
it("should not return negative wait time when reset is in the past", async () => {
71+
vi.mocked(rateLimiter).mockReturnValue(() => {
72+
return {
73+
limit: 10,
74+
remaining: -1,
75+
reset: Date.now() - 5000, // past time
76+
success: false,
77+
} as RatelimitResponse;
78+
});
79+
80+
const identifier = "test-identifier";
81+
const rateLimitingType = "core";
82+
83+
await expect(
84+
checkRateLimitAndThrowError({ rateLimitingType, identifier })
85+
).rejects.toThrow("0 seconds");
86+
});
7087
});

packages/lib/checkRateLimitAndThrowError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function checkRateLimitAndThrowError({
1313
const { success, reset } = response;
1414
if (!success) {
1515
const convertToSeconds = (ms: number) => Math.floor(ms / 1000);
16-
const secondsToWait = convertToSeconds(reset - Date.now());
16+
const secondsToWait = Math.max(0, convertToSeconds(reset - Date.now()));
1717
throw new HttpError({
1818
statusCode: 429,
1919
message: `Rate limit exceeded. Try again in ${secondsToWait} seconds.`,

0 commit comments

Comments
 (0)