I am using a map for a set of elements (let’s call...
# prefect-community
a
I am using a map for a set of elements (let’s call them COUNTRY_ID), then, for specific countries, the task failed (and it should failed), however, I would like to catch that error some how and deal with it properly using Prefect. I have read all the doc regarding Handlers, Results and stuff like this. However, I am still struggling to understand which is the best option to, let’s say in my own words, do a try/except in Prefect.
n
@Alberto de Santos you had a chance to look at triggers and/or signals? I think one of those could be really useful for catching the errors you're looking for and doing something when they occur.
a
Yes, @nicholas, I had a look to them, but not sure if that’s what I am looking for.
My question precisely is the following: Given a
map
done in Prefect, is triggers/signals something I can use so that, regardless they fail or not, I can handle them properly, so that the task ends SUCCESSFULY?
In other words, how to deal with FAILED in a
map
n
@Alberto de Santos - would a second map with on the return values from the first work? Maybe something like this:
Copy code
import prefect
from prefect import Flow, task


@task
def return_list():
    return [1, 2, 3, 4]


@task
def parse_value(val):
    if val % 2 != 0:
        raise ValueError("Value is not even!")

    return val


@task(trigger=prefect.triggers.any_failed)
def catch_error(val):
    print(f"Do something with this value error: {val}")


with Flow("Raise error on Odd") as flow:
    my_list = return_list()

    def_list = parse_value.map(my_list)

    catch_error.map(def_list)

flow.run()
Which should throw 2 errors and
catch_error
will run twice with the value of the error