Skip to content

Commit

Permalink
Add status field to QStashError (#216)
Browse files Browse the repository at this point in the history
* feat: add status to QStashError

* fix: tests
  • Loading branch information
CahidArda authored Nov 20, 2024
1 parent 806e187 commit 97f77eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
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

0 comments on commit 97f77eb

Please sign in to comment.