Global Error Handling in C# Minimal APIs

2024 ж. 24 Нау.
13 630 Рет қаралды

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/

Пікірлер
  • Man, this is so much nicer than rolling your own GEH.

    @dasfahrer8187@dasfahrer81872 ай бұрын
    • Agreed.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • 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.

    @DeliberateGeek@DeliberateGeek2 ай бұрын
    • Correct, that would be ideal.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Master, thank you for your valuable content and consistent work. The best, as always!

    @andergarcia1115@andergarcia11152 ай бұрын
    • Thank you!

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Amazing video and simple example, thanks Tim!

    @xelit3@xelit3Ай бұрын
    • You are welcome.

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • This was so simple. Thanks for such a clear cut video.

    @ishaakmall2390@ishaakmall2390Ай бұрын
    • You are welcome.

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • 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.

    @nillname9507@nillname95072 ай бұрын
    • 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.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Seems very useful, thanks!

    @manishrawat1079@manishrawat10792 ай бұрын
    • You are welcome.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Love your work

    @miles6875@miles68752 ай бұрын
    • Thanks!

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Thanks Tim!

    @RickInvents@RickInventsАй бұрын
    • You are welcome.

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • Thanks Tim...

    @user-tq7is7qu8z@user-tq7is7qu8z2 ай бұрын
    • You are welcome.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Interesting stuff

    @broadshare@broadshareАй бұрын
    • Thanks!

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • 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

    @TheDogfighter13@TheDogfighter13Ай бұрын
    • 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/

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • Would be nice to see how to implement the same pattern with gRPC and JsonTranscoding.

    @_miranHorvat@_miranHorvat2 ай бұрын
  • How did you get the colored braces? They look so cool, I want them.

    @JorgeSaucedo-bv8gu@JorgeSaucedo-bv8guАй бұрын
    • Here you go: kzhead.info/sun/p8hxYcxlZJOcq4U/bejne.htmlsi=n6bbvSJ8c37qL5jc&t=1504

      @IAmTimCorey@IAmTimCoreyАй бұрын
  • As it seems to be a good practice that must have, why such is not included in the default template when creating a project?

    @ruwantharatnayake5004@ruwantharatnayake50042 ай бұрын
    • Because different companies implement this differently. It is up to you to choose how best to set this up.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Can this implementation be done in separate class and referenced in Program.cs? just to keep things clean

    @user-tq7is7qu8z@user-tq7is7qu8z2 ай бұрын
    • Sure.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • why if using IEndpointFilters to handle the errors is it a bad approach ?

    @othmanetibba@othmanetibba2 ай бұрын
    • 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.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • Why not return "application/problem+json" ?

    @MichaelEpprecht@MichaelEpprecht2 ай бұрын
    • 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.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
  • 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.

    @gonace@gonace2 ай бұрын
    • 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?

      @spacemanjack777@spacemanjack7772 ай бұрын
    • There is no point of using a MVC controller for JSON based API.

      @md.redwanhossain6288@md.redwanhossain62882 ай бұрын
    • 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).

      @IAmTimCorey@IAmTimCorey2 ай бұрын
    • @@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

      @gonace@gonaceАй бұрын
  • Sir I am stuck with navigation in wpf for page to window then multiple windows , and closing of previous page can you guide me

    @tejp33@tejp332 ай бұрын
    • 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.

      @IAmTimCorey@IAmTimCorey2 ай бұрын
    • @@IAmTimCorey ok sir thank you...

      @tejp33@tejp332 ай бұрын
KZhead