In this video.
Let's go ahead and improve the way we are handling this kind of error.
That is the validation error.
Remember, the global error handler handles any error that might occur in our application.
But let's say that we want to handle a specific error type, for example, validation error, and we
want to send some customized message based on this kind of error.
In that case, we can create a function to do that, or we can maintain this way because this one is
more clear than the before.
But let me show you how we can create a specific error message for specific error type.
So here we go inside the utils folder.
Let's create one file core handle.
Validation error in MongoDB DB for short as that.
So here is a function const handle validation error DB is equal to error function.
For this function we are going to pass in the entire error object as we see in the catch here.
So for that object we are going to extract some properties and add our custom message to it.
So our pass in the error here.
And before we continue, let's go ahead and continue that log, the error that we passing here upon
calling the function.
So let's export the function here as that.
So now inside this file, let's go ahead and require the function.
So inside this route that is on the catch, let's remove this one for now or we can comment that one
and we call the function here and then we pass in the entire error.
So let's check our terminal after we make the request.
So here we go.
Our head send.
And you can see that this is an error object.
And what we want is that we want to extract the properties and value and customize it.
So inside a function, let's see how we can get that one done.
We are going to create an object called arrows, and it's equal to we're going to use object values
to get the values from the properties of the arrow.
So make use of objects, dot values.
So here we pass in the entire arrow objects, and on that we have a property called arrows.
Next step is let's go ahead and look through, use it map.
And for this one, I have access to the individual element.
And on that we have what is called message.
So the next step is that I'm going to create a variable core message.
And on that, I'm going to add my custom message by saying that invalid.
Imputes.
Data.
And then I would inject the actual message and I'm going to use dollar sign to replaces the entire error
objects.
Then I'm going to join with dot as that.
Next step is let's return the message from this function.
The next step is that inside the server file, let's go ahead and then require that call handler validation
error.
We have it already.
So now here is the handler.
So if I go ahead and send the request now, let's see, you can see that it is loading because we are
not retaining or passing to the next function.
So what we need to do is that we are going to convert or pass this message to the global error handler.
So we are going to bring in the app error class here.
App error.
And before sending, I want to customize it.
So here I'll make use of new and then create instance of the app error.
And next I'll pass in the message, the one that I've customized, which is this one and next is I'll
pass in the status quo score 400.
So for this one, we are sending our customized message to the global arrow handler as that.
So what we are going to do is that we are going to send this message to the next handler.
So here we go.
Let's remove everything from here and I'll bring it next.
And for that I'll pass in the function as that.
So when I say it now, let's go ahead and make the request.
And this time around you can see that we have our custom message code invalid.
I think I made some type of mistake here, so here is going to be as invalid.
As that.
So let's see, when I hit ST and you can see that we have a customized message that is invalid input
data.
So if we want to maintain by using the global method of 100 arrows, we can do that.
Or if we want to stick to a custom message for a specific arrow type, then this is a way to go.
So the next video.
Let's see how we can handle Arrow for example, invalid object ID.
Let's get into that.
The next video.