Skip to content

Commit 0002cf9

Browse files
bruxodasilvasidharthachatterjeeelithrar
authored
Workflows: NonRetryableErrors Section Improvement (#17819)
* Reenforce that for NonRetryableErrors, users need to manage the error or the Workflow will fail * Added code example to catch NonRetryableError --------- Co-authored-by: Sidhartha Chatterjee <[email protected]> Co-authored-by: Matt Silverlock <[email protected]>
1 parent 97efde9 commit 0002cf9

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/content/docs/workflows/build/sleeping-and-retrying.mdx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let someState = step.do("call an API", {
8787
}, async () => { /* Step code goes here /* }
8888
```
8989
90-
## Force a Workflow to fail
90+
## Force a Workflow instance to fail
9191
9292
You can also force a Workflow instance to fail and _not_ retry by throwing a `NonRetryableError` from within the step.
9393
@@ -111,3 +111,38 @@ export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
111111
```
112112
113113
The Workflow instance itself will fail immediately, no further steps will be invoked, and the Workflow will not be retried.
114+
115+
## Catch Workflow errors
116+
117+
Any uncaught exceptions that propagate to the top level, or any steps that reach their retry limit, will cause the Workflow to end execution in an `Errored` state.
118+
119+
If you want to avoid this, you can catch exceptions emitted by a `step`. This can be useful if you need to trigger clean-up tasks or have conditional logic that triggers additional steps.
120+
121+
To allow the Workflow to continue its execution, surround the intended steps that are allowed to fail with a `try-catch` block.
122+
123+
124+
```ts
125+
...
126+
await step.do('task', async () => {
127+
// work to be done
128+
});
129+
130+
try {
131+
await step.do('non-retryable-task', async () => {
132+
// work not to be retried
133+
throw new NonRetryableError('oh no');
134+
});
135+
} catch(e as Error) {
136+
console.log(`Step failed: ${e.message}`);
137+
await step.do('clean-up-task', async () => {
138+
// Clean up code here
139+
});
140+
}
141+
142+
// the Workflow will not fail and will continue its execution
143+
144+
await step.do('next-task', async() => {
145+
// more work to be done
146+
});
147+
...
148+
```

0 commit comments

Comments
 (0)