1 00:00:01,140 --> 00:00:01,440 All right. 2 00:00:01,470 --> 00:00:02,070 Let's get to it. 3 00:00:02,070 --> 00:00:05,880 We're going to start to build out these subclasses of request validation error and database connection 4 00:00:05,910 --> 00:00:07,980 error as we are building into two classes. 5 00:00:07,990 --> 00:00:10,140 There's going to be a little bit of strange code. 6 00:00:10,230 --> 00:00:14,580 We're going to mostly just kind of blaze through these two implementations because as soon as we start 7 00:00:14,580 --> 00:00:18,420 using them you are going to understand what is going on as we're building them. 8 00:00:18,430 --> 00:00:20,070 You're going to say wait what. 9 00:00:20,100 --> 00:00:23,190 But don't worry as soon as we use them it's going to be really clear. 10 00:00:23,190 --> 00:00:23,450 OK. 11 00:00:23,500 --> 00:00:24,660 Let's get to it. 12 00:00:24,870 --> 00:00:29,160 Back inside my editor are going to find my SRT directory and inside there are gonna make a new folder 13 00:00:29,160 --> 00:00:35,370 called ears inside this directory we're gonna write out all these different custom subclasses of air 14 00:00:36,340 --> 00:00:41,590 so the first one I'm going to make is request validation error Dot T S 15 00:00:46,020 --> 00:00:55,390 then at the very top I'm going to first import validation air from Express dash validator validation 16 00:00:55,390 --> 00:01:03,460 error is a type it is describing the type that comes back whenever we do a validation attempt using 17 00:01:03,460 --> 00:01:04,750 express validator. 18 00:01:04,920 --> 00:01:07,960 So it's essentially a type that refers to this kind of object right here. 19 00:01:07,960 --> 00:01:13,120 Something has a message a param also technically a field and one of the property I can't remember off 20 00:01:13,120 --> 00:01:14,470 the top my head. 21 00:01:14,470 --> 00:01:19,360 The reason that we are importing this is that when we start to build a subclass at some point time we're 22 00:01:19,360 --> 00:01:24,460 going to have to receive a list of validation errors like in other words the actual reasons that the 23 00:01:24,460 --> 00:01:30,100 request failed validation so we are importing this type to make sure that we can describe the requirements 24 00:01:30,460 --> 00:01:34,710 or the so we're going to be assigned to our subclass after that. 25 00:01:34,720 --> 00:01:45,300 I'm going to export a new class called request validation error and we're going to extend. 26 00:01:45,440 --> 00:01:50,950 So again we are building our own custom implementation of an error been inside of here going to define 27 00:01:51,020 --> 00:01:59,220 the constructor function I'm going to say that this is going to need to be called with a list of validation 28 00:01:59,220 --> 00:01:59,670 errors. 29 00:01:59,730 --> 00:02:05,300 So again that is why we import this type at the very top so I to write out right here private errors 30 00:02:05,960 --> 00:02:07,880 is going to be validation error. 31 00:02:08,010 --> 00:02:11,750 Right let's break this down really quickly. 32 00:02:11,750 --> 00:02:16,730 I'm using the keyword private right here because we want to take this first property and assign it as 33 00:02:16,730 --> 00:02:18,980 a property to the overall class. 34 00:02:18,980 --> 00:02:23,810 So writing out the word private right there is 100 percent equivalent to writing this dot errors equal 35 00:02:23,870 --> 00:02:32,500 errors and then also defining this property ahead of time though something like validation error. 36 00:02:32,540 --> 00:02:43,950 So what you see right here is a hundred percent equivalent to just writing out private errors. 37 00:02:43,970 --> 00:02:49,100 Now I still have an error around the constructor as because we are extending a base class whenever we 38 00:02:49,100 --> 00:02:54,050 do so and we defined a constructor we have to call super to invoke the constructor inside the base class 39 00:02:54,050 --> 00:02:55,900 which in this case is air. 40 00:02:55,940 --> 00:02:57,320 So I will call super to fix that 41 00:03:01,740 --> 00:03:03,570 now one word very small thing. 42 00:03:03,570 --> 00:03:06,120 This is a very small technicality. 43 00:03:06,120 --> 00:03:11,740 It only has to be done when we are writing TypeScript and trying to extend a class that is built in 44 00:03:11,740 --> 00:03:12,800 to language. 45 00:03:12,930 --> 00:03:17,160 So we are trying to extend it right here which is built into the language to do so. 46 00:03:17,160 --> 00:03:19,620 We have to write out one additional line of code right here. 47 00:03:19,650 --> 00:03:25,530 I'm going to put it a little quick comment that says only because we are extending a built in class 48 00:03:26,070 --> 00:03:33,420 we will write out the object that set prototype of and then this and request validation error. 49 00:03:33,420 --> 00:03:40,450 That prototype but this is just some behind the scenes stuff that gets our class to work correctly. 50 00:03:40,460 --> 00:03:45,190 Again just because we are extending a built in class OK. 51 00:03:45,220 --> 00:03:47,320 So now how would we use this class right here. 52 00:03:47,320 --> 00:03:53,410 Well we would say something like new request validation error and then we would pass in the list of 53 00:03:53,440 --> 00:03:58,290 errors like so we would probably want to throw this as well because it is intended to be an error. 54 00:03:58,630 --> 00:04:03,850 And then this thing this instance that we are creating and throwing again should be communicated automatically 55 00:04:03,850 --> 00:04:10,390 down so that's like that error right there it should be communicated down into our error handling middleware 56 00:04:12,570 --> 00:04:14,730 now there is one other quick change I'm going to make here. 57 00:04:14,730 --> 00:04:17,670 I'm actually gonna update this keyword of private over to public. 58 00:04:17,670 --> 00:04:20,340 We eventually are going to reverted back to private. 59 00:04:20,340 --> 00:04:23,680 I was reading my notes here looking at the final implementation. 60 00:04:23,730 --> 00:04:25,510 We're gonna leave it as public for just a moment. 61 00:04:25,570 --> 00:04:29,350 We were gonna flip that back over to private and just a little bit OK. 62 00:04:29,380 --> 00:04:35,150 So we've now got our first custom air put together let's build our other custom air out which is going 63 00:04:35,150 --> 00:04:39,860 to be called database connection air that's gonna be very similar in nature but it's gonna have this 64 00:04:39,920 --> 00:04:44,390 reason property that's gonna be a string instead of reasons that's going to be an array of validation 65 00:04:44,420 --> 00:04:53,550 errors inside of my ears folder I'm going to create another file called database connection air dot 66 00:04:53,600 --> 00:04:58,320 T.S. and then inside of here we're going to do something very similar. 67 00:04:58,360 --> 00:05:09,160 So we will export a new class called database connection air stands air or sea not super but instructor 68 00:05:10,120 --> 00:05:13,500 inside the constructor we have to call super. 69 00:05:13,640 --> 00:05:17,780 We also have to do that same kind of little initialization line because we are extending a built in 70 00:05:17,780 --> 00:05:18,690 class. 71 00:05:18,800 --> 00:05:31,330 Will do object set prototype of this and database connection air dot prototype and then in this case 72 00:05:31,330 --> 00:05:34,510 I'm going to hard code in the reason that we are throwing this error. 73 00:05:34,630 --> 00:05:41,560 So I will put in a fixed reason property right here that says something like air connecting to database 74 00:05:45,210 --> 00:05:50,640 now in this case do we actually have to extend air or something like database connection air over really 75 00:05:50,820 --> 00:05:55,290 trying to communicate in this case is a fixed string so perhaps it doesn't make a lot of sense to make 76 00:05:55,290 --> 00:05:55,980 a base class. 77 00:05:55,980 --> 00:05:57,770 I just wanted to give you kind of a counterpoint. 78 00:05:57,780 --> 00:06:02,920 I want to give you a second subclass to create beyond just request validation error. 79 00:06:03,120 --> 00:06:06,560 So we're going to be mostly just focusing on request validation error for right now. 80 00:06:06,570 --> 00:06:10,650 Again I just want to give you some other area that we could possibly create that might make sense in 81 00:06:10,650 --> 00:06:12,880 an application like this. 82 00:06:13,050 --> 00:06:17,830 OK so now you put these two air subclasses together we're going to import them into the request handler 83 00:06:18,100 --> 00:06:22,300 and then anytime something goes wrong inside there we're going to throw either request validation error 84 00:06:22,390 --> 00:06:27,900 or database connection error so I have to go back over to my sign up root handler 85 00:06:31,090 --> 00:06:34,320 and then at the top I'm going to import those two custom subclasses. 86 00:06:34,330 --> 00:06:39,940 We just put together so request validation error 87 00:06:45,300 --> 00:06:48,400 and database and auctioneer 88 00:06:55,160 --> 00:07:00,940 now we're going to make sure that then rather than throwing a very generic error we will instead throw 89 00:07:00,940 --> 00:07:07,590 it in the instance of request validation or database connection down inside of my implementation I'm 90 00:07:07,590 --> 00:07:11,490 going to find where we check to see if there were any errors during validation going to delete everything 91 00:07:11,490 --> 00:07:21,380 inside their and replace it with row new request validation error and I'll pass in our list of errors 92 00:07:21,470 --> 00:07:29,170 so errors dot array like so I'll then do something very similar for the other error right here rather 93 00:07:29,170 --> 00:07:33,040 than throwing a very generic error that doesn't really tell us a lot about what is going on. 94 00:07:33,040 --> 00:07:37,210 Instead throw a new database connection air 95 00:07:42,860 --> 00:07:50,590 OK so that is step one we have now subclass error created these two very custom errors we are throwing 96 00:07:50,590 --> 00:07:54,850 them out of our request handler they're going to eventually show up inside of our area handling middleware 97 00:07:54,880 --> 00:07:59,350 and so now the last thing we have to do is add in some code to inspect the incoming error and check 98 00:07:59,350 --> 00:08:04,300 to see if it is an instance of request validation error or an instance of database validation error 99 00:08:04,840 --> 00:08:10,840 and if it is we can build up our response and send back to the user appropriately so quick pause right 100 00:08:10,840 --> 00:08:14,890 here we're gonna build this last up out in just a moment and all this stuff is going to start to come 101 00:08:14,890 --> 00:08:16,450 together very very quickly.