Errors & Retrying
How to deal with errors and write reliable tasks.
When an uncaught error is thrown inside your task, that task attempt will fail.
You can configure retrying in two ways:
- In your trigger.config file you can set the default retrying behavior for all tasks.
- On each task you can set the retrying behavior.
By default when you create your project using the CLI init command we disabled retrying in the DEV environment. You can enable it in your trigger.config file.
A simple example with OpenAI
This task will retry 10 times with exponential backoff.
openai.chat.completions.create()
can throw an error.- The result can be empty and we want to try again. So we manually throw an error.
Combining tasks
One way to gain reliability is to break your work into smaller tasks and trigger them from each other. Each task can have its own retrying behavior:
Another benefit of this approach is that you can view the logs and retry each task independently from the dashboard.
Retrying smaller parts of a task
Another complimentary strategy is to perform retrying inside of your task.
We provide some useful functions that you can use to retry smaller parts of a task. Of course, you can also write your own logic or use other packages.
retry.onThrow()
You can retry a block of code that can throw an error, with the same retry settings as a task.
If all of the attempts with retry.onThrow
fail, an error will be thrown. You can catch this or
let it cause a retry of the entire task.
retry.fetch()
You can use fetch
, axios
, or any other library in your code.
But we do provide a convenient function to perform HTTP requests with conditional retrying based on the response:
If all of the attempts with retry.fetch
fail, an error will be thrown. You can catch this or let
it cause a retry of the entire task.
Advanced error handling and retrying
We provide a handleError
callback on the task and in your trigger.config
file. This gets called when an uncaught error is thrown in your task.
You can
- Inspect the error, log it, and return a different error if you’d like.
- Modify the retrying behavior based on the error, payload, context, etc.
If you don’t return anything from the function it will use the settings on the task (or inherited from the config). So you only need to use this to override things.
OpenAI error handling example
OpenAI calls can fail for a lot of reasons and the ideal retry behavior is different for each.
In this complicated example:
- We skip retrying if there’s no Response status.
- We skip retrying if you’ve run out of credits.
- If there are no Response headers we let the normal retrying logic handle it (return undefined).
- If we’ve run out of requests or tokens we retry at the time specified in the headers.
Preventing retries
Using AbortTaskRunError
You can prevent retries by throwing an AbortTaskRunError
. This will fail the task attempt and disable retrying.
Using try/catch
Sometimes you want to catch an error and don’t want to retry the task. You can use try/catch as you normally would. In this example we fallback to using Replicate if OpenAI fails.
Was this page helpful?