> For the complete documentation index, see [llms.txt](https://hinkal-team.gitbook.io/hinkal/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hinkal-team.gitbook.io/hinkal/hinkal-mobile-sdk/ios/advanced/error-handling-patterns.md).

# Error Handling Patterns

Patterns for handling failures robustly in production.

### Centralize error surfacing

Route all SDK calls through a single helper so messages are handled consistently:

```swift
func run<T>(_ op: () throws -> T) -> Result<T, Error> {
    do { return .success(try op()) }
    catch { return .failure(error) }
}

let result = run { try call {
    hinkal.deposit(chainId, tokenAddrsJSON: tokens, amountsWeiJSON: amounts,
                   preEstimateGas: true, returnTxData: false, error: &$0)
} }
```

### Distinguish user error from system error

* **User-correctable** - bad amount, invalid recipient, insufficient balance. Validate up front (see Error Handling) and prompt the user to fix the input.
* **Transient** - network or relayer errors. Offer a retry.
* **Fatal** - a failed session handshake. Prompt the user to reconnect the wallet.

### Retry safely

Reads (`getTotalBalance`, quotes) are safe to retry. For fund-moving calls, confirm the previous attempt did not land before retrying - re-read the balance with `resetCache: true` first.

### Recover stuck funds

If a scheduled operation is interrupted, funds can end up in blocked UTXOs. Detect with `getStuckShieldedBalances` and recover with `withdrawStuckUtxos` (see Balances).
