Hi - I am trying to piece together a state handler...
# prefect-community
i
Hi - I am trying to piece together a state handler that handles a
task
failing and returns a success and sets the final result to a
None
type or
df
. Can you point me to the specific pattern to copy? Thamks
j
Hey @itay livni I believe you’re going to want to do something like this:
Copy code
def handle_disambig_error(task, old_state, new_state):
    if new_state.is_failed():
        # do your state logic here
    return new_state
The signature takes in the old state and the new state. e.g. signature:
Copy code
def state_handler(obj: Union[Task, Flow], old_state: State, new_state: State) -> Optional[State]:
    """
    Any function with this signature can serve as a state handler.

    Args:
        - obj (Union[Task, Flow]): the underlying object to which this state handler
            is attached
        - old_state (State): the previous state of this object
        - new_state (State): the proposed new state of this object

    Returns:
        - Optional[State]: the new state of this object (typically this is just `new_state`)
    """
    pass
i
Thanks
https://stackoverflow.com/questions/59954398/how-to-handle-a-task-failure-in-prefect-and-return-success-with-the-on-failure-p @josh @Chris White I dont have enough points for a prefect tag on stack. Can someone add it please. It would be useful (IMO)