User Guide
Features
On Failure Step

On Failure Step

The On Failure Step is a special step in Hatchet workflows that allows you to define a function to be executed in the event that any step in the main workflow fails. This feature enables you to handle errors, perform cleanup tasks, or trigger notifications in case of workflow failures.

Defining an On Failure Step

You can define an On Failure Step on your Workflow using the Hatchet client libraries:

@hatchet.workflow(on_events=["user:create"])
class OnFailureWorkflow:
    @hatchet.step()
    def step1(self, context: Context):
        raise Exception("step1 failed")
 
    @hatchet.on_failure_step()
    def on_failure(self, context):
        print("executed on_failure")
        print(context)
 

In the above examples, the on_failure step is defined separately from the main workflow steps. It will be executed only if any of the main workflow steps fail.

Use Cases

Some common use cases for the On Failure Step include:

  • Performing cleanup tasks after a workflow failure
  • Sending notifications or alerts about the failure
  • Logging additional information for debugging purposes
  • Triggering a compensating action or a fallback workflow

By utilizing the On Failure Step, you can handle workflow failures gracefully and ensure that necessary actions are taken in case of errors.

Example: Sending a Slack Notification on Failure

@hatchet.on_failure_step()
def on_failure(self, context):
    slack_client.send_message(
        f"Workflow {context.workflow_run_id()} failed"
    )

In this example, the On Failure Step sends a Slack notification with details about the failed workflow, including the workflow run ID which can be used to find the run in the Hatchet Dashboard.

By leveraging the On Failure Step, you can enhance the reliability and observability of your workflows, ensuring that failures are handled appropriately and relevant stakeholders are notified.