UNPKG

workflow

Version:

Workflow DevKit - Build durable, resilient, and observable workflows

114 lines (87 loc) 2.77 kB
--- title: RetryableError description: Throw to retry a step, optionally after a specified duration. type: reference summary: Throw RetryableError in a step to trigger a retry with an optional delay. prerequisites: - /docs/foundations/errors-and-retries related: - /docs/api-reference/workflow/fatal-error --- When a `RetryableError` is thrown in a step, it indicates that the workflow should retry a step. Additionally, it contains a parameter `retryAfter` indicating when the step should be retried after. You should use this when you want to retry a step or retry after a certain duration. ```typescript lineNumbers import { RetryableError } from "workflow" async function retryableWorkflow() { "use workflow" await retryStep(); } async function retryStep() { "use step" throw new RetryableError("Retryable!") // [!code highlight] } ``` <Callout> The difference between `Error` and `RetryableError` may not be entirely obvious, since when both are thrown, they both retry. The difference is that `RetryableError` has an additional configurable `retryAfter` parameter. </Callout> ## API Signature ### Parameters <TSDoc definition={` import { type RetryableErrorOptions } from "workflow"; interface RetryableError { options?: RetryableErrorOptions; message: string; } export default RetryableError;`} /> #### RetryableErrorOptions <TSDoc definition={` import { type RetryableErrorOptions } from "workflow"; export default RetryableErrorOptions;`} /> ## Examples ### Retrying after a duration `RetryableError` can be configured with a `retryAfter` parameter to specify when the step should be retried after. ```typescript lineNumbers import { RetryableError } from "workflow" async function retryableWorkflow() { "use workflow" await retryStep(); } async function retryStep() { "use step" throw new RetryableError("Retryable!", { retryAfter: "5m" // - supports "5m", "30s", "1h", etc. // [!code highlight] }) } ``` You can also specify the retry delay in milliseconds: ```typescript lineNumbers import { RetryableError } from "workflow" async function retryableWorkflow() { "use workflow" await retryStep(); } async function retryStep() { "use step" throw new RetryableError("Retryable!", { retryAfter: 5000 // - 5000 milliseconds = 5 seconds // [!code highlight] }) } ``` Or retry at a specific date and time: ```typescript lineNumbers import { RetryableError } from "workflow" async function retryableWorkflow() { "use workflow" await retryStep(); } async function retryStep() { "use step" throw new RetryableError("Retryable!", { retryAfter: new Date(Date.now() + 60000) // - retry after 1 minute // [!code highlight] }) } ```