Skip to main content
Design Patterns design recommended

Result/Either Pattern

The Result/Either Pattern provides explicit error handling using discriminated unions instead of exceptions. Enables composable, type-safe error handling with map/flatMap operations.

Difficulty
intermediate
Read time
1 min read
Version
v1.0.0
Confidence
established
Last updated

Quick Reference

Result Pattern: Return Result<T, E> instead of throwing exceptions. Ok(value) for success, Err(error) for failure. Benefits: explicit error handling, type-safe, composable (map, flatMap). Use for expected failures (validation, not found). Keep exceptions for unexpected/unrecoverable errors.

Use When

  • Expected error cases (validation, not found)
  • Composing multiple fallible operations
  • API boundaries where errors should be explicit
  • Functional programming style

Skip When

  • Truly exceptional circumstances
  • Simple scripts where exceptions are fine
  • Framework requires exceptions (e.g., Express middleware)

Result/Either Pattern

The Result/Either Pattern provides explicit error handling using discriminated unions instead of exceptions. Enables composable, type-safe error handling with map/flatMap operations.

Tags

result either error-handling functional design-patterns

Discussion