In this video.
Let's see how we can handle saying errors in experts, for example, when dealing with databases.
Because of that, we are going to handle some possible errors when working with MongoDB, for example,
validation errors, duplicate key errors and invalid object ID errors, even though Mongoose handles
these errors for us automatically.
But what we want to do is that we want to send some meaningful messages to the end user.
So let's start it inside the config folder.
We have a file that connect to MongoDB and you can see that I am calling the function automatically
here and without exporting it.
So as soon as I require this file, this function will run.
And inside the model we have one file called Postal JS and on that we have post schema and inside the
schema we have to file title and content and these fields are required.
Field.
So next step is let's go ahead to require the function to connect to MongoDB.
So back to the server file.
Let's go ahead and do that.
I have collapse or the route that we are done with it.
So above here, this is how I'm going to require the file to connect to MongoDB.
And make sure that your server is running.
So let's check it out and you can see that it has been connected to MongoDB.
The next step is let's require the model const posts is equal to required for a PS+ model and then the
posts.
So let's create a route to create post.
So here we're going to be the end point post for a slash post.
So here we go, app dot post, forward slash post, and then the handler requests response and my error
function and let me mark my function as an async.
So insert here I will bring in try and catch inside a patch.
Let's send the error message.
And instead of try, let's go ahead and create the post.
I'll pass in the entire request, buddy.
And here we go.
To be able to receive incoming data from experts.
We need to configure it by making use of upload to use and then Xpress dot Jason and that is it.
So let's go ahead and send the response if everything goes on well.
So let's go ahead and make the request.
And I'm going to use this extension called ten lines.
This is a route and then you're going to be the post mortem.
So when I hit ST with that passing in the title and the contents, let's look at the arrow.
Now this is what we have.
Looking at the structure of this arrow is not all that descriptive to the end user.
You can see we have nested objects before we can even get an actual message.
So our job is that we are going to handle this error and display some meaningful error messages.
Remember, we have created the Arrow class here.
So any time we want to send some meaningful error messages, we are going to pass the arrow to this
arrow class.
So inside the cards we are going to use the error class or the function that we have created.
Because of that, we need to pass in another element called Next because we are going to pass the error
to next.
So inside here we are going to avoid using this way, but instead we are going to call next and remember
next accept the structured error, which is from the up arrow.
So because of that, our make use of new and then our code, the app error.
And here the first element is the error that we want to send.
And that error can be found on this object.
So here our pass in the entire error here.
And then this is code 400.
So with this one, let's look at the structure.
Now, when I hit Saint and you can see that this message is clear than the previous one better so we
can create its own function to have well-structured message than this one because this error that we
pass in is a global error.
But we can create a specific function to handle this kind of error.
For example, validation error as we've seen.
So the takeaway here is that if you want to handle async error, you need to bring it next here and
then inside a catch you pass in the entire error to the app error class.
Or we can create a global function where we are going to take away the catch.
And so any time I want to cut any error, I don't want to bring in here, but instead I will wrap the
entire function inside this global catch error handler.
But to make it more simple, let's maintain in this way.
So this is how we can handle async error in express in the next video.
Let's go ahead and improve the way we are sending error, such as the validation error to the end user.