Skip to content

Commit 3f50919

Browse files
committed
fix: move activity input JSON.parse inside try-catch for proper error handling
The JSON.parse call for deserializing activity input was positioned outside the try-catch block in ActivityExecutor.execute(). When encodedInput contained malformed JSON, the SyntaxError bypassed the activityFailed log (EventId 605) and propagated with no activity context (name, orchestrationId). Move JSON.parse inside the existing try-catch so all activity execution failures — including input deserialization errors — are logged consistently and re-thrown with proper context. Add unit tests for malformed JSON input, undefined input, and empty string input edge cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 29455dd commit 3f50919

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

packages/durabletask-js/src/worker/activity-executor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ export class ActivityExecutor {
3232
// Log activity start (EventId 603)
3333
WorkerLogs.activityStarted(this._logger, orchestrationId, name);
3434

35-
const activityInput = encodedInput ? JSON.parse(encodedInput) : undefined;
3635
const ctx = new ActivityContext(orchestrationId, taskId);
3736

3837
try {
38+
// Deserialize the input inside the try-catch so that malformed JSON
39+
// is reported through the same activityFailed log path (EventId 605)
40+
// as any other activity execution error.
41+
const activityInput = encodedInput ? JSON.parse(encodedInput) : undefined;
42+
3943
// Execute the activity function
4044
let activityOutput = fn(ctx, activityInput);
4145

packages/durabletask-js/test/activity_executor.spec.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,62 @@ describe("Activity Executor", () => {
5353
expect(caughtException).not.toBeNull();
5454
expect(caughtException?.message).toMatch(/Bogus/);
5555
});
56+
57+
it("should throw and log activityFailed when input is malformed JSON", async () => {
58+
const testActivity = (_: ActivityContext, input: any) => {
59+
return input;
60+
};
61+
62+
const loggerSpy = createSpyLogger();
63+
const [executor, name] = getActivityExecutor(testActivity, loggerSpy);
64+
65+
const malformedJson = "{not valid json";
66+
67+
await expect(executor.execute(TEST_INSTANCE_ID, name, TEST_TASK_ID, malformedJson))
68+
.rejects.toThrow(SyntaxError);
69+
70+
// Verify the activityFailed log (EventId 605) was emitted
71+
expect(loggerSpy.error).toHaveBeenCalled();
72+
const errorCall = loggerSpy.error.mock.calls[0][0];
73+
expect(errorCall).toContain(name);
74+
expect(errorCall).toContain(TEST_INSTANCE_ID);
75+
expect(errorCall).toContain("failed");
76+
});
77+
78+
it("should handle undefined input without error", async () => {
79+
const testActivity = (_: ActivityContext, input: any) => {
80+
return input;
81+
};
82+
83+
const [executor, name] = getActivityExecutor(testActivity);
84+
const result = await executor.execute(TEST_INSTANCE_ID, name, TEST_TASK_ID, undefined);
85+
expect(result).toBeUndefined();
86+
});
87+
88+
it("should handle empty string input without error", async () => {
89+
const testActivity = (_: ActivityContext, input: any) => {
90+
return input;
91+
};
92+
93+
const [executor, name] = getActivityExecutor(testActivity);
94+
const result = await executor.execute(TEST_INSTANCE_ID, name, TEST_TASK_ID, "");
95+
expect(result).toBeUndefined();
96+
});
5697
});
5798

5899
// Activity = Callable[[ActivityContext, TInput], TOutput]
59-
function getActivityExecutor(fn: TActivity<any, any>): [ActivityExecutor, string] {
100+
function getActivityExecutor(fn: TActivity<any, any>, logger?: any): [ActivityExecutor, string] {
60101
const registry = new Registry();
61102
const name = registry.addActivity(fn);
62-
const executor = new ActivityExecutor(registry, testLogger);
103+
const executor = new ActivityExecutor(registry, logger ?? testLogger);
63104
return [executor, name];
64105
}
106+
107+
function createSpyLogger() {
108+
return {
109+
error: jest.fn(),
110+
warn: jest.fn(),
111+
info: jest.fn(),
112+
debug: jest.fn(),
113+
};
114+
}

0 commit comments

Comments
 (0)