# Try / Catch

> Defines a scope for error handling.

Source: https://docs.sommatic.ai/en/nodes/flow.try/

- Slug: `flow.try`
- Family: Flow control
- Version: 1.0.0
- Status: Available
- Execution: sync

Defines a scope for error handling.

This operator marks the beginning of a "Try" block. Any errors occurring in the downstream nodes connected to the 'do' port will be caught and routed to the corresponding "Catch" handler (structurally defined in the workflow graph).

**Behavior:**
- **Normal Flow:** Passes payload to 'do'.
- **Error Scoping:** The engine registers this node as the error handler for its children.
- **Catch:** Errors in children trigger the configured 'Catch' operator (if any).

**Use Cases:**
- Wrapping a risky API call.
- Implementing "Compensating Transactions" for a specific step.
- Graceful degradation (fallback logic).

## Configuration

This node has no configuration fields.

## Output ports

- `do` (default): Start of the Try block.

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (object, optional)

## In expressions

What a later node can read from this node's output with `$("Try / Catch").item.json.<path>` (write your node's label instead of `Try / Catch`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `do`, from the output shown in Ports.

- `$("Try / Catch").item.json.payload` (object) **The worker does not return it: reads as undefined.**
- `$("Try / Catch").item.json.order_id` (string) = `"ord_123"` (Not suggested by the editor)
- `$("Try / Catch").item.json.amount` (number) = `1250` (Not suggested by the editor)
- `$("Try / Catch").item.json.status` (string) = `"active"` (Not suggested by the editor)


## How it works

Opens an error scope: passes the payload unchanged through `do`, and the engine sends exceptions thrown by any node after it to the node connected to its `catch` edge.

1. Returns the resolved payload through `do` with `meta.scope` `try_start`.
2. The engine registers a scope whose catch node is the first node connected from this node with `from.port` `catch` (or none).
3. When a later node throws, the engine walks the graph backwards from it; if this try node is an ancestor and has a catch node, the engine queues the catch node instead of failing the run.
4. The catch node receives `{ error, source_node, source_operator }`; the failing node output is stored as `{ error, stack }`.

## In a workflow

### Node

```json
{
  "node_id": "node-try-sync",
  "label": "Try sync",
  "operator_slug": "flow.try",
  "operator_version": "1.0.0",
  "config": {}
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "order_id": "ord_123",
    "amount": 1250,
    "status": "active"
  },
  "port": "do",
  "meta": {
    "scope": "try_start"
  }
}
```

## Output ports (behaviour in the code)

### `do`

Always: the start of the protected branch.

```json
{
  "order_id": "ord_123",
  "amount": 1250,
  "status": "active"
}
```

### `error`

Only if resolving the config throws, which in practice does not happen.

```json
{
  "error": "<message>"
}
```

## Notes and limits

- Only thrown exceptions are caught. Most nodes report failures as an envelope on their own `error` port (for example `data.transform` or `flow.if`); those follow that port and never reach the catch node. Thrown failures include an operator that cannot be resolved, invalid `credential_bindings`, and workers that rethrow (such as `sommatic.app.execute`).
- `catch` is not a declared port of this node: the engine looks for an edge with `from.port` `catch`. Without it the scope is registered but exceptions still fail the run.
- The scope lasts until the run ends and covers every node that has the try node as an ancestor, including the catch branch: an exception inside the catch node is routed to it again until the 100-step limit fails the run.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "flow.try",
  "name": "Try / Catch",
  "category": "flow",
  "version": "1.0.0",
  "description": "Defines a scope for error handling.\n\n  This operator marks the beginning of a \"Try\" block. Any errors occurring in the downstream nodes connected to the 'do' port will be caught and routed to the corresponding \"Catch\" handler (structurally defined in the workflow graph).\n\n  **Behavior:**\n  - **Normal Flow:** Passes payload to 'do'.\n  - **Error Scoping:** The engine registers this node as the error handler for its children.\n  - **Catch:** Errors in children trigger the configured 'Catch' operator (if any).\n\n  **Use Cases:**\n  - Wrapping a risky API call.\n  - Implementing \"Compensating Transactions\" for a specific step.\n  - Graceful degradation (fallback logic).",
  "capabilities": [
    "error-handling",
    "scope",
    "try-catch"
  ],
  "config_schema": {
    "type": "object",
    "properties": {}
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "do",
      "label": "Do",
      "is_default": true,
      "description": "Start of the Try block."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "error",
    "try",
    "catch",
    "scope"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {}
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "do",
    "label": "Do",
    "is_default": true,
    "description": "Start of the Try block."
  }
]
```
