Global Error Handling in C# Minimal APIs

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 มี.ค. 2024
  • 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/

ความคิดเห็น • 48

  • @dasfahrer8187
    @dasfahrer8187 3 หลายเดือนก่อน +3

    Man, this is so much nicer than rolling your own GEH.

  • @DeliberateGeek
    @DeliberateGeek 3 หลายเดือนก่อน +5

    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  3 หลายเดือนก่อน +2

      Correct, that would be ideal.

  • @andergarcia1115
    @andergarcia1115 3 หลายเดือนก่อน +3

    Master, thank you for your valuable content and consistent work. The best, as always!

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      Thank you!

  • @xelit3
    @xelit3 3 หลายเดือนก่อน

    Amazing video and simple example, thanks Tim!

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      You are welcome.

  • @ishaakmall2390
    @ishaakmall2390 3 หลายเดือนก่อน

    This was so simple. Thanks for such a clear cut video.

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน +1

      You are welcome.

  • @manishrawat1079
    @manishrawat1079 3 หลายเดือนก่อน

    Seems very useful, thanks!

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      You are welcome.

  • @miles6875
    @miles6875 3 หลายเดือนก่อน

    Love your work

  • @nillname9507
    @nillname9507 3 หลายเดือนก่อน +3

    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  3 หลายเดือนก่อน +3

      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 3 หลายเดือนก่อน

    Thanks Tim!

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      You are welcome.

  • @user-tq7is7qu8z
    @user-tq7is7qu8z 3 หลายเดือนก่อน

    Thanks Tim...

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน +1

      You are welcome.

  • @ruwantharatnayake5004
    @ruwantharatnayake5004 3 หลายเดือนก่อน

    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  3 หลายเดือนก่อน +1

      Because different companies implement this differently. It is up to you to choose how best to set this up.

  • @broadshare
    @broadshare 3 หลายเดือนก่อน

    Interesting stuff

  • @_miranHorvat
    @_miranHorvat 3 หลายเดือนก่อน

    Would be nice to see how to implement the same pattern with gRPC and JsonTranscoding.

  • @robwalker4653
    @robwalker4653 หลายเดือนก่อน

    Question I hope you can answer here Tim. With minimal APIs, if you use auto routing for parameters. i.e. app.MapGet("/book/{id}", (int id) =>
    If you don't send an integer, this will result in a bad request but with no context and the method in the video will not catch this. Although if you run in development mode to show exceptions with trace, it does show. And you can't do any handling within the body because it's being caught before it even gets there.
    How can you give basic context back to the request, to say that it was expecting an int?
    I've found some methods that allow me to output some data, and I can use addproblemdetails to try add the exception message for those types of exception, but the exception is null in the problemdetails context.

    • @IAmTimCorey
      @IAmTimCorey  หลายเดือนก่อน

      I'm going off of my memory here, so I may be incorrect, but I believe the issue here is if you don't pass an int, you aren't actually calling that route. There's no exception inside of that route because that route wasn't called. It would either call just the /book route if you left the int off or it would call whatever route you specified if you included something besides an int like /book/test/. Either way, that would be handled at the routing level rather than the particular endpoint.

  • @JorgeSaucedo-bv8gu
    @JorgeSaucedo-bv8gu 3 หลายเดือนก่อน

    How did you get the colored braces? They look so cool, I want them.

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      Here you go: th-cam.com/video/td81h--afxM/w-d-xo.htmlsi=n6bbvSJ8c37qL5jc&t=1504

  • @TheDogfighter13
    @TheDogfighter13 3 หลายเดือนก่อน

    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  3 หลายเดือนก่อน

      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/

  • @othmanetibba
    @othmanetibba 3 หลายเดือนก่อน

    why if using IEndpointFilters to handle the errors is it a bad approach ?

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน +1

      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.

  • @user-tq7is7qu8z
    @user-tq7is7qu8z 3 หลายเดือนก่อน

    Can this implementation be done in separate class and referenced in Program.cs? just to keep things clean

  • @MichaelEpprecht
    @MichaelEpprecht 3 หลายเดือนก่อน

    Why not return "application/problem+json" ?

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน

      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 3 หลายเดือนก่อน

    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.

    • @codecomposer88
      @codecomposer88 3 หลายเดือนก่อน +1

      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 3 หลายเดือนก่อน

      There is no point of using a MVC controller for JSON based API.

    • @IAmTimCorey
      @IAmTimCorey  3 หลายเดือนก่อน +3

      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 3 หลายเดือนก่อน

      @@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 3 หลายเดือนก่อน +1

    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  3 หลายเดือนก่อน +2

      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 3 หลายเดือนก่อน

      @@IAmTimCorey ok sir thank you...