Skip to content

Add IEnumerable<Error> extension methods for simplified AggregateError creation - #53

Draft
MrBogomips with Copilot wants to merge 2 commits into
rel/prodfrom
copilot/fix-10
Draft

MrBogomips with Copilot wants to merge 2 commits into
rel/prodfrom
copilot/fix-10

Conversation

Copilot AI commented Jul 2, 2025

Copy link
Copy Markdown
Contributor

This PR introduces convenient extension methods for IEnumerable<Error> to simplify the creation of AggregateError instances, addressing the need for better error aggregation patterns.

Changes Made

New Extension Methods

  • ToAggregateError() - Creates an AggregateError with the default message "Multiple errors occurred"
  • ToAggregateError(string message) - Creates an AggregateError with a custom message

Usage Examples

// Simple aggregation with default message
var validationErrors = new[]
{
    new LogicError("Name is required"),
    new LogicError("Email format is invalid"),
    new LogicError("Age must be positive")
};

var aggregateError = validationErrors.ToAggregateError();
// Creates AggregateError with message "Multiple errors occurred"

// Custom message for business context
var businessErrors = new[]
{
    new LogicError("Insufficient balance"),
    new LogicError("Account suspended")
};

var customError = businessErrors.ToAggregateError("Payment validation failed");
// Creates AggregateError with custom message

Validation Scenarios

public Result<User> ValidateUser(UserInput input)
{
    var errors = new List<Error>();
    
    if (string.IsNullOrEmpty(input.Name))
        errors.Add(new LogicError("Name is required"));
        
    if (!IsValidEmail(input.Email))
        errors.Add(new LogicError("Email format is invalid"));
    
    if (errors.Any())
        return Result.Failure<User>(errors.ToAggregateError());
        
    return Result.Success(new User(input.Name, input.Email));
}

Files Added/Modified

  • New: src/Monads/Error/ErrorEnumerableExtensions.cs - Extension methods implementation
  • New: test/Monads.UnitTests/ErrorTests/ErrorEnumerableExtensionsTests.cs - Comprehensive test suite (9 tests)
  • Updated: README.md - Added new section "IEnumerable Extensions" with examples and updated Table of Contents

Testing

  • All existing tests continue to pass (336 total tests)
  • Added 9 new unit tests covering various scenarios including null handling, empty collections, mixed error types, and custom messages
  • Manual verification confirms extension methods work as expected

The implementation follows existing patterns in the codebase and provides a clean, minimal API for error aggregation scenarios.

Fixes #10.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: MrBogomips <864417+MrBogomips@users.noreply.github.com>
Copilot AI changed the title [WIP] Errors Aggregate Add IEnumerable<Error> extension methods for simplified AggregateError creation Jul 2, 2025
Copilot AI requested a review from MrBogomips July 2, 2025 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Errors Aggregate

2 participants