The slack alerts sent by state handlers `handler =...
# ask-community
m
The slack alerts sent by state handlers
handler = slack_notifier(only_states=[Failed])
do not contain details of the flow name and the taskname, how can we add that?
a
The slack notifier is a super simplistic callback function. To get details such as task name etc, you could build a task level state handler. You can use the following flow as template:
Copy code
import prefect
from prefect import task, Flow
from prefect.tasks.notifications import SlackTask
from typing import cast


def post_to_slack_on_failure(task, old_state, new_state):
    if new_state.is_failed():
        if isinstance(new_state.result, Exception):
            value = "```{}```".format(repr(new_state.result))
        else:
            value = cast(str, new_state.message)
        msg = (
            f"The task `{prefect.context.task_name}` failed "
            f"in a flow run {prefect.context.flow_run_id} "
            f"with an exception {value}"
        )
        SlackTask(message=msg).run()
    return new_state


@task(state_handlers=[post_to_slack_on_failure])
def divide_numbers(a, b):
    return 1 / (b - a)


with Flow(name="state-inspection-handler") as flow:
    result = divide_numbers(1, 1)


if __name__ == "__main__":
    flow.run()
🙌 1
m
Thanks
👍 1