Global Exception Handling in Asp.Net Core Web API using IExceptionHandler

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there in this video I'm going to talk about one very important topic in ESP R net Cod throughout my experience in software development I have worked on various ESP R net core applications and one common problem that I noticed in most of the project is the unnecessary use of trach block although it is always a good practice to handle the exception but the way software developers are using the triage Block in the application is not a good practice I have seen some applications where developers are using tratch Block in almost all the action methods luckily the esp. Net Framework and the esp. net core framework both of them were having the solution of this problem since a while and with each release this has gotten even better in the past we were having the exception filter then we were having the middleware and that was a great way to handle the exception globally and by using those approach you can remove all your TR catch block now with net 8 we have a new tool in this framework and it is called as I exception Handler by using this I exception Handler you can handle your exception globally and this is a very great way to handle your exception this I exception Handler is the latest and the straightforward way to handle your exceptions globally in this video I will talk about everything about this I exception filter in esp. net core application over here I have created this esp. net core web API application and this concept is applicable on minimal apis as well this is the default application that I have created over here if I will run this application then you will see we will get that default weather application API this application is working fine on this port and this is our default API now just for the better understanding let's create one more controller over here and we will return return some hardcore data from that controller so I'm going to create one new controller let's say this is controller and this is the API controller let's get a blank one and here let's give a meaningful name for example employees and over here let's create our first method so here I'm creating this public over here I have Creed this very basic method and in this method basically I'm returning a list of integers in your application you might have some different scenario and you will return return a list of all the employees or you will have so many apis based on the need but in this video I'm going to talk about the global exception handling and that concept will be applicable in your entire application it does not matter how many apis you have if you will get exception at any place in your application then that Handler will catch that exception let's run this application and see what is the output on the Swagger here you can notice the application is working fine and if I click on this try it out click on this execute then you will notice that we are getting this proper data my main concern is not about this data my concern is about the exception now what will happen if I will throw one exception over here let's say this is throw new exception and this is let's say it is test exception all right now let's run this application again and see what is the output this time this time you will notice that we are getting this kind of message in the response of this particular API why is this happening this is because in this scenario we are throwing this exception on purpose but there are some runtime exceptions as well there could be some scenarios where you are not aware about the exception but because of the logic of your code the exception will occur in that scenario how will you handle that exception that is something I will talk about in this video okay now let's get one new class over here and let's say this is app exception Handler you can give the name to your class as for your need and as a first step you have to inherit it from the I exception Handler this interface is available under this name space now there is one method inside this interface and we have to implement that let's do that Implement interface this time you will notice that we are getting this method this is try handle async and there are three things over here one is this HTTP context second is this exception and third is this cancellation token this HTTP context will have the information about your current HTTP request and if there is any exception in this request then that information will be stored over here in this exception Now by using these parameters we can handle this exception really well okay now let's put a breako over here and before that we have to remove it and let's just return true from this particular method and because this is returning this value task so let's do that Val task. from result and let's return the true from here and just by writing this very simple code we have created our exception Handler now it is time to tell this application that we have created the exception Handler and you have to use that so let's just copy the name of this Handler go back to this program. CS file and over here first we have to register this dependency we can do that easily by using this Builder dot Services dot add exception Handler and over over here you have to provide the name of your exception Handler like this that's it now we have registered this dependency but we have to use this dependency and how can we do that we have to use one middleware over here let's use the middleware over here so this is app dot use exception Handler and if you know a little bit about this middleware then this is available in the framework since few relases and we are using it to handle the exception even without using this exception Handler now we are done with all the changes we just have to run this application and before that let's put a break point over here perfect let's run this application and see what will happen at this place you will notice that we are getting an exception and as per this exception we have to pass some values to few properties one is this exception handling path or we have to provide the value to this exception Handler we have to set the value of either one of them why we are getting this exception this is because this used exception Handler middleware is there even for this exception Handler and as per the implementation of this middleware we have to pass either the path or we have to provide some configurations but now this could be some improvement in the upcoming versions but as of now you have to pass some default values over here either you can use the error path or the best way is that you can pass the default Lambda like this that's it you don't have to pass anything else and this application will work if you want to pass some configurations in your exception Handler then you can pass all of them over here by using this Lambda expression you can pass the options and by using this Lambda expression you can pass and set the values to those properties okay this time let's run this application and see what will happen and this time you will notice that we are hitting this break point over here in this app exception Handler let's see what we have in this HTTP context in this HTTP context we have the details of this HTTP request so here we are having this is the request that you are having and this is the response so it is 500 internal server error and let's see what we have in this exception so here you will notice that we are getting the message this is test exception and this is something that we are throwing on purpose from our Excel method let's continue and see what is the output on the browser here you will notice that we are getting this kind of output now now what if I want to provide a proper formatted response over here what will happen in that case how can I do that so for that we have to update the response of this particular HTTP request and we can do that easily by using this HTTP context do response dot WR async and let's say or we can use the right Json async you can use any one of them and here I'm writing something went wrong you can also set the status code over here and we can do that easily by using this HTTP context do status code is equals to status code let's say this is 500 internal server error remember this is an async method so we have to use the await keyword and if we are using the await keyword then and we have to just remove this value task from here this will work like this now let's just run this application and see what will be the output let's just copy this URL and paste it over here and this time you will notice that we are getting this message something went wrong this message is coming from this particular place okay now let's say I want to pass some formatted values over here so we can have one more class over here in this application and let's say I'm creating the response let's say it is exception or error response and here I'm creating few properties over here and if you want to pass the exception message then you can also have this property okay now let's go back to our exception Handler and over here let's create the object of this particular class so this is let's say it is where response is equals to new this class and over here we can set all our properties so first property is this status code so we can have the status code for example this this one let's just copy it paste it over here and next is this exception message so it could be like this this exception do message you can also format it as for your need and let's say it is something went wrong like this just put it here and this time I will be using this response over here and let's pass this cancellation token as well now what will happen if I will run this application if I will just copy this URL and paste it in the browser then you will notice that we are getting this exception details over here and this is how you can format your response of the exception and you can pass it as per your need over here you will notice that we are creating this one file by inheriting from this I exception Handler what will happen if I want to handle all the exceptions differently for example if I'm getting not implemented exception then I want to handle it by using a different way or if I'm getting the database exception then I want to handle it in a different way so one way could be that here you can use if else block and you can just check the type of this exception if this exception is let's say of not imp implemented or let's say divide by zero you can basically check all your exceptions and you can create all your blocks the another way could be that you can create one more file for your exception handling for the specific one and you can handle your exception in that particular new file let's try that so over here I'm creating one more file so let's say it is ADD and class this is app exception Handler note implemented let's say like this and again by using the same approach we have to use this one implement this method like this let's just copy the entire code from here and paste it over there like this now we will make some change over here so first change would be if this exception is not note implemented exception and after checking it let's put this code over here otherwise I will return return the false like this okay now over here I will check that if the exception is not implemented exception then I will just do this thing and return false otherwise this time let's say I'm using this 501 let's say it is 501 the major change that you will notice over here is returning this true and false if something is handled by this Handler then we return true otherwise we will return false similarly if the exception is handled by this file then we are returning true otherwise we are returning false this concept is called as chaining of the exception Handler now to use it we have to just use it in the program. Cs file just like this one now what will happen based on the way that you are registering them over here first this file will be called and if the exception is handled in this particular file then everything is fine otherwise this new file file will be called and if you are having the code over there then that code will be executed and if you are having more files over here then all of them will be executed in the same order that you are registering them you do not have to make any change over here in this use exception Handler but the order of the registration of this exception Handler matters now in the employee controller I will create one more method let's say it is get by ID here I'm passing only this ID hardcoded I'm not interested in the routing and other concept I'm focusing only on the exception part so I'm throwing one more exception over here throw new note implemented and let's say here I'm passing this is not implemented all right now let's run this application and put some break points in our handlers so here we are this is our note implemented Handler let's put a break point over here and another one in this main one I will be using this time this one try it out click on this execute and you will notice that first we are hitting the break point in this app exception Handler okay let's just click on this continue button and this time you will notice that we are over here inside this app not implemented exception Handler why we are coming over here this is because over here we have put a check if the exception is not not implemented exception then only it must be handled over here otherwise we are returning false this one and if we are returning the false over here then this application will handle all other handlers and then all your middle bears and you will notice that we are getting the message this method is not implemented this is how you can handle all your different exceptions in the ASP net core application if you using loging in your application then it is also very easy to use with this Handler so let's say for example if I talk about this first one here I want to use the I loger so I can can use this IL loger and over here I can just pass the name of this class like this and let's say I'm using the loger over here and by using this loger I can log my exception so for example if I'm logging it over here so let's say it is loger do log error and here I can basically pass any message or you can pass the message let's say it is like this exception exception do message like this the logging of the error is up to you you can handle it as per your need but this is how you can handle the exception in the ASP netcore application I hope the concept is clear if the video was helpful please like it and subscribe to the channel thank you for watching have a great day
Info
Channel: Nitish Kaushik
Views: 2,756
Rating: undefined out of 5
Keywords: iexceptionhandler, exceptionhandler, global exception handling, global exception handling in asp.net core web api, global exception handling in c#, global exception handling in asp.net core, global exception handling in c# mvc, global exception handling in asp.net core mvc, global exception handling in microservices, iexceptionhandler in asp.net core, iexceptionhandler in asp.net, iexceptionhandler in asp.net core mvc
Id: bEYlNuwTSms
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Thu Jan 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.