Does anyone know how to display information about ...
# ask-community
v
Does anyone know how to display information about another broken task or the error that caused the fail in a task? I have very simple etl flow with one alert task. This alert task is executed if any of the etl tasks fail. I want to use this alert task to send a further telegram notification and I want to include meaningful information in this notification than just the "Error" message. Maybe I should use a completely different approach? I tried to get information through context, but this information is not what I need at the moment.
Copy code
from prefect import task, Flow
from prefect.triggers import any_failed
from random import randint

@task()
def extract():
    # this task generates random integers from 0,1,2,3,4,5.
    random_number = randint(0, 5)
    return random_number


@task()
def transform(random_number):
    # this task is potentially dangerous, since the random number can be zero, and then there will be division by zero
    data = 100 / random_number
    return data


@task()
def load(data):
    # just print data
    print(data)


@task(trigger=any_failed)
def telegram_alarm():
    # this task is needed to send notifications about fails in telegram.
    print('ERROR')  # How can I display a more informative message? For example, which task broke? Or what error caused the fail? (in my case ZeroDivisionError: division by zero)

    # import prefect
    # print(prefect.context.to_dict())  # tried this method but didn't find the needed logs


with Flow('test_etl') as flow:
    random_number = extract()
    data = transform(random_number)
    load(data)
    telegram_alarm(upstream_tasks=[flow.get_tasks()])

flow_state = flow.run()
k
Hi @Vladislav Bogucharov! Just a friendly reminder if you could post big code blocks in the thread next time. I think a state handler is ideal for your use case where you can program behavior upon failure and attach it to your task. I am linking a specific section but I think you may have to read the whole page for more context.
You can also explore the automations tab in the UI here
v
@Kevin Kho Sorry, next time I will post the code to the thread. And thanks for the advice, I'll check it out now!
k
No worries at all 🙂