Skip to content

Commit c210e21

Browse files
authored
feat(sdk): validate durable execution input payload (#334)
Add validation to ensure Lambda functions wrapped with withDurableExecution receive proper durable execution payloads. Throws clear error message when DurableExecutionArn or CheckpointToken are missing, preventing confusing downstream errors. - Add validateDurableExecutionEvent function in with-durable-execution.ts - Add unit tests for invalid payload scenarios - Use unknown type instead of any for better type safety
1 parent 48b4dbe commit c210e21

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

packages/aws-durable-execution-sdk-js/src/with-durable-execution.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,35 @@ describe("withDurableExecution", () => {
481481
Result: JSON.stringify(mockResult),
482482
});
483483
});
484+
485+
it("should throw error for invalid durable execution event", async () => {
486+
const mockHandler = jest.fn();
487+
const wrappedHandler = withDurableExecution(mockHandler);
488+
489+
// Test missing DurableExecutionArn
490+
const invalidEvent1 = { CheckpointToken: "token" };
491+
await expect(
492+
wrappedHandler(invalidEvent1 as any, mockContext),
493+
).rejects.toThrow(
494+
"Unexpected payload provided to start the durable execution",
495+
);
496+
497+
// Test missing CheckpointToken
498+
const invalidEvent2 = { DurableExecutionArn: "arn" };
499+
await expect(
500+
wrappedHandler(invalidEvent2 as any, mockContext),
501+
).rejects.toThrow(
502+
"Unexpected payload provided to start the durable execution",
503+
);
504+
505+
// Test completely invalid event
506+
const invalidEvent3 = {};
507+
await expect(
508+
wrappedHandler(invalidEvent3 as any, mockContext),
509+
).rejects.toThrow(
510+
"Unexpected payload provided to start the durable execution",
511+
);
512+
513+
expect(mockHandler).not.toHaveBeenCalled();
514+
});
484515
});

packages/aws-durable-execution-sdk-js/src/with-durable-execution.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ async function runHandler<
231231
}
232232
}
233233

234+
/**
235+
* Validates that the event is a proper durable execution input
236+
*/
237+
function validateDurableExecutionEvent(event: unknown): void {
238+
try {
239+
const eventObj = event as Record<string, unknown>;
240+
if (!eventObj?.DurableExecutionArn || !eventObj?.CheckpointToken) {
241+
throw new Error("Missing required durable execution fields");
242+
}
243+
} catch {
244+
const msg = `Unexpected payload provided to start the durable execution.
245+
Check your resource configurations to confirm the durability is set.`;
246+
throw new Error(msg);
247+
}
248+
}
249+
234250
export const withDurableExecution = <
235251
Input,
236252
Output,
@@ -242,6 +258,7 @@ export const withDurableExecution = <
242258
event: DurableExecutionInvocationInput,
243259
context: Context,
244260
): Promise<DurableExecutionInvocationOutput> => {
261+
validateDurableExecutionEvent(event);
245262
const { executionContext, durableExecutionMode, checkpointToken } =
246263
await initializeExecutionContext(event, context);
247264
let response: DurableExecutionInvocationOutput | null = null;

0 commit comments

Comments
 (0)