Allowing have both Value and Errors and return Result as endpoint response
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 :)
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.
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 };
}
}