1 00:00:01,260 --> 00:00:05,700 Now that we've wrapped up all this air stuff I figure why not do some more air handling stuff. 2 00:00:06,390 --> 00:00:06,610 Yeah. 3 00:00:06,630 --> 00:00:08,190 Sorry I am serious. 4 00:00:08,190 --> 00:00:12,320 I want to go very quickly through the process of creating a new type of area in our application is going 5 00:00:12,310 --> 00:00:15,930 to be really simple and straightforward and you're going to see how easy it is to add into new area 6 00:00:15,930 --> 00:00:17,220 handling stuff. 7 00:00:17,220 --> 00:00:21,690 So I want to try to add in a new air to handle the case in which a user goes through a route that does 8 00:00:21,690 --> 00:00:23,280 not exist. 9 00:00:23,340 --> 00:00:24,980 So here's what we're going to do. 10 00:00:25,120 --> 00:00:27,980 We're gonna first again by finding our ears directory inside there. 11 00:00:27,990 --> 00:00:35,500 I'll make a new file called not bound Air dot t yes so this will be an area that we want to yield to 12 00:00:35,500 --> 00:00:37,350 throw at some point in time. 13 00:00:37,360 --> 00:00:42,430 Anytime a user tries to navigate to some route that doesn't exist any time we want to make a new error 14 00:00:42,640 --> 00:00:45,990 at the very top we will import base air or see me. 15 00:00:45,990 --> 00:00:46,540 Customer 16 00:00:49,560 --> 00:00:56,020 rom custom air will then export a new class named after the air we're trying to make. 17 00:00:56,050 --> 00:00:59,080 So not found air. 18 00:00:59,230 --> 00:01:03,500 This is going to extend custom air inside of here. 19 00:01:03,500 --> 00:01:09,020 We need to define a status code to send back to the user in the response will say status code for 0 20 00:01:09,020 --> 00:01:09,600 4. 21 00:01:09,650 --> 00:01:13,390 And by the way if we didn't add that in typescript will tell us we need to do it. 22 00:01:13,430 --> 00:01:18,500 You'll notice that we're getting air right now saying hey you need to implement serialize errors and 23 00:01:18,500 --> 00:01:27,310 status code we could define that status code we do need to still define our constructor because we need 24 00:01:27,310 --> 00:01:29,790 to not only provide some default message your message. 25 00:01:29,790 --> 00:01:37,570 Something like let's say root not found and we do also have to run that little object set proto type 26 00:01:37,570 --> 00:01:50,080 of thing so object set prototype of this and not found air not roto type. 27 00:01:50,120 --> 00:01:55,940 And then finally we'll implement serialize errors. 28 00:01:56,160 --> 00:02:00,660 And the only requirement for this function we have to return an array of objects where each object has 29 00:02:00,660 --> 00:02:07,190 a message that's a string and possibly a field so in this case let's just return an array with one hardcoded 30 00:02:07,200 --> 00:02:08,030 object. 31 00:02:08,150 --> 00:02:14,890 We'll give it a message of not bound and that should be it. 32 00:02:14,900 --> 00:02:20,720 So now anytime that we throw this error in other words if we're in some root handler and we throw not 33 00:02:20,720 --> 00:02:30,210 found or new not found error and we put in all like that then our Middleware is going to automatically 34 00:02:30,210 --> 00:02:36,700 capture this error and return the appropriate response to the user so let's actually implement a root 35 00:02:36,700 --> 00:02:41,380 handler that's going to capture a request to any arbitrary route that we have not actually defined back 36 00:02:41,380 --> 00:02:44,850 inside of my index dot t s file at the very top. 37 00:02:45,460 --> 00:02:59,620 After importing our air handler I will import not found air from errors not found there and then after 38 00:02:59,650 --> 00:03:05,650 all of our different middleware as we wire up but before the error handler we'll put in an apt gets 39 00:03:06,220 --> 00:03:08,320 to star like some 40 00:03:11,440 --> 00:03:15,670 we don't even have to worry about a request or response in here because we're not going to access those 41 00:03:15,670 --> 00:03:16,640 variables. 42 00:03:16,750 --> 00:03:23,990 All we have to do is throw a new not found error like so that's it. 43 00:03:24,130 --> 00:03:28,960 As soon as we throw this thing Express is going to capture the air and send it off to our middleware 44 00:03:29,430 --> 00:03:34,180 not middleware it is going to take these status code it's going to call that serialize Eris function 45 00:03:34,630 --> 00:03:38,920 and it's going to generate a response and automatically send response back to ever was trying to access 46 00:03:38,920 --> 00:03:40,270 this route. 47 00:03:40,270 --> 00:03:44,330 Let's save this and do a quick test back at post. 48 00:03:44,370 --> 00:03:51,050 I'm going to try to go to a route that doesn't actually exist so I will try to go to API users sign 49 00:03:51,050 --> 00:03:55,760 up slash whatever and we've only set this thing up for a get request. 50 00:03:55,760 --> 00:04:02,430 So I am going to change this to get I'll then send it and lo and behold it works. 51 00:04:02,450 --> 00:04:04,480 So we've got a status of four a four not found. 52 00:04:04,820 --> 00:04:08,410 And we've got that same kind of exact air structure that we were looking for. 53 00:04:09,530 --> 00:04:14,620 Awesome now as I mentioned this is only going to work with get requests right now. 54 00:04:14,690 --> 00:04:19,640 We can make it work with any other arbitrary method if we just change apt I'd get right there to app 55 00:04:19,690 --> 00:04:21,190 dot all I've done. 56 00:04:21,200 --> 00:04:26,030 All is gonna watch for requests with any kind of method and with that any kind of routes. 57 00:04:26,090 --> 00:04:32,760 Now we should be able to save that make a post request to the silly routes and we'll see the same thing. 58 00:04:34,350 --> 00:04:36,920 Well I would say that this is not too bad. 59 00:04:37,140 --> 00:04:42,060 Again that is how we add in some new custom air to our application read a file create a class. 60 00:04:42,090 --> 00:04:47,420 Export it extend customer typescript is going to guide you on what you have to define it here. 61 00:04:47,490 --> 00:04:53,970 So you have to define status code and serialize errors and then throw the air at some point time and 62 00:04:53,970 --> 00:04:55,140 Express is going to take it from there. 63 00:04:56,170 --> 00:04:57,710 So this looks fantastic. 64 00:04:57,710 --> 00:05:00,430 Quick pause right here and I'll see you in just a minute.