# W4010 catch_all_on_open_union

URL: /docs/errors/W4010
Markdown: /docs/errors/W4010.md

> A catch uses _ => … on an error union that may grow.

A warning from the `pattern` phase. `sakura explain W4010` prints this explanation offline.

A `catch` uses `_ => …` on an error union that may grow. New error tags would be silently
swallowed; list the tags instead (design §5.3).

Warning:

```sakura
fn or_default<T, E>(body: fn() -> T ! {Fail<E>}, default: T) -> T {
  try { body() } catch { _ => default }
}
```

Fixed:

```sakura
fn or_default<T>(body: fn() -> T ! {Fail<Refused | Timeout>}, default: T) -> T {
  try { body() } catch { Timeout => default, Refused => default }
}
```

## Wrong program [#wrong-program]

[`compiler/tests/ui/W4010_catch_all_on_open_union.sakura`](https://github.com/icantcodefyi/sakuralang/blob/master/compiler/tests/ui/W4010_catch_all_on_open_union.sakura), which the compiler's ui suite runs through `sakura check`:

```sakura
fn or_default<T, E>(body: fn() -> T ! {Fail<E>}, default: T) -> T {
  try { body() } catch {
    _ => default,
  }
}
```

`sakura check --json` reports this record (pretty-printed; `$DIR` is the fixture's directory):

```json
{
  "schema_version": "1.0",
  "kind": "diagnostic",
  "id": "d:d035a4a83bf4f903",
  "code": "W4010",
  "severity": "warning",
  "phase": "pattern",
  "message": "this catch-all arm catches every error of the open type `E`, including ones callers add later; match specific tags where you can",
  "snapshot": "sha256:$SNAPSHOT",
  "symbol_id": "main::W4010_catch_all_on_open_union::or_default",
  "spans": [
    {
      "uri": "file://$DIR/W4010_catch_all_on_open_union.sakura",
      "content_hash": "sha256:c1845e4f3393b763ee6b588a26a8e1b3a46aaec1bcfd8d8a934b86a5eb963015",
      "start_byte": 131,
      "end_byte": 132,
      "role": "primary"
    }
  ],
  "related_ids": [],
  "expected": null,
  "actual": null,
  "obligation": null,
  "counterexample": null,
  "explanation_uri": "sakura://explain/W4010",
  "fixes": []
}
```

## Fixed program [#fixed-program]

[`compiler/tests/ui/W4010_catch_all_on_open_union.fixed.sakura`](https://github.com/icantcodefyi/sakuralang/blob/master/compiler/tests/ui/W4010_catch_all_on_open_union.fixed.sakura), which `sakura check` accepts with no diagnostics:

```sakura
error Timeout

error Refused

fn or_default<T>(body: fn() -> T ! {Fail<Refused | Timeout>}, default: T) -> T {
  try { body() } catch {
    Timeout => default,
    Refused => default,
  }
}
```
