Skip to content

Commit f2f28cb

Browse files
committed
feat: add status to QStashError
1 parent 806e187 commit f2f28cb

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/client/error.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import type { ChatRateLimit, RateLimit } from "./types";
22
import type { FailureFunctionPayload, Step } from "./workflow/types";
33

4+
const RATELIMIT_STATUS = 429;
5+
46
/**
57
* Result of 500 Internal Server Error
68
*/
79
export class QstashError extends Error {
8-
constructor(message: string) {
10+
public readonly status?: number;
11+
12+
constructor(message: string, status?: number) {
913
super(message);
1014
this.name = "QstashError";
15+
this.status = status;
1116
}
1217
}
1318

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

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

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

5358
constructor(args: RateLimit) {
54-
super(`Exceeded daily rate limit. ${JSON.stringify(args)} `);
59+
super(`Exceeded daily rate limit. ${JSON.stringify(args)}`, RATELIMIT_STATUS);
5560
this.name = "QstashDailyRatelimitError";
5661
this.limit = args.limit;
5762
this.remaining = args.remaining;

src/client/http.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ export class HttpClient implements Requester {
248248

249249
if (response.status < 200 || response.status >= 300) {
250250
const body = await response.text();
251-
throw new QstashError(body.length > 0 ? body : `Error: status=${response.status}`);
251+
throw new QstashError(
252+
body.length > 0 ? body : `Error: status=${response.status}`,
253+
response.status
254+
);
252255
}
253256
}
254257
}

src/client/workflow.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("workflow tests", () => {
3131
// eslint-disable-next-line @typescript-eslint/no-deprecated
3232
const throws = qstashClient.workflow.cancel(workflowRunId);
3333
expect(throws).rejects.toThrow(
34-
new QstashError(`{"error":"workflowRun ${workflowRunId} not found"}`)
34+
new QstashError(`{"error":"workflowRun ${workflowRunId} not found"}`, 404)
3535
);
3636
});
3737
});

0 commit comments

Comments
 (0)