1 00:00:00,670 --> 00:00:03,930 There are still a couple of things we have to do around our sign up root handler. 2 00:00:04,140 --> 00:00:07,990 So we have to go through that password hashing what we need to make sure that we generate a cookie or 3 00:00:07,990 --> 00:00:09,080 Jason web token. 4 00:00:09,100 --> 00:00:13,150 We also need to make sure that we resolve or fix up this if statement check right here. 5 00:00:13,150 --> 00:00:15,040 Let's take care of that really quickly. 6 00:00:15,040 --> 00:00:18,640 So in this if statement check we're checking to see if there is already some existing user inside of 7 00:00:18,640 --> 00:00:20,190 our database with the same e-mail. 8 00:00:20,230 --> 00:00:23,790 At present we are just doing a council log and then sending back an empty object. 9 00:00:23,880 --> 00:00:25,210 And that's not really appropriate. 10 00:00:25,390 --> 00:00:31,000 If a existing user already exists we need to somehow tell the person who made this request exactly why 11 00:00:31,000 --> 00:00:37,130 their request failed so we're going to do this in the same way that we've been doing air handling already 12 00:00:37,330 --> 00:00:39,100 in all those previous videos. 13 00:00:39,200 --> 00:00:44,540 We're going to create a new kind of custom air and then rather than trying to send back some message 14 00:00:44,540 --> 00:00:48,970 or response rate here manually we're going to instead throw that error. 15 00:00:49,010 --> 00:00:51,590 Let me show you what I would like to be able to eventually do. 16 00:00:51,620 --> 00:00:54,090 This is this would be an ideal scenario. 17 00:00:54,440 --> 00:01:02,720 If there is an existing user I want to be able to do something like throw a new bad Request air and 18 00:01:02,720 --> 00:01:05,870 then provide a string to tell the user exactly what went wrong. 19 00:01:05,890 --> 00:01:12,040 So in this case provide a string that would say something like email in use or something like that. 20 00:01:12,260 --> 00:01:14,240 That would be ideal. 21 00:01:14,290 --> 00:01:17,770 We do not have a custom air called Bad Request air just yet. 22 00:01:17,950 --> 00:01:22,390 So we're gonna be building that inside this video this bad Request air customer that we're gonna put 23 00:01:22,390 --> 00:01:25,300 together is going to be a very general use air. 24 00:01:25,420 --> 00:01:29,050 We're going to be making use of this customer quite a bit throughout the remainder of this course. 25 00:01:29,170 --> 00:01:34,210 We're going to use it just about any time that something goes wrong inside of request handler due to 26 00:01:34,210 --> 00:01:36,730 some input that a user gave us. 27 00:01:36,800 --> 00:01:40,880 This is a different kind of custom air compared to one that we put together already. 28 00:01:40,880 --> 00:01:44,790 That's somewhat similar to a number we put together a request validation error. 29 00:01:44,810 --> 00:01:51,590 Already the goal of this custom error was to handle outputs from Express validator that was exactly 30 00:01:51,620 --> 00:01:56,950 and only what this error was supposed to handle so this new customer we're going to put together called 31 00:01:56,950 --> 00:02:00,110 Battery cluster is a much more general purpose air. 32 00:02:00,250 --> 00:02:05,320 We're gonna throw it any time just about anything goes wrong if we don't have some reason to make a 33 00:02:05,320 --> 00:02:11,860 more general use case thing so let's create this new battery cluster we're going to get started by finding 34 00:02:11,890 --> 00:02:19,100 our heirs directory and making a new file inside they're called Bad request air dot to us now if you 35 00:02:19,100 --> 00:02:23,240 do not recall how to put a custom air together even though we spent a pretty good amount of time on 36 00:02:23,240 --> 00:02:28,790 it just a little bit ago remember the general rule of thumb all you have to do here is import that custom 37 00:02:28,820 --> 00:02:35,290 air abstract class you are going to create a new class you're going to extend custom air and then typescript 38 00:02:35,300 --> 00:02:38,840 is going to jump in and tell you how to proceed from there. 39 00:02:38,900 --> 00:02:46,240 Let me show you exactly how that would happen at the top of battery cluster I will import custom air 40 00:02:46,330 --> 00:02:57,040 from customer well then export a new class called Bad request air and that's going to be extending customer 41 00:02:58,070 --> 00:03:01,850 and so we can see right away that typescript is going to jump in and it's going to tell us everything 42 00:03:01,850 --> 00:03:09,120 we have to do to actually implement customer so we need to define that serialize errors method we also 43 00:03:09,120 --> 00:03:14,770 need to define that status code property let's get started status code it's going to set up a status 44 00:03:14,770 --> 00:03:20,140 code we're going to say that if any general error occurs due to some requests that a user is sending 45 00:03:20,140 --> 00:03:27,140 us we're gonna send back a response with a status code of 400 we can now hover over the constructor 46 00:03:27,140 --> 00:03:30,980 or the class name once again and now it's telling us OK you took care of status code but now I need 47 00:03:30,980 --> 00:03:37,390 to take care of serialize errors so I going to put inside of your serialize errors then type scripts 48 00:03:37,390 --> 00:03:41,200 going to jump in once again and say All right well you've got this method but you did not define it 49 00:03:41,200 --> 00:03:46,930 correctly you need to make sure that you return an array of objects for each object has a message and 50 00:03:46,930 --> 00:03:54,340 possibly a field so to make that go away we would return an array of objects where each object has at 51 00:03:54,340 --> 00:04:04,180 least a message like so OK so that would definitely get us at least one step closer but remember what 52 00:04:04,180 --> 00:04:08,530 I said about how we actually want to implement this when we throw this error I want to provide some 53 00:04:08,530 --> 00:04:13,920 message right here and I want that message to go back to whoever actually sent that request to us so 54 00:04:13,930 --> 00:04:19,600 I really want to take that string and display it right there I want to go back in the response that 55 00:04:19,600 --> 00:04:24,940 gets sent back to the user so we need to make sure that when we throw this air we capture that string 56 00:04:25,030 --> 00:04:32,030 by defining the constructor method and save a reference to that string we can then reference that string 57 00:04:32,330 --> 00:04:34,520 right there as the message property. 58 00:04:34,520 --> 00:04:39,480 Long story short we just need to define a constructor and receive that message so I got to define my 59 00:04:39,480 --> 00:04:40,570 constructor. 60 00:04:40,770 --> 00:04:47,050 I'm going to set up a property that will call message. 61 00:04:47,150 --> 00:04:50,430 That's gonna be a string inside of your. 62 00:04:50,470 --> 00:04:56,520 We're gonna call super with this dot message and I'm getting a little bit of an air here. 63 00:04:56,530 --> 00:05:01,860 It's just because I'm trying to reference a message technically remember we put public on there. 64 00:05:02,050 --> 00:05:06,460 It's going typescript is going to automatically take this property or this argument and save it as a 65 00:05:06,460 --> 00:05:09,050 property to our instance of bad request error. 66 00:05:09,280 --> 00:05:15,280 If we try to reference this dot message inside the super right here super is executed before typescript 67 00:05:15,280 --> 00:05:20,060 kind of jumps in and saves a reference to message on our instance. 68 00:05:20,130 --> 00:05:25,300 So we can not actually reference this dot message right there but we can anytime after that safely you'll 69 00:05:25,300 --> 00:05:27,280 notice that error now goes away. 70 00:05:27,280 --> 00:05:32,300 It that's definitely better to then inside of a year after that Super we do also have to add in that 71 00:05:32,300 --> 00:05:38,620 little extra magic line of code just because we are extending a class that is built in to javascript. 72 00:05:38,750 --> 00:05:49,230 So that was the object set prototype of this bad request error dot prototype like so. 73 00:05:49,310 --> 00:05:56,300 And now finally right here we can reference this dot message. 74 00:05:56,620 --> 00:06:03,710 And that should be at let's now go back over to our sign up request handler we're getting an air around 75 00:06:03,710 --> 00:06:07,290 Battery cluster that is just because we have not imported it into this file. 76 00:06:07,330 --> 00:06:17,080 So at the very top I'm gonna make sure that I import bad requests here from up one directory air's bad 77 00:06:17,150 --> 00:06:22,670 requester. 78 00:06:22,690 --> 00:06:23,800 All right I'm gonna save that. 79 00:06:23,890 --> 00:06:26,080 I'm gonna make sure I do not have any errors down here. 80 00:06:26,080 --> 00:06:27,680 Yep everything looks good. 81 00:06:27,730 --> 00:06:32,830 I'm going to delete that comments about stuff going to save the file once more and then I'll go back 82 00:06:32,830 --> 00:06:36,530 over to postman and do a quick test of this okay. 83 00:06:36,560 --> 00:06:45,350 So it Postman you'll notice that I've got a unique email enough in here so I can send this off I have 84 00:06:45,350 --> 00:06:49,940 now signed up for new accounts and now we're going to send the same exact request again and we should 85 00:06:49,940 --> 00:06:52,730 end up seeing an error with the appropriate structure. 86 00:06:52,800 --> 00:06:57,230 So remember it should match that consistent response structure that we want to have for all of our different 87 00:06:57,230 --> 00:06:59,340 errors that could possibly occur. 88 00:06:59,390 --> 00:07:02,530 So I'll send again and there we go. 89 00:07:02,560 --> 00:07:03,570 Now I've got errors. 90 00:07:03,580 --> 00:07:08,160 It's the array of objects that has the message property that we are looking for yet. 91 00:07:08,170 --> 00:07:08,960 So this looks pretty good. 92 00:07:10,510 --> 00:07:15,400 Well I'd say that we are making progress but we sought to handle that password hashing stuff. 93 00:07:15,400 --> 00:07:20,830 We also need to make sure that we eventually take this signed in user and send back a cookie or Jason 94 00:07:20,830 --> 00:07:22,810 web token that stuff as well. 95 00:07:22,810 --> 00:07:25,110 Let's take care of the password hashing in just a moment.