
result-types
Never throw for expected failures. Use Result<T, E> types with explicit error handling and workflow
作者 jagreehal|オープンソース
Typed Errors: Never Throw
Critical rules
- Expected failures return
Result<T, E>— visible in the signature, exhaustively handled, composable. - Reserve
throw/assertsfor invariant violations and corrupted state only. - Compose fallible steps with
createWorkflow/step(short-circuit onerr). Bridge throws withstep.try(). - Map Results to HTTP in one shared boundary mapper — not per-handler ad hoc.
- Enumerate every expected failure in
E(nostring/any). Group large unions by domain. - Before drafting examples or error shapes, read references/examples.md and references/patterns.md.
Workflow
- List expected failure modes for the function; choose string literals or discriminated unions.
- Return
ok/errfrom corefn(args, deps)— catch infra exceptions at the edge of the function. - Before chaining steps, read references/examples.md for
step/step.try/step.fromResult. - Compose with
createWorkflow; keep business functions free of retry (useresilience). - At the HTTP boundary, map errors via one status table. Exhaustive
switchonresult.error. - If error unions grow large, group them — see references/patterns.md.
Resources
- references/examples.md — Result shape, workflow, HTTP mapping, error type forms. Read when implementing.
- references/patterns.md — grouping, when to throw/asserts, rationalizations. Read when designing error models.
Validation
- Expected failures return
Result<T, E>, not throwingPromise<T> -
Eenumerates every expected failure - Chains use
createWorkflow/step; third-party throws bridged withstep.try() - HTTP status mapping is shared and exhaustive
-
throw/assertsonly for invariants / impossible states
Constraints
- Do not return Result for programmer errors. Do not use
nullto signal distinct failures. - Related:
fn-args-deps,validation-boundary,resilience,api-design,observability.