Error Handling
The project uses two different Result shapes — know which one you are touching.
Public-facing functions return the tuple form. The object form only appears at the boundary where massaman's attemptAsync is called.
Rules
Use the Result Type
Define success and failure as a tuple where the first element is the error (or null) and the second is the value (or null). Destructure the tuple to check which case occurred.
Construct success and failure tuples directly:
Return Results for Expected Failures
Use Result<T, E> for operations that can fail in expected ways such as parsing, validation, file I/O, and external calls. Define a specific error interface for each domain.
Correct
Incorrect
Wrap Async Operations with attemptAsync
Import attemptAsync from massaman/control to convert promise rejections into a Result. Note: the helper returns an Ok<T> | Err object (with ok, value, error fields), not the project-native tuple.
When bridging between massaman's object Result and the project's tuple Result, destructure with .ok, .value, .error from massaman, then return [null, value] or [error, null] from your function.
Correct
Define Domain-Specific Results
Create type aliases for consistency within a domain. This keeps function signatures short and error types discoverable.
Correct
Chain Results with Early Returns
Use early returns to chain multiple Result-producing steps. Each step bails out on the first error.
Correct
Handle Multiple Error Types
Use destructuring and early returns to handle different error types. For exhaustive handling of multiple error variants, combine with match from massaman/match.
Correct
Never Throw in Result-Returning Functions
A function that declares Result as its return type must never throw. All failure paths must return an error tuple.
Correct
Incorrect
Always Check Results Before Accessing Values
Never access the value element without first confirming the error element is null. Destructure the tuple and check the error before using the value.
Correct
Incorrect
References
- Types -- Discriminated union patterns
- Conditionals --
massaman/matchfor error handling