1 00:00:00,600 --> 00:00:04,210 In the last video we establish that we need to communicate more information from the request handler 2 00:00:04,210 --> 00:00:09,190 to the air handler middleware through this air object but because we are using typescript we can not 3 00:00:09,190 --> 00:00:13,060 just arbitrarily add in additional properties to the air object itself. 4 00:00:13,560 --> 00:00:17,770 So in this video we're going to figure out how to solve this issue with a very typescript style approach. 5 00:00:18,310 --> 00:00:21,010 So here's really the issue here here's what we want to do. 6 00:00:21,040 --> 00:00:26,000 Summarized in single sentence we really want an object like an air. 7 00:00:26,110 --> 00:00:27,780 We really want an air object essentially. 8 00:00:27,780 --> 00:00:31,540 Well you want to add in some more custom properties to it. 9 00:00:31,660 --> 00:00:33,850 That's we really want to do right now. 10 00:00:33,910 --> 00:00:38,470 So any time that you say that you want something like X but you want to add in some more custom properties 11 00:00:38,650 --> 00:00:42,100 that is usually a sign that you want to subclass something. 12 00:00:42,100 --> 00:00:44,650 And that's exactly what you and I are going to do. 13 00:00:44,920 --> 00:00:47,690 We are going to take the built in air object. 14 00:00:47,750 --> 00:00:51,580 We are going to subclass it into two separate subclasses. 15 00:00:51,630 --> 00:00:58,450 We're gonna call one request validation error and the second database connection air request validation 16 00:00:58,450 --> 00:01:03,340 error is going to be a new type of air object that we're going to throw anytime that there is an error 17 00:01:03,550 --> 00:01:09,630 with the incoming data on that incoming request then anytime that anything goes wrong with a database 18 00:01:09,630 --> 00:01:12,910 connection we'll instead throw a database connection error. 19 00:01:13,020 --> 00:01:16,060 So that would be like this right here. 20 00:01:16,220 --> 00:01:20,330 The reason that we are making both these subclasses is that when we create the subclass we can add in 21 00:01:20,330 --> 00:01:25,630 some additional custom properties to it that will further clarify the reason for the error. 22 00:01:25,640 --> 00:01:32,470 Again we are trying to use these custom subclasses to communicate much more information from the request 23 00:01:32,470 --> 00:01:37,130 handler down to that area handling middleware now at this point. 24 00:01:37,140 --> 00:01:38,660 This might seem pretty crazy. 25 00:01:38,670 --> 00:01:44,160 Let me show you a diagram to help you understand the entire big picture of what is going on here. 26 00:01:44,180 --> 00:01:44,430 OK. 27 00:01:44,430 --> 00:01:45,980 So kind of complicated diagram. 28 00:01:46,020 --> 00:01:51,510 We'll go through it step by step they're going to first start off up here inside of our request handler. 29 00:01:51,510 --> 00:01:53,910 You and I are going to create two new subclasses. 30 00:01:53,910 --> 00:01:58,380 One is going to be called request validation error and another that is going to be called database connection 31 00:01:58,410 --> 00:02:01,710 error because we are building these classes ourselves. 32 00:02:01,710 --> 00:02:04,730 We can add in as many custom properties as we wish. 33 00:02:04,740 --> 00:02:10,860 So for example on the request validation error subclass we can assign to it a new property called reasons 34 00:02:11,180 --> 00:02:17,760 and that could be an array of objects that has exactly why the request validation failed then over on 35 00:02:17,760 --> 00:02:23,640 this other subclass we can say that it needs to have a message property or maybe a reason property we 36 00:02:23,640 --> 00:02:28,440 can call it whatever we want and it could be a simple string that says something like failed to connect 37 00:02:28,440 --> 00:02:36,650 to database or request handler at some point time is going to throw either this or this to be precise 38 00:02:36,670 --> 00:02:41,540 it's going to throw an instance of request validation error or an instance of database connection error 39 00:02:42,620 --> 00:02:47,510 so that error either this one or this one is going to be thrown and it's eventually going to flow on 40 00:02:47,510 --> 00:02:53,560 down to our error handling middleware then instead of our era handling middleware we're going to set 41 00:02:53,560 --> 00:02:57,810 up some code inside there to inspect the area that it is receiving. 42 00:02:58,020 --> 00:03:02,280 So we're going to check inside the error handling middleware and we're going to say OK I see an error 43 00:03:02,280 --> 00:03:03,120 is coming. 44 00:03:03,120 --> 00:03:06,940 Is it an instance of the request validation error class. 45 00:03:07,260 --> 00:03:09,370 If it is then that means great. 46 00:03:09,390 --> 00:03:12,010 We have an instance of request validation error. 47 00:03:12,060 --> 00:03:17,250 Let's take that reason's property though this property right here and we're going to use that to build 48 00:03:17,280 --> 00:03:24,670 a response to send back to the user if the incoming error is not a request validation error then we 49 00:03:24,670 --> 00:03:31,880 will check to see if it is an instance of database connection error if it is then great let's take that 50 00:03:31,880 --> 00:03:37,670 reason a property and we'll send it to the user and if we fail both these checks then we don't know 51 00:03:37,700 --> 00:03:39,900 what kind of error this is it is a generic error. 52 00:03:39,920 --> 00:03:45,450 And so in that scenario we should probably send some kind of generic error message back to the user. 53 00:03:45,470 --> 00:03:47,250 So this is it this is the entire flow. 54 00:03:47,810 --> 00:03:52,070 We're going to build out these subclasses to describe all the different things that can go wrong whenever 55 00:03:52,070 --> 00:03:53,720 we throw one of these errors. 56 00:03:53,960 --> 00:03:56,150 It's going to go into the area handling middleware. 57 00:03:56,150 --> 00:03:59,960 We're going to try to figure out that type of error that it is and then we could build up some appropriate 58 00:03:59,960 --> 00:04:02,020 response. 59 00:04:02,030 --> 00:04:06,860 Now what I'm showing here on this diagram and specifically adding these properties right here is still 60 00:04:06,860 --> 00:04:08,650 kind of a simplified version. 61 00:04:08,840 --> 00:04:13,130 After we build an initial implementation of this we're going to come back and refine this even more 62 00:04:13,340 --> 00:04:16,820 to add in way more automatic aspects to this. 63 00:04:16,820 --> 00:04:20,600 I know in the last couple of videos I've said Hey just hold on please be patient. 64 00:04:20,600 --> 00:04:25,160 We're almost at the point now where you're going to see that this really is an incredible error handling 65 00:04:25,190 --> 00:04:26,030 approach. 66 00:04:26,030 --> 00:04:29,720 So just a little bit more patients are going to start to write out this implementation in the next video 67 00:04:29,930 --> 00:04:33,560 and you're really going to start to see all this stuff start to come together so quick pause and I'll 68 00:04:33,560 --> 00:04:34,790 see you in just a minute.