Global Error Handling in .NET 8 - My Favorite Approach

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024
  • All videos in this playlist: • ASP.NET 8 REST API Tut...
    In today's video, I'll show you my favorite way to handle exceptions globally in my ASP.NET applications.
    Global error handling is crucial in any backend application and is important to get right. Luckily, .NET comes with some really handle tools right out of the (ASP.NET) box.
    Get early access to videos: / @amantinband
    Join us on discord: / amantinband
    Get the source code: / amantinband
    Support the channel: / amantinband
    Connect with me on 'em socials:
    Twitter: / amantinband
    LinkedIn: / amantinband
    GitHub: github.com/ama...
    My industry-level courses:
    Zero to Hero - Domain-Driven Design: dometrain.com/...
    Zero to Hero - Clean Architecture in .NET: dometrain.com/...
    All my courses: dometrain.com/...
    Thanks for watching, don't forget to like & comment & subscribe! ❤️ 💻

Комментарии • 26

  • @ahmedshahjr
    @ahmedshahjr Месяц назад +2

    Thanks for the amazing demo Amichai Mantinband

  • @Eirenarch
    @Eirenarch Месяц назад +13

    I don't understand the value of separate endpoint. Why not have a middleware that catches exceptions discriminates by their types, builds the appropriate ProblemDetails and writes it to the response

    • @amantinband
      @amantinband  Месяц назад +4

      Both are solid choices. It mostly comes down to preference

    • @Dustyy01
      @Dustyy01 Месяц назад +4

      There is no benefit in using the route approach like this. Normally you handle the exception using the IExceptionHandler interface and if you return false in the TryHandleError method, the route will receive the exception to further process (e.g. show a nice error html page). If you return true your exception is handled.
      I dont know any other reason why you want to use the route instead of the Interface in a plain API

    • @Fikusiklol
      @Fikusiklol Месяц назад

      @@Dustyy01 I agree aswell. If you want granular exception handling - IExceptionHandler is the way to go.
      However for easy/small services both approaches mentioned above are just fine.

  • @Buutyful
    @Buutyful Месяц назад

    perfect video 10/10

  • @10Totti
    @10Totti Месяц назад +2

    Great tutorial, but why not use a new IExceptionHandler abstraction for managing exceptions ?

    • @amantinband
      @amantinband  Месяц назад +2

      I would choose IExceptionHandler over this approach for larger applications that need the modularity. For smaller apps, I personally prefer this approach

  • @felipecosta9889
    @felipecosta9889 Месяц назад +2

    Great content. One question. Is it best practice to throw exceptions in my code and catch them with global error handling? Or is it better to throw only exceptions that cannot be predicted?

    • @amantinband
      @amantinband  Месяц назад +1

      This is exactly next Monday’s video

  • @turcanuioangeorge4750
    @turcanuioangeorge4750 Месяц назад +1

    What is the tool you used to highlight code? Or is it just done in post production

  • @afgone123456
    @afgone123456 Месяц назад

    So if a client comes back with a traceid how would a developer find the real issue for that request? Are you saying we need to log it to db or something using the error endpoint?

  • @xybersurfer
    @xybersurfer Месяц назад

    any tips on how to do global error handling in Blazor? ErrorBoundary doesn't seem to catch everything

  • @obinnaokafor6252
    @obinnaokafor6252 Месяц назад +1

    What icon plugin are you using in your VS Code?

  • @solvestation
    @solvestation Месяц назад +1

    Great tutorial.
    I would like to ask if the implementation is usable in a .net 8 MVC project?
    Thank you @Amichai

  • @SerafimMakris
    @SerafimMakris Месяц назад +1

    Good job. clean code as usual. i Hate you :D :D :D
    Ty @amantinband

  • @MartinoBordin
    @MartinoBordin Месяц назад

    Can we use it also for grpc services?

  • @harrisonwell1719
    @harrisonwell1719 Месяц назад

    why you don't respond to questions in the comments?

    • @AslamNazeerShaikh
      @AslamNazeerShaikh Месяц назад +1

      Not sure if this is 100% true or not ?! 🤔

    • @amantinband
      @amantinband  Месяц назад +1

      I can’t always keep up with all the comments.. sorry if I missed your previous message 🙏🏼