C
C#10mo ago
UltraWelfare

Tips on error handling

I'm currently using OneOf to handle errors. For example:
public async Task<OneOf<None, NotFound, List<ValidationFailure>, GenericError>> EditOrderItem(Guid uid, EditOrderItemOptions options)
public async Task<OneOf<None, NotFound, List<ValidationFailure>, GenericError>> EditOrderItem(Guid uid, EditOrderItemOptions options)
However that is kinda of mouthful. I could try to refactor it using OneOfBase, but there are cases where I need <None, NotFound> or <None, NotFound, GenericError> or really any combination... Is there any way I can refactor it? I'd like to avoid exceptions as this would migrate the problem to "try-catching" everything then `if - is" on the exceptions
2 Replies
Mango
Mango10mo ago
You could make an error interface that holds an error enum and reason. Return the data you want, or an IError. OneOf<Data, IError> I haven’t really used OneOf and I don’t know if this is a scenario where it should be used, but this is an idea I have. Alternatively if you’re not returning any data to the method and just want to return any possible errors, IError MyMethod() { }
UltraWelfare
UltraWelfareOP10mo ago
The problem is that sometimes I want a List of validation failures (Fluent validation) other times I want just a string (generic error) and other times I want a Not Found error I basically ended up doing something like you said OneOf<Result, Errors> Where Result can be any type but Errors is : OneOf<NotFound, GenericError, List<ValidationFailure>> It's basically Result | (NotFound | GenericError | List<ValidationFailure>) The only problem is, if a function doesn't give out a specific error I'd still have to declare a callback for it.. but eh
Want results from more Discord servers?
Add your server