There are still a couple of things we have to do around our sign up root handler.
So we have to go through that password hashing what we need to make sure that we generate a cookie or
Jason web token.
We also need to make sure that we resolve or fix up this if statement check right here.
Let's take care of that really quickly.
So in this if statement check we're checking to see if there is already some existing user inside of
our database with the same e-mail.
At present we are just doing a council log and then sending back an empty object.
And that's not really appropriate.
If a existing user already exists we need to somehow tell the person who made this request exactly why
their request failed so we're going to do this in the same way that we've been doing air handling already
in all those previous videos.
We're going to create a new kind of custom air and then rather than trying to send back some message
or response rate here manually we're going to instead throw that error.
Let me show you what I would like to be able to eventually do.
This is this would be an ideal scenario.
If there is an existing user I want to be able to do something like throw a new bad Request air and
then provide a string to tell the user exactly what went wrong.
So in this case provide a string that would say something like email in use or something like that.
That would be ideal.
We do not have a custom air called Bad Request air just yet.
So we're gonna be building that inside this video this bad Request air customer that we're gonna put
together is going to be a very general use air.
We're going to be making use of this customer quite a bit throughout the remainder of this course.
We're going to use it just about any time that something goes wrong inside of request handler due to
some input that a user gave us.
This is a different kind of custom air compared to one that we put together already.
That's somewhat similar to a number we put together a request validation error.
Already the goal of this custom error was to handle outputs from Express validator that was exactly
and only what this error was supposed to handle so this new customer we're going to put together called
Battery cluster is a much more general purpose air.
We're gonna throw it any time just about anything goes wrong if we don't have some reason to make a
more general use case thing so let's create this new battery cluster we're going to get started by finding
our heirs directory and making a new file inside they're called Bad request air dot to us now if you
do not recall how to put a custom air together even though we spent a pretty good amount of time on
it just a little bit ago remember the general rule of thumb all you have to do here is import that custom
air abstract class you are going to create a new class you're going to extend custom air and then typescript
is going to jump in and tell you how to proceed from there.
Let me show you exactly how that would happen at the top of battery cluster I will import custom air
from customer well then export a new class called Bad request air and that's going to be extending customer
and so we can see right away that typescript is going to jump in and it's going to tell us everything
we have to do to actually implement customer so we need to define that serialize errors method we also
need to define that status code property let's get started status code it's going to set up a status
code we're going to say that if any general error occurs due to some requests that a user is sending
us we're gonna send back a response with a status code of 400 we can now hover over the constructor
or the class name once again and now it's telling us OK you took care of status code but now I need
to take care of serialize errors so I going to put inside of your serialize errors then type scripts
going to jump in once again and say All right well you've got this method but you did not define it
correctly you need to make sure that you return an array of objects for each object has a message and
possibly a field so to make that go away we would return an array of objects where each object has at
least a message like so OK so that would definitely get us at least one step closer but remember what
I said about how we actually want to implement this when we throw this error I want to provide some
message right here and I want that message to go back to whoever actually sent that request to us so
I really want to take that string and display it right there I want to go back in the response that
gets sent back to the user so we need to make sure that when we throw this air we capture that string
by defining the constructor method and save a reference to that string we can then reference that string
right there as the message property.
Long story short we just need to define a constructor and receive that message so I got to define my
constructor.
I'm going to set up a property that will call message.
That's gonna be a string inside of your.
We're gonna call super with this dot message and I'm getting a little bit of an air here.
It's just because I'm trying to reference a message technically remember we put public on there.
It's going typescript is going to automatically take this property or this argument and save it as a
property to our instance of bad request error.
If we try to reference this dot message inside the super right here super is executed before typescript
kind of jumps in and saves a reference to message on our instance.
So we can not actually reference this dot message right there but we can anytime after that safely you'll
notice that error now goes away.
It that's definitely better to then inside of a year after that Super we do also have to add in that
little extra magic line of code just because we are extending a class that is built in to javascript.
So that was the object set prototype of this bad request error dot prototype like so.
And now finally right here we can reference this dot message.
And that should be at let's now go back over to our sign up request handler we're getting an air around
Battery cluster that is just because we have not imported it into this file.
So at the very top I'm gonna make sure that I import bad requests here from up one directory air's bad
requester.
All right I'm gonna save that.
I'm gonna make sure I do not have any errors down here.
Yep everything looks good.
I'm going to delete that comments about stuff going to save the file once more and then I'll go back
over to postman and do a quick test of this okay.
So it Postman you'll notice that I've got a unique email enough in here so I can send this off I have
now signed up for new accounts and now we're going to send the same exact request again and we should
end up seeing an error with the appropriate structure.
So remember it should match that consistent response structure that we want to have for all of our different
errors that could possibly occur.
So I'll send again and there we go.
Now I've got errors.
It's the array of objects that has the message property that we are looking for yet.
So this looks pretty good.
Well I'd say that we are making progress but we sought to handle that password hashing stuff.
We also need to make sure that we eventually take this signed in user and send back a cookie or Jason
web token that stuff as well.
Let's take care of the password hashing in just a moment.