Skip to content

Commit 6ebc057

Browse files
authored
chore(sdk): Removes unnecessary throw statements (#412)
*Issue #, if available:* #411 *Description of changes:* Removes unnecessary exception handling patterns in `with-durable-execution.ts`: ## 1. Simplified `validateDurableExecutionEvent` The previous implementation threw an error inside a try block and immediately caught all exceptions to replace with a different message: ```typescript function validateDurableExecutionEvent(event: unknown): void { try { const eventObj = event as Record<string, unknown>; if (!eventObj?.DurableExecutionArn || !eventObj?.CheckpointToken) { throw new Error("Missing required durable execution fields"); } } catch { const msg = `Unexpected payload provided to start the durable execution...`; throw new Error(msg); } } ``` This was problematic because: - The specific validation error message was never visible to callers - Type assertions don't throw at runtime and optional chaining prevents most errors - The `try/catch` was effectively redundant Now simplified to directly throw the user-facing error without the wrapper. ## 2. Removed redundant catch block in `withDurableExecution` The function contained a catch block that simply re-threw the error: ```ts try { response = await runHandler(...); return response; } catch (err) { throw err; } ``` This serves no purpose: errors propagate naturally without it. Replaced with a direct return of `runHandler()`. --- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 3c31bc3 commit 6ebc057

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,12 @@ async function runHandler<
268268
* Validates that the event is a proper durable execution input
269269
*/
270270
function validateDurableExecutionEvent(event: unknown): void {
271-
try {
272-
const eventObj = event as Record<string, unknown>;
273-
if (!eventObj?.DurableExecutionArn || !eventObj?.CheckpointToken) {
274-
throw new Error("Missing required durable execution fields");
275-
}
276-
} catch {
277-
const msg = `Unexpected payload provided to start the durable execution.
278-
Check your resource configurations to confirm the durability is set.`;
279-
throw new Error(msg);
271+
const eventObj = event as Record<string, unknown>;
272+
if (!eventObj?.DurableExecutionArn || !eventObj?.CheckpointToken) {
273+
throw new Error(
274+
"Unexpected payload provided to start the durable execution.\n" +
275+
"Check your resource configurations to confirm the durability is set.",
276+
);
280277
}
281278
}
282279

@@ -368,19 +365,13 @@ export const withDurableExecution = <
368365
validateDurableExecutionEvent(event);
369366
const { executionContext, durableExecutionMode, checkpointToken } =
370367
await initializeExecutionContext(event, context, config?.client);
371-
let response: DurableExecutionInvocationOutput | null = null;
372-
try {
373-
response = await runHandler(
374-
event,
375-
context,
376-
executionContext,
377-
durableExecutionMode,
378-
checkpointToken,
379-
handler,
380-
);
381-
return response;
382-
} catch (err) {
383-
throw err;
384-
}
368+
return runHandler(
369+
event,
370+
context,
371+
executionContext,
372+
durableExecutionMode,
373+
checkpointToken,
374+
handler,
375+
);
385376
};
386377
};

0 commit comments

Comments
 (0)