Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QstashDailyRatelimitError #124

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/client/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ export class QstashError extends Error {

export class QstashRatelimitError extends QstashError {
constructor(args: RateLimit) {
super(`You have been ratelimited. ${JSON.stringify(args)} `);
super(`Exceeded burst rate limit. ${JSON.stringify(args)} `);
CahidArda marked this conversation as resolved.
Show resolved Hide resolved
}
}

export class QstashChatRatelimitError extends QstashError {
constructor(args: ChatRateLimit) {
super(`You have been ratelimited. ${JSON.stringify(args)} `);
super(`Exceeded chat rate limit. ${JSON.stringify(args)} `);
}
}

export class QstashDailyRatelimitError extends QstashError {
constructor(args: RateLimit) {
super(`Exceeded daily rate limit. ${JSON.stringify(args)} `);
}
}
13 changes: 12 additions & 1 deletion src/client/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable @typescript-eslint/no-magic-numbers */
import { QstashError, QstashRatelimitError, QstashChatRatelimitError } from "./error";
import {
QstashError,
QstashRatelimitError,
QstashChatRatelimitError,
QstashDailyRatelimitError,
} from "./error";
import type { BodyInit, HeadersInit, RequestOptions } from "./types";
import type { ChatCompletionChunk } from "./llm/types";

Expand Down Expand Up @@ -217,6 +222,12 @@ export class HttpClient implements Requester {
"reset-requests": response.headers.get("x-ratelimit-reset-requests"),
"reset-tokens": response.headers.get("x-ratelimit-reset-tokens"),
});
} else if (response.headers.get("RateLimit-Limit")) {
throw new QstashDailyRatelimitError({
limit: response.headers.get("RateLimit-Limit"),
remaining: response.headers.get("RateLimit-Remaining"),
reset: response.headers.get("RateLimit-Reset"),
});
}

throw new QstashRatelimitError({
Expand Down