up:: [[AWS MOC]]
# Catching and Responding To Failures in AWS Step Functions
To handle errors within a Step Function, you can use a Catch block that hands off execution to an Error State. This State can be a Task type, which can (among other things) trigger a Lambda that does the actual error handling.
## Example
```python
# Step function failure handler - in the future this will be more complex than writing to DB
failure_handler = lambda_python.PythonFunction(
self,
f"failure-handler",
entry="lambda/",
index="failure_handler.py",
runtime=_lambda.Runtime.PYTHON_3_9,
)
failure_task = sfn_tasks.LambdaInvoke(
self,
f"failure-lambda-task",
lambda_function=training_pipeline_failure_handler,
output_path="
quot;,
)
# State machine definition
task_one.add_catch(failure_task, result_path="$.error")
task_two.add_catch(failure_task, result_path="$.error")
state_machine_def = task_one.next(task_two)
state_machine = sfn.StateMachine(
self, f"failing-state-machine", definition=state_machine_def
)
```
## Sources
[[Trigger Audience Training Pipeline from SAM#^qpf9xv]]
[AWS Step Function Documentation - handling error conditions](https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-handling-error-conditions.html#using-state-machine-error-conditions-step-4)