The Right Way To Return API Errors in .NET

preview_player
Показать описание


Hello, everybody. I'm Nick, and in this video, I will show you the right or modern way of returning API errors in .NET following the latest RFC standard.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

Started a new aspnet rest project from scratch with a cqrs mediatr pattern. Threw a not found exception in the handler. Stared at it for 2 days and it all felt wrong but no idea what do. Much Grok and websites later then found this video. Thank you Nick.

VirtualWesternCape
Автор

I personally use a custom result object which can either be returned as a success or a failure. When it's a failure I can specify an error code, a message, and an http status code. Then the caller of the method checks if the result is a failure and if yes the caller also returns a failure and passes the original failed result higher. This results in errors bubbling up all the way to the controller where I have the full call stack thanks to the caller attributes which I can now log and I can also convert the result into a DTO which the API will return. It's ultra fast and no exceptions are necessary but it requires you to follow a very specific coding pattern.

Thial
Автор

Hey man, I’m not a C# dev but I just wanted to say that you make some really good content, and I really respect your hustle. Keep up the good work!!

nguyen_tim
Автор

Great tutorial on how to make unreadable code. GJ

noobFPV
Автор

Using custom exception handling approach.

We sometimes need to throw 404 not found from a DB layer when an account is trying to update entities that don’t belong to it. Sometimes we to send 422 from the service layer when an entity is not in a compatible state with the request. Being able to just throw these as soon as something is off about the request simplifies a lot of our code.

polyped
Автор

How should we return result problem from other places in the code (I mean not from controllers/endpoints directly but from some other services in our code) if not by throwing an exception?

martynachomicz
Автор

Definitely a fan of the first approach because throwing exceptions is expensive!

stephajn
Автор

been doing it this way for 15 years, one thing to add, which may be obvious, but you'll need to handle various kinds of exceptions or add a "severity"property if you want to return different kidns of status codes so that an unexpected exception doesn't get returned as a bad request for example. Though of course, I don't really throw exceptions for validation, which is handled separately,

a__random__person
Автор

What is the standard way to handle standard ProblemDetails on another API side? Like I call your example API via another API with HttpClient because the result can be the expected object or the ProblemDetails. Is there a good and standard way to do this?

Suvg
Автор

Hi Nick, funnily enough, today I got a issue assigned that has is basically "figure out a way to add generic error results to API".
How would you go about following this standard, if the API is also a backend for a SPA that needs some kind of errorCode or unique, to translate the real message to a users preferred language?

EffektUpInHere
Автор

Thank you! Are there cool ways to consume and deserialize problem details in consuming applications?

jesslilly
Автор

To put a security hat on for a second, this is really good to have a more organized and deliberate way to throw errors.

GoBayside
Автор

242 / 5 000
Thanks for showing this, including the exception part. Very useful! Although I am personally against exceptions when the problem can be foreseen, it is not always me who decides. If my client's strategy is to use exceptions, then...

persehlin
Автор

cool. How might you pass a domain specific error code as an additional property here? (to avoid leaking internal / api type text messages up to the UI)

paulmdevenney
Автор

We can throw ProblemException from switch expression without equality checking

AryZuK
Автор

this is great, follow every video you release, I want to buy the unit test course that you have, I'm having a lot of questions on how to create good unit test and correct way to do it, but not sure if it will help me to understand how to create them will all dependencies or scenarios that methods have, what can you recommend me ?

davidmontilla
Автор

Are there worlds that exist where setting a problem type parameter is going to be different from the status code parameter? I know this was a very bare bones example, but I'm just having a hard time not cringing at setting two properties/parameters to what amounts to the same value in different formats (enum vs string). That all being said, this was very eye-opening on a few different fronts and I (don't) look forward refactoring some (all) of our company's apis xD

mightygingercles
Автор

If I recall at least ast far back as net7 you would get with if you used standard controllers and the API controller attributes and model state for bad requests and you could return a validation problem manually if you wish (in combination with model state)

shakeuk
Автор

You don't need to get the traceId from the activity feature as it's already provided

stephenyork
Автор

Hey man, great video! Went straight in a implemented in my side project - great!
But i have one problem.. when using typedresults to generate a openapi spec with swagger, the documentation says nothing about a possible code 401.
Is there someway to generate a nice open api spec with the TypedResults.Problem or

holm
join shbcf.ru