|
| 1 | +--- |
| 2 | +description: Ox uses two distinct channels for errors: exceptions for bugs/unexpected situations, and application errors as values (`Either`, union types). |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Ox Error Handling: Dual Channel |
| 7 | + |
| 8 | +Ox uses two distinct channels for error handling: exceptions for bugs and unexpected situations, and application errors represented as values. |
| 9 | + |
| 10 | +## Two Error Channels |
| 11 | + |
| 12 | +1. **Exceptions**: For bugs, unexpected situations, integration with Java libraries |
| 13 | +2. **Application errors**: Represented as values using `Either`, union types, or custom data types |
| 14 | + |
| 15 | +## Exception Handling |
| 16 | + |
| 17 | +Exceptions are handled by computation combinators and propagate normally: |
| 18 | + |
| 19 | +```scala |
| 20 | +import ox.{par, race, supervised, fork} |
| 21 | + |
| 22 | +// Exceptions propagate through high-level operations |
| 23 | +val result = par( |
| 24 | + computation1(), // might throw |
| 25 | + computation2() // cancelled if computation1 throws |
| 26 | +) |
| 27 | + |
| 28 | +// Exceptions end supervised scopes |
| 29 | +supervised { |
| 30 | + fork { |
| 31 | + sleep(1.second) |
| 32 | + println("This won't print") |
| 33 | + } |
| 34 | + fork { |
| 35 | + sleep(500.millis) |
| 36 | + throw new RuntimeException("boom!") // Ends entire scope |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Use try/catch for exception handling |
| 41 | +try { |
| 42 | + riskyOperation() |
| 43 | +} catch { |
| 44 | + case e: SpecificException => handleError(e) |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Application Errors as Values |
| 49 | + |
| 50 | +For type-safe error handling, use `Either` and error modes: |
| 51 | + |
| 52 | +```scala |
| 53 | +import ox.either |
| 54 | +import ox.either.ok |
| 55 | + |
| 56 | +case class User() |
| 57 | +case class Organization() |
| 58 | +case class Assignment(user: User, org: Organization) |
| 59 | + |
| 60 | +def lookupUser(id: Int): Either[String, User] = ??? |
| 61 | +def lookupOrganization(id: Int): Either[String, Organization] = ??? |
| 62 | + |
| 63 | +// Use either block to unwrap values |
| 64 | +val result: Either[String, Assignment] = either: |
| 65 | + val user = lookupUser(1).ok() |
| 66 | + val org = lookupOrganization(2).ok() |
| 67 | + Assignment(user, org) |
| 68 | +``` |
| 69 | + |
| 70 | +## Union Types for Multiple Error Types |
| 71 | + |
| 72 | +```scala |
| 73 | +import ox.either |
| 74 | +import ox.either.ok |
| 75 | + |
| 76 | +val v1: Either[DatabaseError, String] = ??? |
| 77 | +val v2: Either[NetworkError, String] = ??? |
| 78 | + |
| 79 | +// Errors are combined in union type |
| 80 | +val result: Either[DatabaseError | NetworkError, String] = either: |
| 81 | + v1.ok() ++ v2.ok() |
| 82 | +``` |
| 83 | + |
| 84 | +## Error Modes for Advanced Scenarios |
| 85 | + |
| 86 | +```scala |
| 87 | +import ox.{supervisedError, EitherMode} |
| 88 | + |
| 89 | +// Use supervisedError for application error propagation |
| 90 | +given ErrorMode[String] = EitherMode[String] |
| 91 | + |
| 92 | +supervisedError { |
| 93 | + val f1 = forkError { |
| 94 | + Left("application error") // Propagated as value, not exception |
| 95 | + } |
| 96 | + f1.join() // Either[String, ResultType] |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Best Practices |
| 101 | + |
| 102 | +### Use Exceptions For |
| 103 | +- Bugs and programming errors |
| 104 | +- Unexpected system failures |
| 105 | +- Integration with Java libraries that throw |
| 106 | +- Violations of preconditions/invariants |
| 107 | + |
| 108 | +### Use Application Errors For |
| 109 | +- Expected business logic failures |
| 110 | +- Validation errors |
| 111 | +- API error responses |
| 112 | +- Type-safe error handling |
| 113 | + |
| 114 | +### Working with Eithers |
| 115 | +- use the `either` block within which `Either` values can be unwrapped using `.ok()` |
| 116 | + |
| 117 | +## Exception Conversion |
| 118 | + |
| 119 | +Convert exceptions to values when needed: |
| 120 | + |
| 121 | +```scala |
| 122 | +import ox.either |
| 123 | + |
| 124 | +// Convert exception-throwing code to Either |
| 125 | +val result: Either[Throwable, String] = either.catching { |
| 126 | + riskyOperationThatThrows() |
| 127 | +} |
| 128 | + |
| 129 | +// Handle specific exception types |
| 130 | +val result2: Either[SQLException, List[User]] = either.catching { |
| 131 | + databaseQuery() |
| 132 | +}.recover { |
| 133 | + case _: ConnectionException => Left(ConnectionError) |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Avoid |
| 138 | + |
| 139 | +```scala |
| 140 | +// Don't mix error handling approaches inconsistently |
| 141 | +def badExample(): Either[String, User] = { |
| 142 | + if (condition) throw new Exception("mixed approach") // Bad! |
| 143 | + else Right(user) |
| 144 | +} |
| 145 | + |
| 146 | +// Don't swallow exceptions without handling |
| 147 | +try { |
| 148 | + riskyOperation() |
| 149 | +} catch { |
| 150 | + case _ => // Bad - don't ignore errors |
| 151 | +} |
| 152 | +``` |
0 commit comments