Skip to content

Commit 7ee4d1e

Browse files
authored
DX-1987: add try/catch to body parser of failureFunction (#116)
* fix: add try/catch to body parser of failureFunction more details available in the ticket on linear * fix: rm log
1 parent 12bbe68 commit 7ee4d1e

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/workflow-parser.test.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,80 @@ describe("Workflow Parser", () => {
793793
},
794794
});
795795

796+
test("should show failResponse with warning when payload doesn't have message field", async () => {
797+
const routeFunction = async (context: WorkflowContext) => {
798+
await context.sleep("sleeping", 1);
799+
};
800+
801+
const incorrectErrorResponseBody = JSON.stringify({
802+
nonMessageField: "any-value",
803+
});
804+
805+
let calledFailureFunction = false;
806+
const failureFunction: WorkflowServeOptions["failureFunction"] = async ({ failResponse }) => {
807+
expect(failResponse).toBe(
808+
`Couldn't parse 'failResponse' in 'failureFunction', received: '${incorrectErrorResponseBody}'`
809+
);
810+
calledFailureFunction = true;
811+
return;
812+
};
813+
814+
const result2 = await handleFailure(
815+
failureRequest,
816+
JSON.stringify({
817+
status: 201,
818+
body: btoa(incorrectErrorResponseBody),
819+
url: WORKFLOW_ENDPOINT,
820+
}),
821+
client,
822+
initialPayloadParser,
823+
routeFunction,
824+
failureFunction,
825+
{},
826+
0,
827+
undefined
828+
);
829+
830+
expect(result2.isOk()).toBeTrue();
831+
expect(calledFailureFunction).toBeTrue();
832+
});
833+
834+
test("should show failResponse with warning when payload can't be parsed", async () => {
835+
const routeFunction = async (context: WorkflowContext) => {
836+
await context.sleep("sleeping", 1);
837+
};
838+
839+
const nonJSONPayload = "helloWorld";
840+
841+
let calledFailureFunction = false;
842+
const failureFunction: WorkflowServeOptions["failureFunction"] = async ({ failResponse }) => {
843+
expect(failResponse).toBe(
844+
`Couldn't parse 'failResponse' in 'failureFunction', received: '${nonJSONPayload}'`
845+
);
846+
calledFailureFunction = true;
847+
return;
848+
};
849+
850+
const result2 = await handleFailure(
851+
failureRequest,
852+
JSON.stringify({
853+
status: 201,
854+
body: btoa(nonJSONPayload),
855+
url: WORKFLOW_ENDPOINT,
856+
}),
857+
client,
858+
initialPayloadParser,
859+
routeFunction,
860+
failureFunction,
861+
{},
862+
0,
863+
undefined
864+
);
865+
866+
expect(result2.isOk()).toBeTrue();
867+
expect(calledFailureFunction).toBeTrue();
868+
});
869+
796870
test("should throw WorkflowError if header is set but function is not passed", async () => {
797871
let called = false;
798872
const routeFunction = async (context: WorkflowContext) => {
@@ -875,7 +949,6 @@ describe("Workflow Parser", () => {
875949
0,
876950
undefined
877951
);
878-
console.log(result);
879952

880953
expect(result.isOk()).toBeTrue();
881954
expect(result.isOk() && result.value).toBe("is-failure-callback");

src/workflow-parser.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,19 @@ export const handleFailure = async <TInitialPayload>(
333333
};
334334

335335
const decodedBody = body ? decodeBase64(body) : "{}";
336-
const errorPayload = JSON.parse(decodedBody) as FailureFunctionPayload;
336+
let errorMessage: string = "";
337+
try {
338+
const errorPayload = JSON.parse(decodedBody) as FailureFunctionPayload;
339+
if (errorPayload.message) {
340+
errorMessage = errorPayload.message;
341+
}
342+
} catch {
343+
// skip
344+
}
345+
346+
if (!errorMessage) {
347+
errorMessage = `Couldn't parse 'failResponse' in 'failureFunction', received: '${decodedBody}'`;
348+
}
337349

338350
// create context
339351
const workflowContext = new WorkflowContext<TInitialPayload>({
@@ -371,7 +383,7 @@ export const handleFailure = async <TInitialPayload>(
371383
await failureFunction({
372384
context: workflowContext,
373385
failStatus: status,
374-
failResponse: errorPayload.message,
386+
failResponse: errorMessage,
375387
failHeaders: header,
376388
});
377389
} catch (error) {

0 commit comments

Comments
 (0)