Skip to content

Commit

Permalink
fix: update failureFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
CahidArda committed Nov 18, 2024
1 parent a35a648 commit e9fc62e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/ci/app/test-routes/failureFunction-retry/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const { POST, GET } = testServe(
}, {
baseUrl: BASE_URL,
retries: 1,
failureFunction: async (context, failStatus, failResponse) => {
failureFunction: async ({ context, failStatus, failResponse }) => {
expect(failStatus, 500);
expect(failResponse, errorMessage);
expect(context.headers.get("authentication")!, authHeaderValue);
Expand Down
2 changes: 1 addition & 1 deletion examples/ci/app/test-routes/failureFunction/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const { POST, GET } = testServe(
}, {
baseUrl: BASE_URL,
retries: 0,
failureFunction: async (context, failStatus, failResponse) => {
failureFunction: async ({ context, failStatus, failResponse }) => {
expect(failStatus, 500);
expect(failResponse, errorMessage);
expect(context.headers.get("authentication")!, authHeaderValue);
Expand Down
4 changes: 2 additions & 2 deletions src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ describe.skip("live serve tests", () => {
});
},
retries: 0,
failureFunction: (context, failStatus, failResponse, failHeaders) => {
failureFunction: ({ context, failStatus, failResponse, failHeaders }) => {
expect(failStatus).toBe(500);
expect(failResponse).toBe("my-custom-error");
expect(context.headers.get("authentication")).toBe("Bearer secretPassword");
Expand Down Expand Up @@ -557,7 +557,7 @@ describe.skip("live serve tests", () => {
});
},
retries: 1,
failureFunction: (context, failStatus, failResponse, failHeaders) => {
failureFunction: ({ context, failStatus, failResponse, failHeaders }) => {
expect(failStatus).toBe(500);
expect(failResponse).toBe("my-custom-error");
expect(context.headers.get("authentication")).toBe("Bearer secretPassword");
Expand Down
6 changes: 1 addition & 5 deletions src/serve/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,7 @@ describe("serve", () => {
test("should set failureUrl as context url if failureFunction is passed", async () => {
const request = getRequest(WORKFLOW_ENDPOINT, "wfr-bar", "my-payload", []);
let called = false;
const myFailureFunction: WorkflowServeOptions["failureFunction"] = async (
_status,
_header,
_body
) => {
const myFailureFunction: WorkflowServeOptions["failureFunction"] = async () => {
return;
};
const { handler: endpoint } = serve(routeFunction, {
Expand Down
17 changes: 9 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ export type WorkflowServeOptions<
* @param failResponse error message
* @returns void
*/
failureFunction?: (
failureData: {
context: Omit<WorkflowContext, "run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify">
failStatus: number;
failResponse: string;
failHeader: Record<string, string[]>
}
) => Promise<void> | void;
failureFunction?: (failureData: {
context: Omit<
WorkflowContext,
"run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify"
>;
failStatus: number;
failResponse: string;
failHeaders: Record<string, string[]>;
}) => Promise<void> | void;
/**
* Base Url of the workflow endpoint
*
Expand Down
34 changes: 18 additions & 16 deletions src/workflow-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe("Workflow Parser", () => {
expect(throws).toThrow(
new QStashWorkflowError(
`Incompatible workflow sdk protocol version.` +
` Expected ${WORKFLOW_PROTOCOL_VERSION}, got ${requestProtocol} from the request.`
` Expected ${WORKFLOW_PROTOCOL_VERSION}, got ${requestProtocol} from the request.`
)
);
});
Expand Down Expand Up @@ -639,11 +639,11 @@ describe("Workflow Parser", () => {
};
test("should return not-failure-callback when the header is not set", async () => {
const request = new Request(WORKFLOW_ENDPOINT);
const failureFunction: WorkflowServeOptions["failureFunction"] = async (
{ context,
failStatus,
failResponse }
) => {
const failureFunction: WorkflowServeOptions["failureFunction"] = async ({
context,
failStatus,
failResponse,
}) => {
return;
};

Expand Down Expand Up @@ -676,8 +676,8 @@ describe("Workflow Parser", () => {
expect(result.isErr() && result.error.name).toBe(QStashWorkflowError.name);
expect(result.isErr() && result.error.message).toBe(
"Workflow endpoint is called to handle a failure," +
" but a failureFunction is not provided in serve options." +
" Either provide a failureUrl or a failureFunction."
" but a failureFunction is not provided in serve options." +
" Either provide a failureUrl or a failureFunction."
);
});

Expand All @@ -688,9 +688,11 @@ describe("Workflow Parser", () => {
[WORKFLOW_FAILURE_HEADER]: "true",
},
});
const failureFunction: WorkflowServeOptions["failureFunction"] = async (
{ failHeader, failResponse, failStatus }
) => {
const failureFunction: WorkflowServeOptions["failureFunction"] = async ({
failHeaders,
failResponse,
failStatus,
}) => {
throw new Error("my-error");
};

Expand All @@ -712,11 +714,11 @@ describe("Workflow Parser", () => {
[WORKFLOW_FAILURE_HEADER]: "true",
},
});
const failureFunction: WorkflowServeOptions["failureFunction"] = async (
{ context,
failStatus,
failResponse }
) => {
const failureFunction: WorkflowServeOptions["failureFunction"] = async ({
context,
failStatus,
failResponse,
}) => {
expect(failStatus).toBe(201);
expect(failResponse).toBe(failMessage);
expect(context.headers.get("authorization")).toBe(authorization);
Expand Down
13 changes: 9 additions & 4 deletions src/workflow-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const validateRequest = (
if (!isFirstInvocation && versionHeader !== WORKFLOW_PROTOCOL_VERSION) {
throw new QStashWorkflowError(
`Incompatible workflow sdk protocol version. Expected ${WORKFLOW_PROTOCOL_VERSION},` +
` got ${versionHeader} from the request.`
` got ${versionHeader} from the request.`
);
}

Expand Down Expand Up @@ -278,8 +278,8 @@ export const handleFailure = async <TInitialPayload>(
return err(
new QStashWorkflowError(
"Workflow endpoint is called to handle a failure," +
" but a failureFunction is not provided in serve options." +
" Either provide a failureUrl or a failureFunction."
" but a failureFunction is not provided in serve options." +
" Either provide a failureUrl or a failureFunction."
)
);
}
Expand Down Expand Up @@ -321,7 +321,12 @@ export const handleFailure = async <TInitialPayload>(
debug,
});

await failureFunction({ context: workflowContext, failStatus: status, failResponse: errorPayload.message, failHeader: header });
await failureFunction({
context: workflowContext,
failStatus: status,
failResponse: errorPayload.message,
failHeaders: header,
});
} catch (error) {
return err(error as Error);
}
Expand Down

0 comments on commit e9fc62e

Please sign in to comment.