In this video.
Let's see how we can handle what is called on handle rejection.
An example of this kind of error is when the DB connection refused to connect to MongoDB, meaning if
there are some connection error to MongoDB and then another type of error that it can be OC and the
on handle rejection is that if we refuse to bring in catch block inside async await function and this
kind of error is going to help us in the development mode.
Let me show you an example inside the config file for the DB.
Let's change the password and for this one if I check my terminal, you can see that we have some random
string of error here.
And again, if you are new to programming or especially to MongoDB, this kind of error message is,
you know, be understand.
So to be able to send the required or simplified message, we are going to handle this kind of can handle
promise rejection.
So how are we going to do it so we inside the server file, we need to create an instance of the server.
So here we are going to assign a variable called server to what comes back from the app dot.
Listen.
And on this one we can change or listen to any event type.
Remember, Node.js is an event driven technology, so it means that we can listen to types of events
and the first type of event that we are going to listen to is what we call on handle rejection and we
have access to that on the process dot on.
And here we are listening to an event types and we have different types of events in Node.js.
The one that we are going to handle is what we call on handle rejection, which is this one in our pass
in a callback function.
I have access to the actual error and then the promise.
So here we go.
So here I can go ahead and then console.log this simplified message.
If you check the console, you can see that we have the message as back off, which is the authentication
error, meaning that I pass a wrong password.
So here we have access to those messages on error message and error dot name.
So here you go back tick and then dollar sign replaces the error dot name.
And then space dollar sign carry braces, error, dot message and that is it.
So let's save it.
And now let's check the console and you can see that we got simplify message as mongo server error but
authentication meaning the authentication failed.
All right.
So here ends the section on how we can handle errors in experts charges.