fix: prevent negative wait time in rate limit error message (#28765)
Co-authored-by: Romit <85230081+romitg2@users.noreply.github.com>
This commit is contained in:
1 parent
cdeaca4dc2
commit
3c52f57231
2 files changed
+18
-1
No files matched your search
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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.`,
|
||||
|
||||
Reference in new issue
Block a user