C
C#5mo 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
Mango5mo 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
UltraWelfare5mo 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
More Posts
✅ Trying to send message to a specific user in signalR but javascript is just not recieving the dataTrying to send message to a specific user in signalR but javascript is just not recieving the data IOnline solo class for learning Cloud Azure.Hi, can someone recommend good online course/training with /expert/interesting/ho to feel/ Cloud Social Media & AI .Hi All, Just a quick question for those interested in AI technology, specifically with Social MediaVisual Studio 2022 winforms problemEror: CS1061 ```csharp private void btn_Giris_Click(object sender, EventArgs e) { if (!sCombine string formatting (eg `$"{number:N2}" with D2) so like `{number:N2D2}`)You have string interpolation like $"{number:N2}", how can I combine formats? Like $"{number:N2D2}".NET MAUI Android Error: Type androidx.collection.ArrayMapKit is defined multiple timesthis error happens to me when i download firebase storage or firebase firestore plugins. New projectI'm getting "specified method is not supported" when I include a specific navigation property with eI'm using ASP.NET Core Identity with a custom IdentityUser that looks like this: public class AppliIs there a way to authenticate using OAuth2 token and map it to internal JWTBasically, my server checks for some stuff by utilizing OAuth2 token and returns JWT Im looking for Beginner question. what does this createhostbuilder do?I'm learning .net core and I saw this code in program.cs. I can understand the main method calls thiim creating a game in unity for school, and need some help with a script errorso the purpose of the script is to freeze the character in place and block all inputs when "p" is pr