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

How to convert Result<Foo> to Result<RandomClass> when result is not success #219

Open
ma-lar opened this issue Nov 7, 2024 · 1 comment

Comments

@ma-lar
Copy link

ma-lar commented Nov 7, 2024

Question:

I may call in a method different services. When result is success I use result.Value to continue but if there are errors like conflict or anything else I want to return and stop execution there but I want to keep errors. Map is not always possible (I guess). Is there a good way to accomplish this? Should I use map with an empty value like return result.Map(new RandomClass()) only to keep errors since value wont matter at this point. Or can I just return result.Map();?

public Result<int> ReturnIntResult(int id)
{
     Result<Foo> resultFromService = _service.GetFoo(id)
     
     if(!result.IsSucces)
    {
        //How can I return a type Result<int> with errors from resultFromService? Like conflict or anything else. I have seen some basic example with int and string using Map. But sometime _service can return value and errors which value is used later but mapping is not necessary needed for the value. Only for the error; 
        return ???;
    }

  return new Result<int>(resultFromService.Value);
}
@MoMakkawi
Copy link

MoMakkawi commented Nov 27, 2024

@ma-lar please try to use the Error function i think it may can solve your issue also you can use :
even it may be better if errors functions is not related to any types

public Result<int> ReturnIntResult(int id)
{
    Result<Foo> resultFromService = _service.GetFoo(id);

    if (!resultFromService.IsSuccess)
    {
        // Use Error method with ErrorList to return a Result<int> with errors from resultFromService
        var errorList = new ErrorList(resultFromService.Errors, resultFromService.CorrelationId);
        return Result<int>.Error(errorList);
    }

    return Result<int>.Success(resultFromService.Value.Id);
}

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