Male Instructor: Finally, let's now handle
Mongoose's validation errors.
So remember how we tried to update a tour
with some invalid data here,
and then got this kind of error?
Okay.
And let's actually try to add another one.
So a name that's really short, okay?
And because that's not allowed, again.
Oh and now we actually no longer can see our error
because we're now in production.
And of course this error right now
is not handled correctly.
It is not marked as operational.
And so therefore,
remember we get this kind of generic error message back.
Okay?
So just to see the error that we had before,
let's quickly switch back to development.
So npm start.
And,
send it now again.
And so now we get back our development errors, okay?
Because now I want to show you
how we can actually create a meaningful error message
based on all of this, what we have here.
Now right.
So inside our error,
we get an error properties.
And that property itself is an object
which has a lot of objects in there,
and each of them is for one of the fields that has an error.
All right?
So the first one here is for the length of the tour.
Then the second one is for the difficulty,
that is also wrong.
And the third one is for the rating, okay?
So each of these actually has a nice error message, okay?
So basically the one that we defined in our Mongoose schema.
And so now we want to extract
these three messages from here,
and put them all into one string, all right?
So let's go ahead and do that.
Okay.
And again, I'm gonna start
by actually creating the conditional down here.
So if(error),
and actually let's take a look.
So here we have the error.
We have the errors, all of them.
And I need to scroll here.
And yeah, so here's the name.
So error.name is ValidationError, all right?
So let's grab that.
Okay.
And so this again, is an error created by Mongoose.
So just like the first one,
and so they look similar, okay.
Now I don't want this here.
But instead I want that the error
should be equal to handleValidationErrorDB,
and send in the error, okay?
Now let's copy this again.
All right.
And let's again, simply start by creating our message.
Invalid input data.
And then let's also return the error.
So new AppError(message, 400).
So VS Code already recognized
that I wanted to type just that, all right?
Now in order to create one big string
out of all the strings from all the errors,
we basically have to loop over all of these objects,
and then extract all the error messages into a new array.
So let's again, take a look at that.
Okay.
So the object that has all of the objects
in there is errors, okay?
So we have one error for name, one for difficulty,
and one for ratingsAverage.
And so we're gonna basically loop over this errors object.
Okay?
And in JavaScript, we use Object.values
in order to basically loop over an object.
So the elements of an object.
All right?
So let's create a variable here called errors,
which again will be an array
of all the error messages for now,
and now Object.values.
And so we want the values of err.errors, all right?
And now loop over them using a map.
And then in each iteration,
we are simply gonna return the error message, okay?
So just to make sure we're all on the same page here,
the Object.values are these objects, okay?
So this object, and this object, and the next one, okay?
So these are the values.
And so now all we have to do
in order to extract the message,
is to say value.message, okay?
So .message.
So basically the current value,
or let's say the current element,
I like to use element for that,
and we want to return el.message.
Okay.
And now, of course, this should not be here.
And you had probably already noticed that.
So in fact, this is where we want this, okay?
And so now all we need to do
is to pluck this into our message string, all right?
So errors, and now we simply join all of them together
into one string using period and then space, okay?
And you will see, in a second, why that is.
All right.
So let's switch back to production here.
So running our production start script.
Try it again now and let's wait for it,
and bam!
Here we go!
So invalid input data.
Then the first error string,
must have more or equal than 10 characters.
Then the second one,
and the third one.
Perfect.
And so that's why I used the period and space, okay?
So to basically separate these three strings
with a period and a space, all right?
And now this looks like a very nicely formatted
error message that everyone can easily understand.
Right?
So, we're basically done here.
All right.
Now we could've made this error, handling error,
a lot more complete still.
For example, we could define different error severity levels
like saying, this error is not so important,
this error is medium important,
and this error is very important or even critical.
And we could also then email some administrator
about critical errors.
And really, there's a lot of stuff that we could implement.
But again, in a kind of small application like this one,
what we have here is already really good, okay?
So this is quite a robust strategy already
that we have implemented here,
and I'm really happy with it, okay?
So all this logic here with the operational errors
that we implemented here,
so that's already quite sophisticated.
Okay?
Now if we were ever to find another error
that we want to mark as operational,
then of course all we would have to do
is something similar to what we have here, okay?
So basically implement another function for that one,
and then return our own operational error
so that the send error production
can then actually send that operational error
to the client, right?
Okay, and with that being said,
our error controller is actually finished.
But there are still some other errors that we need to handle
which are completely outside of Mongo or even of Express.
And so we're doing that in the rest of this section.