-
Notifications
You must be signed in to change notification settings - Fork 94
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
Extend API to facilitate multiple error handling #112
Comments
I am also looking at a similar feature like this. On top of that, I am also trying to have
|
You can achive this behavior by forwarding the result using Created an example for you using ErrorOr;
ErrorOr<TransactionGroup> Handle(ISender sender, int externalUserId, int transactionGroupId) =>
new UserQuery(externalUserId)
.ToErrorOr()
.Then(userQuery => sender.Send(userQuery))
.Then(user => (user, new TransactionSingleQuery(externalUserId, transactionGroupId)))
.Then(tuple => (tuple.Item1, sender.Send(tuple.Item2)))
.Then(
(tuple) =>
{
User user = tuple.Item1;
TransactionGroup transactionGroup = tuple.Item2;
// do work with these
return transactionGroup;
}
);
record User(int Id, string Name);
record UserQuery(int ExternalUserId);
record TransactionSingleQuery(int ExternalUserId, int TransactionGroupId);
record TransactionGroup(int Id, DateTime TransactionDateTime);
interface ISender
{
User Send(UserQuery query);
TransactionGroup Send(TransactionSingleQuery query);
} |
To achieve the similar behavior. I created a custom extension like this. public static class Errors
{
public static List<Error> Combine(params IErrorOr[] errorOrs) =>
errorOrs
.Where(x => x.IsError)
.Where(x => x.Errors is not null)
.SelectMany(x => x.Errors!)
.ToList();
} Maybe a static method on You can find an example usage of the extension method I created below. ErrorOr<Success> HandleUpdateUser()
{
User user = GetUser();
if (
Errors.Combine(user.SetFirstName(string.Empty), user.SetLastName(string.Empty))
is var errors
and { Count: not 0 }
)
{
return errors;
}
return Result.Success;
}
class User
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public ErrorOr<Updated> SetFirstName(string firstName)
{
if (string.IsNullOrWhiteSpace(firstName))
{
return Error.Validation(description: "First name is required");
}
return Result.Updated;
}
public ErrorOr<Updated> SetLastName(string lastName)
{
if (string.IsNullOrWhiteSpace(lastName))
{
return Error.Validation(description: "Last name is required");
}
return Result.Updated;
}
} |
What dou you think about the following propose?
Before:
After (Option 1 - Separate class)
After (Option 2 - ErrorOrFactory)
Implementation propose:
Of course, improvements are required to handle cases like null values.
The text was updated successfully, but these errors were encountered: