Global Error Handling in ASP.NET Core Web API (updated video link in the description)

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


The exception-handling features help us deal with the unforeseen errors that could appear in our code. Let's learn how to implement it.

FOLLOW US ON SOCIAL MEDIA!

VIDEO SUMMARY:
Even though there is nothing wrong with the try-catch blocks in our Actions in Web API project, we can extract all the exception-handling logic into a single centralized place.

By doing that, we make our actions more readable and the error-handling process more maintainable. If we want to make our actions even more readable and maintainable, we can implement Action Filters.

We won’t talk about action filters in this article but we strongly recommend reading our post Action Filters in .NET Core.

In this video, we are going to handle errors by using a try-catch block first and then rewrite our code by using built-in middleware and our custom middleware for global error handling to demonstrate the benefits of this approach.
Рекомендации по теме
Комментарии
Автор

Thank you all for watching and for your support.

CodeMaze
Автор

Best explanation I have gotten on Exception handling.
Thanks a bunch.

Aweklin
Автор

you are one of the good teams there thanks for your helpful articles

mahmoudalaskalany
Автор

Very good and nice video. Thank you.

ತುಂಬಾನೇ ಉಪಯುಕ್ತವಾಗಿದೆ.

elqmpbh
Автор

Congratulations on the video! really enjoyed!

Would it be possible to make each log message customizable?
I have several controllers, and each one has a specific error message. Is it possible to specify each log with an error message for each controller?

wellingtondiasneves
Автор

Hi Code Maze, thanks for the tutorial. I have only one question.
In the if(env.IsDevelopment()) we have defined
You register the below the if statement. Will this override the app.UseDeveloperExceptionPage logic?
What would be different if the was defined in the else statement like this *if(env.IsDevelopment()) else

lukabirtasevic
Автор

Thanks for the nice video. I think, The example will only work for physical web/app server. It would have been great if you could have covered How to set it up for azure /aws cloud platform.

gleamofcolor
Автор

This is amazing, thank you for sharing, but i have a question about middleware an dependency injection, are they not compatible?

despovizcaino
Автор

Hello CodeMaze, Thanks for share your knowledge. I understand everithing that you did in this video, but what if i want to have the why, the reason for the internal error, something like: when from my frontend i send something bad and my database throw that exception, how can i send the response, handle the error and alert my user, no only something wrong happened, but this is the specific error. If you can illustrated me, i would thank you so much!

davidmontes
Автор

Hi, thanks for the video. Middleware always return 500 error, what if you have or raise a 4xx or 5xx errors in controller or main logic code?

millisecondhmd
Автор

Is there a reason to not have any Explicit logging? ie Log.("something happened") and just capture different levels from the stack trace?

freeborn
Автор

How does your code know your endpoint is api/values if you do not have route on top of the method/endpoint?? Also I'm working on ASP.NET Core web api and in postman I remove the content-type header to test to see if I recieve a repsonse without it I want to generate my own error, but from looking at this video it seems that your endpoint always gets hit, while when I remove content-type from postman my endpoint does not get invoked only the constructor. then right after I get the generic problem details response. with type, tiitle, rfc link, etc. I'm going to try this out and let you know how it goes thanks for this video

shadowthehedgehog
Автор

How can we Include Central/Global exceptions in the Controller Unit test case with xUnit?
As we have to test status codes in the controller if we implemented global exception, then it is not unit testable.

venkat
Автор

Hi, great tutorial but I have one question. How would we handle the cases where we would like to implement a specific exception handling logic depending on where the exception has been thrown or depending on the type of exception? This solution seems great but I might need to implement a more specific logic to recover from errors in the future and I'm not sure how it can work with this approach?

denizarca
Автор

Is this approach forcing us to get rid of Ok(), BadRequest() and other ObjectResult returnin methods?
Should I just throw exceptions now?

Hantick
Автор

Thanks for your nice videos but only thing which needs to be improve is zooming content some places it's not clear to see.

shaikhakhtar
Автор

Thanks, but for exception like 400 404 403 what shall we do, do we catch on global handler or catch separetely?

galaxiegalaxie
Автор

your project started with LoggerSrevice ? not explained about it. 4:23 ILoggerManager, any idea about these. I am new to dotnet, anyone help Is appreciated. Thank you.

ivtechnet
Автор

Hi good video. But couple of questions.
When creating custom Middleware
1) why not use IMiddleWare interface
And
2) Why extend the Middleware why not just call the directly in startup class?

mayurmanudhanya
Автор

Hi Code Maze Team,
Thanks for Sharing your knowledge, This will help us.
Please make a video for JWT refresh token AuthN/AuthZ using Asp.net core web api.
I have implement authentication part but authorization is remains, Please help me for the understanding the same.
Thanks in advance.

ashishkumarsahu