Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "WithErrors" methods that accept error object/error message collections? #73

Closed
wolf8196 opened this issue Oct 27, 2020 · 7 comments

Comments

@wolf8196
Copy link

I've just started with the whole concept of Result objects, and currently trying to use them/learn how to use them in the old pet project of mine.
One thing that I found lacking is the method like .WithErrors, that would accept IEnumerable of either Error objects or error messages. Because there are few places where I already have error collection of some sort, and it would be convenient to attach it to the Result.
Currently I had to write an extension method:

    public static class ResultExtensions
    {
        public static Result WithErrors(this Result result, IEnumerable<string> errorMessages)
        {
            foreach (var errorMessage in errorMessages)
            {
                result.WithError(errorMessage);
            }

            return result;
        }
    }

What are your thoughts on adding something similar to the library?
Or is there something I've missed?

@altmann
Copy link
Owner

altmann commented Oct 27, 2020

Hi thanks for your feedback! This is interesting. In which scenarios do you use this extension method?

@wolf8196
Copy link
Author

wolf8196 commented Oct 28, 2020

There are currently 2 instances.
First is after validation. FluentValidation returns ValidationResult obj with errors in it, and I put its error messages into the Result obj.

            var validationResult = validator.Validate(model);

            if (!validationResult.IsValid)
            {
                return Result
                    .Fail("Request model is invalid.")
                    .WithErrors(validationResult.Errors.Select(x => x.ErrorMessage));
            }

The second case is similar. DotLiquid's Template obj contains errors if rendering went wrong.

            var parsedTemplate = Template.Parse(template);

            var result = parsedTemplate.Render(Hash.FromDictionary(model));

            if (parsedTemplate.Errors.Any())
            {
                return Result
                    .Fail("Failed to process template.")
                    .WithErrors(parsedTemplate.Errors.Select(x => x.Message));
            }

@altmann
Copy link
Owner

altmann commented Oct 28, 2020

Thanks for the samples. I will have a look at it tomorrow but I also think that it makes sense to provide a WithErrors(...) method.

@altmann
Copy link
Owner

altmann commented Oct 29, 2020

With the current version of FluentResults you can use the method WithReasons(...) to pass multiple errors to result. A Reason can be an Error or a Success. Both types (Error and Success) inherit from type Reason. I know it's not perfect because the intent to add errors is not that much clear as with a method name like WithErrors(...).

The next version of FluentResults contains new methods to support your scenarios in a more readable way

  • Result class
    • WithErrors(IEnumerable<string> errors)
    • WithErrors(IEnumerable<Error> errors)
  • Error class
    • CausedBy(IEnumerable<string> errors)
    • CausedBy(IEnumerable<Error> errors)

Hint
You create an Result with the message "Request model is invalid." and add multiple errors to this Result object. This causes one flat list of errors in the result object. (list is accessable via result.Errors). If this is that what you want then its fine.

FluentResults supports a hierarchy of errors. If you use Result.Fail(new Error("Request model is invalid.").CausedBy(validationResult.Errors.Select(x => x.ErrorMessage))); then you create a hierarchy of errors. result.Errors contains one error with message "Request model is invalid." The causedBy errors are stored in the Reasons property of the error itself, accessable for example via result.Errors.First().Reasons.

@altmann
Copy link
Owner

altmann commented Oct 30, 2020

The new version is published https://www.nuget.org/packages/FluentResults/2.3.0

@wolf8196
Copy link
Author

Thanks for the update. Works great.
About the hierarchy.
At first I wanted to go simpler, and did that flat error list, but after your comment tried the hierarchy, and it does look way better)

@altmann
Copy link
Owner

altmann commented Oct 30, 2020

Thanks for the response. If you have any other feedback please let me know.

@altmann altmann closed this as completed Oct 30, 2020
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

No branches or pull requests

2 participants