1 ---
2 sidebar_label: "no-try-catch-in-workflow"
3 ---
4
5 export const metadata = {
6 title: `no-try-catch-in-workflow - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule disallows `try/catch` statements directly inside a `createWorkflow` constructor or a `when().then()` callback.
12
13 ## Severity
14
15 `error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets files in your project's `src/workflows` directory. It reports `try/catch` statements placed directly in a workflow's constructor or in a `when().then()` callback.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/workflows/example.ts"
24 import {
25 createWorkflow,
26 WorkflowResponse,
27 } from "@medusajs/framework/workflows-sdk"
28
29 createWorkflow("my-workflow", (input) => {
30 try {
31 return new WorkflowResponse(doSomething(input))
32 } catch (e) {
33 return new WorkflowResponse(null)
34 }
35 })
36 ```
37
38 Instead, use a step's compensation function or its error-handling options:
39
40 ```ts title="src/workflows/example.ts"
41 import {
42 createWorkflow,
43 WorkflowResponse,
44 } from "@medusajs/framework/workflows-sdk"
45
46 createWorkflow("my-workflow", (input) => {
47 const result = doSomethingStep(input).config({
48 continueOnPermanentFailure: true,
49 })
50
51 return new WorkflowResponse(result)
52 })
53 ```
54
55 ## Why it's Important
56
57 A workflow's constructor and `when().then()` callbacks run at definition time, not execution time. So, a `try/catch` there doesn't catch step errors. Use steps with compensation functions, `throwOnError: false`, or `skipOnPermanentFailure` / `continueOnPermanentFailure` instead.
58
59 Learn more in the [Error Handling in Workflows documentation](!docs!/learn/fundamentals/workflows/errors).
60
61 ## Fixable
62
63 This rule isn't auto-fixable.