Result icon indicating copy to clipboard operation
Result copied to clipboard

Allowing have both Value and Errors and return Result as endpoint response

Open wjlabcwjl opened this issue 1 year ago • 1 comments

Hello,

I want to return a list of objects and a list of errors. The idea is to avoid failing the entire request, return the successfully processed objects, and include error messages for the objects failed. If I create a custom class with Value and Errors properties and use it with Result<MyClass>.Success(), it seems redundant with Result.

Actually the Error is defined as below, which I can't add element or set to a new list. public IEnumerable<string> Errors { get; protected set; } = (IEnumerable<string>) Array.Empty<string>();

Thanks, let me know what you think :)

wjlabcwjl avatar Oct 03 '24 12:10 wjlabcwjl

This doesn't seem like a common enough use case to support directly but you probably could just wrap the results.

Result<Result<List<Items>>>

Then one result holds the success and Value, which in turn includes the errors/failures?

Probably you'd still want a custom type with two lists though: ProcessedItems and ItemsWithFailures or something... just brainstorming here.

ardalis avatar Oct 03 '24 13:10 ardalis

OK thanks for the reply, finally I made a simple class like this, which is enough for my use case.

public class ResultWithErrors<T> : Result<T>
{
    public static Result<T> SuccessWithErrors(T value, IEnumerable<string> errorMessages)
    {
        return new ResultWithErrors<T> { Value = value, Errors = errorMessages };
    }
}

wjlabcwjl avatar Oct 09 '24 11:10 wjlabcwjl