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

Feature Request: Add ElseDo and ElseDoAsync #139

Open
b564518128 opened this issue Jan 15, 2025 · 6 comments
Open

Feature Request: Add ElseDo and ElseDoAsync #139

b564518128 opened this issue Jan 15, 2025 · 6 comments

Comments

@b564518128
Copy link

Would really appreciate to see ElseDo and ElseDoAsync get added to the toolkit. The relationship between ElseDo and Else would be similar to that between ThenDo and Then. The best use case for ElseDo would be emitting a log when error happened but still want to carry over the error status.

example usecase:

2.ToErrorOr()
  .FailIf(val => val == 2, Error.Unexpected())
  .ThenDo(val => Console.Writeline("value is not 2")
  .ElseDo(_ => Console.Writeline("value is 2 :(")
@cottozen
Copy link

Some time ago I had the same need and added this feature In PR #117

@zbyszekprasak
Copy link

In case like this I suggest using below solution, works against 2.0.1 version:

2.ToErrorOr()
    .FailIf(val => val == 2, Error.Unexpected())
    .Switch(
        val => Console.WriteLine("value is not 2"),
        _ => Console.WriteLine("value is 2 :("));

@cottozen
Copy link

Right, but at times you only want to log something on error states. You could use the Else method but that takes a Func (instead of an Action) as its argument making the code a bit ugly in my opinion.

2.ToErrorOr()
    .FailIf(val => val == 2, Error.Unexpected())
    .Else(errors => {
        Console.WriteLine("value is 2 :(");
        return errors;
    });

compared to the ElseDo implementation.

2.ToErrorOr()
    .FailIf(val => val == 2, Error.Unexpected())
    .ElseDo(_ => Console.WriteLine("value is 2 :("));

@zbyszekprasak
Copy link

@cottozen I fully agree. My remark regards the original example posted by @b564518128 .

@b564518128
Copy link
Author

@zbyszekprasak Thanks for the response, my need is exactly what @cottozen has described though.

@b564518128
Copy link
Author

@zbyszekprasak Another issue with using Switch is that it returns void, which means it has to be the last step of the chain. However, logging needs do not only occur at the end of the chain. ElseDo can keep the chain going.

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

3 participants