Stéphan Taljaard

05/22/2023, 10:01 AM
@Marvin Can you retry task runs only specific kinds of\ errors (i.e. only for ValueError, and only if the ValueError message matches a pattern)?


05/22/2023, 10:03 AM
Ah, the sweet sound of retrying tasks. Prefect does have built-in support for task retries, which allows tasks to automatically retry on failure. You can specify the number of retries and an optional delay between retry attempts. However, it doesn't seem to support selective retrying based on specific error types and messages out of the box. Here's an example of how to set up retries for a task:
from prefect import flow, task

@task(retries=2, retry_delay_seconds=60)
def failure():
    raise ValueError("bad code")

def test_retries():
    return failure()
In this example, the
task will run a total of three times before the flow fails, with a 60-second delay between retries. If you need to selectively retry tasks based on specific error types and messages, you might have to implement custom error handling within the task function itself. Here's the documentation on Flow and Task Retries and Tasks for more information. I hope this helps, even if it's not the most uplifting news for your error handling needs.