1 import { createRule } from "../../create-rule"
2 import {
3 createWorkflowSdkBindings,
4 isInWorkflowDefinitionScope,
5 trackWorkflowSdkImports,
6 } from "../../util/workflow-scope"
7
8 type MessageIds = "tryCatchInWorkflow"
9
10 export const rule = createRule<[], MessageIds>({
11 name: "no-try-catch-in-workflow",
12 meta: {
13 type: "problem",
14 docs: {
15 description:
16 "Disallow `try/catch` statements directly inside a `createWorkflow` constructor or a `when().then()` callback. Use steps with compensation functions, `throwOnError: false`, or `skipOnPermanentFailure` / `continueOnPermanentFailure` instead.",
17 },
18 messages: {
19 tryCatchInWorkflow:
20 "Do not use `try/catch` inside a workflow constructor or a `when().then()` callback — both run at definition time, not execution time. Use steps with compensation functions, `throwOnError: false`, or `skipOnPermanentFailure` / `continueOnPermanentFailure` on the failing step.",
21 },
22 schema: [],
23 },
24 defaultOptions: [],
25 create(context) {
26 const bindings = createWorkflowSdkBindings()
27
28 return {
29 ImportDeclaration(node) {
30 trackWorkflowSdkImports(node, bindings)
31 },
32
33 TryStatement(node) {
34 if (bindings.createWorkflow.size === 0) {
35 return
36 }
37 if (!isInWorkflowDefinitionScope(node, bindings)) {
38 return
39 }
40 context.report({
41 node,
42 messageId: "tryCatchInWorkflow",
43 })
44 },
45 }
46 },
47 })
48
49 export default rule