-
Notifications
You must be signed in to change notification settings - Fork 309
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
Question: return the result #521
Comments
You can nest an error handler inside using CSharpFunctionalExtensions;
var user = new User();
await Result.Success(new Message("Bar", "Test Person"))
.Ensure(message => Result.SuccessIf(DoesTextHaveValidFullName(message.Text), message, Errors.FullNameIsInvalid)
.TapError(e => new TelegramBotClient().ReplyToMessageAsync(message, "Bot1", e))
)
.Bind(message => new SomeDataService().ExtractFullName(message.Text)
// Translate a service level error to client one, I guess
.MapError(_ => Errors.FullNameIsInvalid)
.TapError(e => new TelegramBotClient().ReplyToMessageAsync(message, "Bot2", e))
)
.Bind(n => user.UpdatePersonalInfo(n))
.Tap(u => new UserService().UpdateAsync(u))
.Tap(() => Console.WriteLine("User personal info has been updated"))
.TapError(e => Console.WriteLine($"[ERROR] Parse error occurred: {e}"));
bool DoesTextHaveValidFullName(string name) {
return string.IsNullOrEmpty(name) == false;
}
public record Message(string User, string Text);
public class User {
public Result<User> UpdatePersonalInfo(string fullName) {
return this;
}
}
public static class Errors {
public const string FullNameIsInvalid = "Invalid Full name";
}
public class SomeDataService {
public Result<string> ExtractFullName(string message) {
return Result.SuccessIf(message.Contains("Test Person"), "FullName", "Message does not contain full name");
}
}
public class TelegramBotClient {
public Task ReplyToMessageAsync(Message message, string user, string text) {
Console.WriteLine($"[{user}] [REPLY TO {message.Text}]: {text}");
return Task.CompletedTask;
}
}
public class UserService {
public Task UpdateAsync(User user) {
return Task.CompletedTask;
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, colleagues.
I can't figure out how to translate this code, namely, it confuses the function in case of failure.
Is it possible to combine it somehow?
Source code
What happened
The text was updated successfully, but these errors were encountered: