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 status field to QStashError #216

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions src/client/error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { ChatRateLimit, RateLimit } from "./types";
import type { FailureFunctionPayload, Step } from "./workflow/types";

const RATELIMIT_STATUS = 429;

/**
* Result of 500 Internal Server Error
*/
export class QstashError extends Error {
constructor(message: string) {
public readonly status?: number;

constructor(message: string, status?: number) {
super(message);
this.name = "QstashError";
this.status = status;
}
}

Expand All @@ -17,7 +22,7 @@ export class QstashRatelimitError extends QstashError {
public reset: string | null;

constructor(args: RateLimit) {
super(`Exceeded burst rate limit. ${JSON.stringify(args)} `);
super(`Exceeded burst rate limit. ${JSON.stringify(args)}`, RATELIMIT_STATUS);
this.name = "QstashRatelimitError";
this.limit = args.limit;
this.remaining = args.remaining;
Expand All @@ -34,7 +39,7 @@ export class QstashChatRatelimitError extends QstashError {
public resetTokens: string | null;

constructor(args: ChatRateLimit) {
super(`Exceeded chat rate limit. ${JSON.stringify(args)} `);
super(`Exceeded chat rate limit. ${JSON.stringify(args)}`, RATELIMIT_STATUS);
this.name = "QstashChatRatelimitError";
this.limitRequests = args["limit-requests"];
this.limitTokens = args["limit-tokens"];
Expand All @@ -51,7 +56,7 @@ export class QstashDailyRatelimitError extends QstashError {
public reset: string | null;

constructor(args: RateLimit) {
super(`Exceeded daily rate limit. ${JSON.stringify(args)} `);
super(`Exceeded daily rate limit. ${JSON.stringify(args)}`, RATELIMIT_STATUS);
this.name = "QstashDailyRatelimitError";
this.limit = args.limit;
this.remaining = args.remaining;
Expand Down
5 changes: 4 additions & 1 deletion src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ export class HttpClient implements Requester {

if (response.status < 200 || response.status >= 300) {
const body = await response.text();
throw new QstashError(body.length > 0 ? body : `Error: status=${response.status}`);
throw new QstashError(
body.length > 0 ? body : `Error: status=${response.status}`,
response.status
);
}
}
}
2 changes: 1 addition & 1 deletion src/client/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("workflow tests", () => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const throws = qstashClient.workflow.cancel(workflowRunId);
expect(throws).rejects.toThrow(
new QstashError(`{"error":"workflowRun ${workflowRunId} not found"}`)
new QstashError(`{"error":"workflowRun ${workflowRunId} not found"}`, 404)
);
});
});
2 changes: 2 additions & 0 deletions src/client/workflow/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe("serve", () => {
"content-type": "application/json",
"upstash-forward-upstash-workflow-sdk-version": "1",
"upstash-method": "POST",
"upstash-retries": "3",
"upstash-workflow-runid": workflowRunId,
"upstash-workflow-init": "false",
"upstash-workflow-url": WORKFLOW_ENDPOINT,
Expand All @@ -147,6 +148,7 @@ describe("serve", () => {
"content-type": "application/json",
"upstash-forward-upstash-workflow-sdk-version": "1",
"upstash-method": "POST",
"upstash-retries": "3",
"upstash-workflow-runid": workflowRunId,
"upstash-workflow-init": "false",
"upstash-workflow-url": WORKFLOW_ENDPOINT,
Expand Down
Loading