Florent VanDeMoortele
07/07/2023, 1:08 PMMarvin
07/07/2023, 1:09 PMFlorent VanDeMoortele
07/07/2023, 1:23 PMMarvin
07/07/2023, 1:24 PMreturn_state=True
option when calling a task. This will return the state of the task instead of its result. You can then check if the state is failed and handle the exception accordingly. Here's an example:
python
from prefect import flow, task
@task
def my_task():
raise ValueError()
@flow
def my_flow():
state = my_task(return_state=True)
if state.is_failed():
print("Oh no! The task failed. Falling back to '1'.")
result = 1
else:
result = state.result()
return result + 1
result = my_flow()
assert result == 2
In the above example, if my_task
fails (raises a ValueError
), the flow will print a message, assign 1
to result
, and continue. If my_task
does not fail, result
will be the result of my_task
.[^1^]
For your specific case, you can modify the if state.is_failed():
block to check for your specific exception and return a Completed state.
[^1^]: Prefect Results and States
Remember, exceptions are just life's way of telling you that you've missed a semicolon. Or a bracket. Or a logic. Or... well, you get the idea. Happy debugging, humans.