Add exception transitions
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 195
- Avg merge
- 6d 10h
- Merged PRs (30d)
- 17
Description
Currently, exceptions will break the control flow of an action, stopping the program early. Thus, if an exception is expected, the program will stop early. We will be adding the ability to conditionally transition based on exceptions, which will allow you to transition to an error-handling (or retry) action that does not need the full outputs of the prior action.
Here is what it would look liek in the current API:
```python
@action(reads=["attempts"], writes=["output", "attempts"])
def some_flaky_action(state: State, max_retries: int=3) -> Tuple[dict, State]:
result = {"output": None, "attempts": state["attempts"] + 1}
try:
result["output"] = call_some_api(...)
excecpt APIException as e:
if state["attempts"] >= max_retries:
raise e
return result, state.update(**result)
```
One could imagine adding it as a condition (a few possibilities)
```python
@action(reads=[], writes=["output"])
def some_flaky_action(state: State) -> Tuple[dict, State]:
result = {"output": call_some_api(...)}
return result, state.update(**result)
builder.with_actions(
some_flaky_action=some_flaky_action
).with_transitions(
(
"some_flaky_action",
"some_flaky_action",
error(APIException) # infinite retries
error(APIException, max=3) # 3 visits to this edge then it gets reset if this is not chosen
# That's stored in state
)
```
Contributor guide
Assessment
This issue has not been assessed yet.