1 00:00:01,170 --> 00:00:05,560 All right let's start to build out this additional functionality inside of our area handling middleware. 2 00:00:05,850 --> 00:00:11,010 So to get started I'm going to find my error handling middleware inside the middleware as directory. 3 00:00:11,150 --> 00:00:13,880 It is right here at the very top. 4 00:00:13,890 --> 00:00:19,050 I'm going to import those two custom air subclasses we just put together once again rather than writing 5 00:00:19,050 --> 00:00:22,740 these out to actually let's just do a copy paste from these sign up root handler. 6 00:00:22,750 --> 00:00:25,060 We're just working on the here sign up dot. 7 00:00:25,070 --> 00:00:28,900 Yes we want these to exact import statements. 8 00:00:28,920 --> 00:00:35,610 So I got a copy both them and then paste them inside of the air handler. 9 00:00:35,620 --> 00:00:42,580 So now at the very top I'm going to delete that console log and we'll add in our little tight checks. 10 00:00:42,640 --> 00:00:54,000 So we're going to say if air is an instance of request validation error then for right now let's just 11 00:00:54,000 --> 00:01:04,570 do a console log and say something like handling this error as a request validation error. 12 00:01:04,650 --> 00:01:06,410 We will also do a return. 13 00:01:06,570 --> 00:01:08,520 Actually we'll leave that return off for just a second. 14 00:01:08,520 --> 00:01:13,390 Let's just make sure we can get these instance of checks work appropriately after that we'll say if 15 00:01:13,390 --> 00:01:26,140 there is an instance of database connection error we'll do a console log of handling this error as a 16 00:01:26,620 --> 00:01:28,150 DV connection error 17 00:01:31,390 --> 00:01:35,590 we'll save this and then let's go ahead and do a quick test over with post man. 18 00:01:35,680 --> 00:01:39,490 So all we're trying to do right now is to make sure that we can interpret the type of that incoming 19 00:01:39,490 --> 00:01:42,700 error so we can eventually handle it appropriately. 20 00:01:42,700 --> 00:01:45,130 So either we'll take the reasons property and send it back. 21 00:01:45,220 --> 00:01:48,420 We'll take that reason property and send it over whatever is most appropriate. 22 00:01:48,460 --> 00:01:53,300 We still need to figure that part out but to test this out we got to open up. 23 00:01:53,370 --> 00:02:00,720 Once again I'll try to handle the invalid request validate or the request a validation error first. 24 00:02:00,760 --> 00:02:05,440 It's going to put in an invalid e-mail and then send this off. 25 00:02:05,490 --> 00:02:10,070 So I've got a status 400 I don't have a message or I've an empty string right there which is no Cape 26 00:02:10,080 --> 00:02:10,800 right now. 27 00:02:10,800 --> 00:02:15,830 More importantly let's go back over to our terminal and take a look at what was console logged out. 28 00:02:15,950 --> 00:02:20,390 So at my terminal I'll see handling this area as request validation error. 29 00:02:20,520 --> 00:02:21,060 Awesome. 30 00:02:21,070 --> 00:02:27,120 So that means that we can tell our error handling middleware how to run some custom logic depending 31 00:02:27,120 --> 00:02:30,980 upon the type of error that it is receiving. 32 00:02:31,210 --> 00:02:35,590 Now let's do a similar test and make sure that it can recognize a database connection error coming in 33 00:02:37,610 --> 00:02:45,880 I'm going to put in a valid e-mail now send it I'll go back over to my terminal again and I see handling 34 00:02:45,880 --> 00:02:48,070 this area as a db connection error. 35 00:02:48,430 --> 00:02:50,980 Awesome. 36 00:02:51,140 --> 00:02:57,550 Ok so now that our error handling middleware can determine what kind of error this is we can add some 37 00:02:57,550 --> 00:03:02,920 code inside there to take off either that reasons property or the reason property in the case of database 38 00:03:02,920 --> 00:03:06,910 connection error and then build up some kind of response and send it back over to the user. 39 00:03:07,330 --> 00:03:09,830 So let's take care of that last step in just a moment.