-
Notifications
You must be signed in to change notification settings - Fork 19
feat(errors): typed per-operation error hierarchy #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...es/aws-durable-execution-sdk-python-examples/src/error_handling/catch_typed_step_error.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Demonstrates catching a typed StepError raised by a failed step. | ||
|
|
||
| A failure inside a durable operation surfaces as a typed | ||
| DurableOperationError subclass (StepError, InvokeError, ChildContextError, | ||
| WaitForConditionError) rather than a single catch-all error, so callers can | ||
| handle each operation's failure distinctly. | ||
|
|
||
| The handler also crosses a wait/replay boundary after catching the error. This | ||
| shows the typed StepError is deterministic across replay: on the resume | ||
| invocation the handler replays from the top, the failed step is reconstructed | ||
| from its checkpoint (its body is NOT re-executed), and it surfaces the identical | ||
| StepError, which the same ``except`` catches again. | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python import StepError | ||
| from aws_durable_execution_sdk_python.config import Duration, StepConfig | ||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
| from aws_durable_execution_sdk_python.retries import ( | ||
| RetryStrategyConfig, | ||
| create_retry_strategy, | ||
| ) | ||
| from aws_durable_execution_sdk_python.types import StepContext | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> dict[str, Any]: | ||
| """Run a step that fails, catch the typed StepError, then replay across a wait.""" | ||
|
|
||
| def failing_step(_step_context: StepContext) -> None: | ||
| raise ValueError("payment declined") | ||
|
|
||
| # A single attempt so the step fails without scheduling retries. | ||
| config = StepConfig( | ||
| retry_strategy=create_retry_strategy(RetryStrategyConfig(max_attempts=1)) | ||
| ) | ||
|
|
||
| outcome: dict[str, Any] = {"handled": False} | ||
| try: | ||
| context.step(failing_step, name="charge-card", config=config) | ||
| except StepError as error: | ||
| # The class identifies the operation kind (StepError); error_type carries | ||
| # the original failure type (ValueError). | ||
| outcome = { | ||
| "handled": True, | ||
| "caught": type(error).__name__, | ||
| "error_type": error.error_type, | ||
| "message": error.message, | ||
| } | ||
|
|
||
| # Replay boundary: the execution suspends here and resumes as a new | ||
| # invocation that replays from the top. On resume, the failed step above is | ||
| # reconstructed from its checkpoint and raises the same StepError, which the | ||
| # except catches again — so `outcome` is identical on first run and replay. | ||
| context.wait(duration=Duration.from_seconds(1), name="post-failure-cooldown") | ||
|
|
||
| return outcome |
62 changes: 62 additions & 0 deletions
62
...-durable-execution-sdk-python-examples/test/error_handling/test_catch_typed_step_error.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Tests for catch_typed_step_error.""" | ||
|
|
||
| import pytest | ||
| from aws_durable_execution_sdk_python.execution import InvocationStatus | ||
| from aws_durable_execution_sdk_python.lambda_service import ( | ||
| OperationStatus, | ||
| OperationType, | ||
| ) | ||
|
|
||
| from src.error_handling import catch_typed_step_error | ||
| from test.conftest import deserialize_operation_payload | ||
|
|
||
|
|
||
| @pytest.mark.example | ||
| @pytest.mark.durable_execution( | ||
| handler=catch_typed_step_error.handler, | ||
| lambda_function_name="Catch Typed Step Error", | ||
| ) | ||
| def test_catch_typed_step_error(durable_runner): | ||
| """A failed step surfaces as a catchable StepError, identical across replay. | ||
|
|
||
| The handler catches the StepError and then crosses a wait/replay boundary, so | ||
| the returned ``outcome`` is produced on the resume (replay) invocation, while | ||
| the STEP FAILED checkpoint was written on the first invocation. Asserting the | ||
| surfaced error matches the persisted checkpoint error shows the typed error is | ||
| reconstructed identically across replays. | ||
| """ | ||
| with durable_runner: | ||
| result = durable_runner.run(input=None, timeout=10) | ||
|
|
||
| assert result.status is InvocationStatus.SUCCEEDED | ||
|
|
||
| # The error caught on the replay pass (returned as the execution result). | ||
| result_data = deserialize_operation_payload(result.result) | ||
| assert result_data == { | ||
| "handled": True, | ||
| "caught": "StepError", | ||
| "error_type": "ValueError", | ||
| "message": "payment declined", | ||
| } | ||
|
|
||
| # A WAIT operation proves the execution suspended and replayed after the | ||
| # error was caught, so the result above reflects the replay invocation. | ||
| wait_ops = [ | ||
| op for op in result.operations if op.operation_type == OperationType.WAIT | ||
| ] | ||
| assert len(wait_ops) >= 1 | ||
|
|
||
| # The step's own checkpoint (written on the first run) is FAILED and records | ||
| # the raw escaping error type. | ||
| step_ops = [ | ||
| op for op in result.operations if op.operation_type == OperationType.STEP | ||
| ] | ||
| assert len(step_ops) == 1 | ||
| checkpoint_error = step_ops[0].error | ||
| assert step_ops[0].status is OperationStatus.FAILED | ||
| assert checkpoint_error is not None | ||
|
|
||
| # Cross-replay identity: the error surfaced on the replay pass (result_data) | ||
| # matches the error persisted on the first run (the checkpoint). | ||
| assert result_data["error_type"] == checkpoint_error.type == "ValueError" | ||
| assert result_data["message"] == checkpoint_error.message == "payment declined" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.