π Result Pattern v2.2.0 - Introducing unwrapOrGetErrors()
β¨ What's New
This release introduces a powerful new method that makes working with errors even more convenient:
π unwrapOrGetErrors()
- The Best of Both Worlds
// Before: You had to handle Ok and Fail cases separately
if (result.isOk) {
const value = result.value;
// Use value
} else {
const error = result.value;
// Handle error
}
// Now: Get either the value or the error in one call!
const valueOrError = result.unwrapOrGetErrors();
// If result is Ok, valueOrError contains the success value
// If result is Fail, valueOrError contains the error value
This method is especially powerful when working with combined results:
const email = Email.try("[email protected]");
const password = StrongPassword.try("weak123");
const combined = ResultUtils.combine(email, password);
// Get either the valid data or all validation errors at once
console.log(combined.unwrapOrGetErrors());
// If validation fails: ["Password too weak"]
// If validation passes: [Email, StrongPassword]
π Why This Matters
The new unwrapOrGetErrors()
method solves a common pain point when working with the Result Pattern:
- β No more type constraints when you just want to access error values
- β Cleaner code with fewer conditional branches
- β Simplified error handling in combined results
- β Better developer experience with less boilerplate
π Complete Feature List
This release builds on our solid foundation:
- β
Type-safe error handling with
Ok<V, E>
andFail<V, E>
classes - β
Powerful transformation methods like
map
,flatMap
, andmapFails
- β
Error aggregation with
ResultUtils.combine
- β
Flexible value extraction with
unwrap
,unwrapOr
, andunwrapOrElse
- β
Logical operations with
and
andor
methods - β
Value/error swapping with
flip
- β
NEW: Direct error access with
unwrapOrGetErrors
π οΈ Migration Guide
This is a minor release with full backward compatibility. Simply update your package:
# npm
npm update @eicode/result-pattern
# yarn
yarn upgrade @eicode/result-pattern
# pnpm
pnpm update @eicode/result-pattern
Then start using the new unwrapOrGetErrors()
method wherever you need to access either success values or error values without type constraints.
π Usage Examples
Form Validation
function validateForm(formData: FormData) {
const name = validateName(formData.get('name'));
const email = validateEmail(formData.get('email'));
const password = validatePassword(formData.get('password'));
const result = ResultUtils.combine(name, email, password);
// Get either the validated data or all validation errors
const dataOrErrors = result.unwrapOrGetErrors();
if (result.isFail) {
// dataOrErrors is an array of error messages
displayErrors(dataOrErrors);
} else {
// dataOrErrors is a tuple of validated values
submitForm(dataOrErrors);
}
}
API Request Handling
async function fetchUserData(userId: string) {
try {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
return new Fail(`HTTP error: ${response.status}`);
}
const data = await response.json();
return new Ok(data);
} catch (error) {
return new Fail(`Network error: ${error.message}`);
}
}
// In your component:
const userData = await fetchUserData(userId);
const userOrError = userData.unwrapOrGetErrors();
if (userData.isOk) {
renderUserProfile(userOrError);
} else {
showErrorMessage(userOrError);
}
Made with π», β, and a bit of πͺ by @caiolandgraf