https://prefect.io logo
Title
s

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)?
m

Marvin

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:
python
from prefect import flow, task

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

@flow
def test_retries():
    return failure()
In this example, the
failure()
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.