Global Error Handling in C# Minimal APIs

  Рет қаралды 13,385

IAmTimCorey

IAmTimCorey

Ай бұрын

Whenever possible, you should handle errors directly in the call chain. However, it is also good to have a global error handler installed so that no implementation details get leaked to the public.
In this video, we will create a basic Minimal API with a broken endpoint. Then, we will create a global error handler to address the error, in case we do not catch it in the specific endpoint where it happens.
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.app/?Resource=Api...
Mailing List: signup.iamtimcorey.com/

Пікірлер: 46
@dasfahrer8187
@dasfahrer8187 Ай бұрын
Man, this is so much nicer than rolling your own GEH.
@IAmTimCorey
@IAmTimCorey Ай бұрын
Agreed.
@DeliberateGeek
@DeliberateGeek Ай бұрын
As always, great content! As a consultant, I often find myself not writing code full-time for months at a time. I love coming back to your channel to catch up. It's been a hot minute since I've had to worry about setting up global exception handling. This is clearly a demo, so you haven't tried to demonstrate every little detail, but I just wanted to sort of validate my thinking with you so that I'm sure I haven't missed out on a change. I would imagine that the goal would be to provide a message that is useful to the end-user without exposing potentially dangerous information. Given that, I would imagine that in the output payload, it would make sense to include information like where the user could go to get support along with some sort of logger or correlation ID that would allow the support staff or developers to find the exact log entry associated with the underlying exception.
@IAmTimCorey
@IAmTimCorey Ай бұрын
Correct, that would be ideal.
@andergarcia1115
@andergarcia1115 Ай бұрын
Master, thank you for your valuable content and consistent work. The best, as always!
@IAmTimCorey
@IAmTimCorey Ай бұрын
Thank you!
@xelit3
@xelit3 Ай бұрын
Amazing video and simple example, thanks Tim!
@IAmTimCorey
@IAmTimCorey Ай бұрын
You are welcome.
@ishaakmall2390
@ishaakmall2390 Ай бұрын
This was so simple. Thanks for such a clear cut video.
@IAmTimCorey
@IAmTimCorey Ай бұрын
You are welcome.
@miles6875
@miles6875 Ай бұрын
Love your work
@IAmTimCorey
@IAmTimCorey Ай бұрын
Thanks!
@manishrawat1079
@manishrawat1079 Ай бұрын
Seems very useful, thanks!
@IAmTimCorey
@IAmTimCorey Ай бұрын
You are welcome.
@nillname9507
@nillname9507 Ай бұрын
Geat video. I wonder what is a good practice in terms of error handling in web API. I have try-catch in both the service methods and the controller actions and log the error in the service and throw it again so controller can catch it and log it. Should I always return a 500 response to client when there is an error.
@IAmTimCorey
@IAmTimCorey Ай бұрын
You should let the user know something went wrong. That doesn't always mean a 500, but it does mean that you return some type of error code. Just don't return a 200 with a body that has an error message. That is a BAD practice.
@RickInvents
@RickInvents Ай бұрын
Thanks Tim!
@IAmTimCorey
@IAmTimCorey Ай бұрын
You are welcome.
@user-tq7is7qu8z
@user-tq7is7qu8z Ай бұрын
Thanks Tim...
@IAmTimCorey
@IAmTimCorey Ай бұрын
You are welcome.
@broadshare
@broadshare Ай бұрын
Interesting stuff
@IAmTimCorey
@IAmTimCorey Ай бұрын
Thanks!
@TheDogfighter13
@TheDogfighter13 Ай бұрын
Hi @IAmTimCorey , awesome video as always , i have a video suggestion if you would consider it , How do you find a junior job in an overcrowded market , (I've got in deep in research there but this year it's going to be a nightmare finding a junior position , (side effect of overhiring juniors in 2022 and then letting half of them go early last year as far as i can figure it out) ) Any advice perhaps because after 3 months i am struggling to find even an unpaid internship. My best Nick
@IAmTimCorey
@IAmTimCorey Ай бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@ruwantharatnayake5004
@ruwantharatnayake5004 Ай бұрын
As it seems to be a good practice that must have, why such is not included in the default template when creating a project?
@IAmTimCorey
@IAmTimCorey Ай бұрын
Because different companies implement this differently. It is up to you to choose how best to set this up.
@_miranHorvat
@_miranHorvat Ай бұрын
Would be nice to see how to implement the same pattern with gRPC and JsonTranscoding.
@JorgeSaucedo-bv8gu
@JorgeSaucedo-bv8gu Ай бұрын
How did you get the colored braces? They look so cool, I want them.
@IAmTimCorey
@IAmTimCorey Ай бұрын
Here you go: kzbin.info/www/bejne/qpWbYptjYsaZrq8si=n6bbvSJ8c37qL5jc&t=1504
@user-tq7is7qu8z
@user-tq7is7qu8z Ай бұрын
Can this implementation be done in separate class and referenced in Program.cs? just to keep things clean
@IAmTimCorey
@IAmTimCorey Ай бұрын
Sure.
@othmanetibba
@othmanetibba Ай бұрын
why if using IEndpointFilters to handle the errors is it a bad approach ?
@IAmTimCorey
@IAmTimCorey Ай бұрын
I'm not sure I understand your question. In this video, I am showing you how to catch errors at the global level (any unhandled exception from any endpoint) in case you don't handle them properly in your endpoint. An EndpointFilter wouldn't really change anything here. You could run a filter to check for bad data, etc., but that doesn't stop an unhandled exception and it doesn't address the issue across endpoints.
@MichaelEpprecht
@MichaelEpprecht Ай бұрын
Why not return "application/problem+json" ?
@IAmTimCorey
@IAmTimCorey Ай бұрын
The purpose of this demonstration was not to show you what to put in your global exception handler or how to process a specific scenario. It was to show you how to set up a global error handler.
@gonace
@gonace Ай бұрын
I freaking hate the minimal api pattern, and returning a 400 Bad Request indicated that there was something wrong with the request from the klients side as malformed xml or json so that would not be something you'd like to return if there was a server error.
@spacemanjack777
@spacemanjack777 Ай бұрын
You are mixing things up. The 400 wasn't a replacement for the 500. Returning the 400 from the endpoint was just an example when handling a specific known exception. Could have been something else; a 401 or 404 or whatever. Returning the 500 in the global exception handler was just an example for how to deal with any unknown exceptions you didn't handle in the endpoint. Didn't have to be a 500 either; the 500 was just an example as well. Btw, what's wrong with minimal API?
@md.redwanhossain6288
@md.redwanhossain6288 Ай бұрын
There is no point of using a MVC controller for JSON based API.
@IAmTimCorey
@IAmTimCorey Ай бұрын
As @jakobjrgensen3223 pointed out, the point of this video isn't to tell you what to do inside of that global error handler (or the error handling in the endpoint). The point was to show you to that you could do something in those areas. What you do will depend on your situation. Trying to come up with the exact right thing to do in each would have greatly extended the video, would have made the demonstration less clear, and would not have covered every situation. As for hating minimal APIs, why? They are the new standard that most things are moving to. They are simpler to set up, have less overhead, and are actually faster as well (runtime performance, not just faster to build).
@gonace
@gonace Ай бұрын
@@IAmTimCorey thanks I missed that, it's just a matter of opinion, I feel it's more cluttered and in compination with file-scoped namespaces it begins to be hard, at least to me, to read. Brackets are esensial for me to read code
@tejp33
@tejp33 Ай бұрын
Sir I am stuck with navigation in wpf for page to window then multiple windows , and closing of previous page can you guide me
@IAmTimCorey
@IAmTimCorey Ай бұрын
That sounds like a topic for a video. I would recommend you work on a clear title and a really clear description of what you are looking to see and then you can add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/ Just be sure to clarify what you are asking for. Right now, I'm not certain I understand.
@tejp33
@tejp33 Ай бұрын
@@IAmTimCorey ok sir thank you...
A Cheaper Way to get the Most out of your Computer
6:42
Drew Klassen
Рет қаралды 733
одни дома // EVA mash @TweetvilleCartoon
01:00
EVA mash
Рет қаралды 6 МЛН
didn't want to let me in #tiktok
00:20
Анастасия Тарасова
Рет қаралды 9 МЛН
Мы играли всей семьей
00:27
Даша Боровик
Рет қаралды 3,6 МЛН
How to Get a Developer Job - Even in This Economy [Full Course]
3:59:46
freeCodeCamp.org
Рет қаралды 2,1 МЛН
UI Updates for HTTP Verbs: Building a Postman Clone Course
21:02
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 246 М.
3 .NET "Best Practices" I Changed My Mind About
10:16
Nick Chapsas
Рет қаралды 97 М.
Code Snippets in Visual Studio
5:38
Stephen Samuelsen
Рет қаралды 2 М.
The Path to C# in 2024
1:03:41
IAmTimCorey
Рет қаралды 63 М.
Stop Using the Worst Way to Loop Lists in .NET!
9:35
Nick Chapsas
Рет қаралды 38 М.
Make Your LINQ Up to 10x Faster!
11:08
Nick Chapsas
Рет қаралды 51 М.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Nick Chapsas
Рет қаралды 81 М.