Skip to content

πŸš€ Result Pattern v2.2.0 - Introducing unwrapOrGetErrors()

Compare
Choose a tag to compare
@caiolandgraf caiolandgraf released this 13 Mar 19:11
· 13 commits to main since this release

✨ 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> and Fail<V, E> classes
  • βœ… Powerful transformation methods like map, flatMap, and mapFails
  • βœ… Error aggregation with ResultUtils.combine
  • βœ… Flexible value extraction with unwrap, unwrapOr, and unwrapOrElse
  • βœ… Logical operations with and and or 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