All right.
And the first error that I want to work on is the missing field.
So when we're setting up the schema, we set up three properties to be required.
So if one or multiple are missing, we are going to be getting back the hours and we'll have access
to those errors in the error handler.
So now what I want to do is set up the condition where I check for that error and the error is going
to look something like this.
So if I'm going to go to a postman and if I'm going to remove the name, you'll notice that response
comes back, something like this.
So the message is going to be equal to a our object.
And there we have the errors property, which is an object itself.
And in there, every field that is going to be missing is going to be added as a property also in that
error object.
So the main one, not in the errors.
That's a separate property.
But on the main, our object, we also have these properties where we have the message name and also
one more message.
So this one is with an underscore where the fields are displayed, the ones that are missing, and if
there's only one field, then we'll get this response.
But if there's, for example, two fields missing, you'll notice that there are more properties in
the error object.
Again, there is one big error object and then inside of it first we have errors property.
So these are all the fields that are missing at the moment is just one, but if I remove the emailing
us, I don't need to set up proper syntax now in order not only I have the name, I also have the email
in the Irish property and then on the object, the error object.
There's also properties here for the name and message.
And here's what I want to do.
I want to first check the name property, and if it's going to be equal to validation error, then we
know that we'll have to change some things around because the moment I'm sending back five hundred and
that's not what I want.
Actually, I want to send back 400 because it is a bad request.
That's number one.
Number two, I mean, we're sending back this giant object.
How useful that is to the frontend?
Well, not really.
We kind of want to shorten this up a bit and send just, hey, listen, the field is missing or stuff
along those lines.
And the way we can do that first.
I can just pass on the message if I want.
That's definitely an option.
But in our case, I'll also show you how we can iterate over these properties in the areas, object
the property on the error object, and then we'll just grab the message over here.
Senator, we have email and then we have the message.
And the same goes for the name one again.
Two options.
This probably is a faster, but I think that one is a bit more concise.
The second option?
So let's start working on that.
I'm going to navigate back to our project and where I have the air handler.
First thing that I want to check is if in the air object, the name property is equal to validation
error.
How's that going to look like?
Well, I can go with if error.
So that's the main object, not the error property inside of it.
A big distinction.
And I'm going to say, if the error name is equal to and Huron, you can type it out.
But in my case, just so I can avoid some silly bugs, I'm going to copy and paste.
I'm going to set it up as validation error.
And if this is the case, first thing that I one there is change the status code because like I said,
it's not five hundred anymore.
It is 400.
It is a better request.
How do we do that while we go with default error vanish status code?
And that is equal to.
And let's use our status code and then we're looking for bar request.
So that's number one.
Number two, what I can also do is change this message where I'm going to say, if this is the case,
then the default error message property is going to be equal to my error.
And then remember this property over here.
We also have the message correct so we can just go here with error and message like show.
And now, of course, you probably want to see the response.
So it just changes around where I'm going to leave this one just in case I ever want to use it.
I want to send back and then I'll copy and paste and uncommon this one.
And instead of the error that we're sending back, what I want to do right now is send back the default
error and then the message, not service one.
And what you'll notice right now in the post, man, once we send it, we should get back four hundred.
That's number one.
And then we get this message.
Here's a validation failed email.
And I'm definitely you can keep it like this.
But in my case, what I'm going to do, I'm going to comment this one out.
Just search state for your friends and instead what we're going to do.
We're going to look in that error object and in their outlook for the property values.
So remember, that was a giant object where we had all the fields that are missing the name, the email
and password.
I will set them up as all right, I'll iterate over them and then I'll join the messages together.
Now, since probably this is a little bit confusing, I'll come on this one out and let's send it one
more time.
So let's send it.
And you'll notice this error, right?
So in JavaScript, we have object values, which essentially is going to set up all these properties
as an array.
So whatever you have over here.
So in my case, I have the email and I have the name, and then I'll iterate over that array and I'm
just going to look for these messages just so have a little bit shorter error response.
Just in case you need to jog your memory on object values method.
The Amdocs is a very good place to start.
So let's navigate back over here, and I'll just simply say a default error message is going to be equal
to object values.
And this is going to set up all the property values as an array and what properties I'm looking for
while I'm looking for error.
That's the main one.
And then more specifically, errors object.
And then once I have that, I can just change that map and then I'll call this an item.
So now I'm talking about each and every property over here, and I want to access this message.
So let's navigate back here.
And let's say that as I'm iterating, I want to return the message and I want to join them together
as one string, so chain not join.
And I'll just add a comma in between.
And once I have this code in place now again, I will uncommon this one and comment out into a response.
So if we go back to the postman and if we're sending notice, not only I have four hundred, but I also
have please provide email.
Please provide a name.
So if I'm going to set up my name back up.
And in this case, I'm going to go back with John.
You'll notice that now I only have.
Please provide email again if you're somewhat iffy on this code.
You can always use this error and message.
And lastly, let me just point out something where remember when we were setting up the user, we were
also checking for the valid email.
So just to reiterate in the user schema, we check for empty values.
We check for unique email, which we are going to cover in the next video and also we check for valid
email.
And what's really nifty that it actually gets handled with our validation error as well.
And if you want to start out, go to your postman and try to set up the user with some kind of invalid
email.
So in this case, I'm going with email and then one at one.
And what you'll notice when you send back is the message, as well as the status code.
And in the message, it clearly says please provide valid email, which is again, super, super cool
because we don't have to handle that separately.
So not only we're handling missing fields, we're also handling a valid email.