All right.
And once we can successfully pass our error down to our error handler using next.
Now let's deal with this.
Try and catch.
Because as you're looking at this controller, you're probably thinking, okay, so this is nice, but
do I have to repeat this?
Try and catch pretty much in every controller.
And the answer is, well, kind of because it is very useful to use.
Try and catch when you have these async operations.
But there's also another way how we can do that, where essentially we can use a package that handles
that for us.
And as a result, we won't have to set up these trying catches in every controller.
Effectively.
That will be done automatically for us behind the scenes.
Now, there's multiple ways how you can do that.
Actually, you can set up the function yourself.
And again, not to sound redundant, but we did that in the Node Express Project video.
However, in this case we'll just go the fast route right away where we'll install the package.
Now what package am I talking about?
The package name is following Express async our package.
And effectively by using this package we can avoid setting up these trick edge blocks.
And again, the package will do all the heavy lifting behind the scenes.
If you want to find out more info, here's the link.
And before I navigate to the docs, I just want to install so everything is working.
So let me copy and paste.
That should be fast and let me go with npm start.
And the way we use the package is following and these are the docs we install the package.
We just did that.
And essentially in the server you just want to require the package.
Remember we're using a sex module, so syntax is going to be a little bit different in here.
They're using require and they do have a notice about calling next, which at the moment probably is
not going to make much sense since we're not going to implement that, since we're not checking for
things in the controller directly.
So in our case, it's going to work right from the get go anyway.
But eventually, once we start checking for things in the controller instead of using next like we have
in our code right now.
We'll need to throw a new error.
Again, at this point, it probably doesn't make much sense, but it will make sense once we start checking
for things in the controller.
And then, of course, I'll just remind you that it is because we are using the package.
So what we want to do right now, well, we want to go to server JS and you know what?
Actually, no.
Let's go to our controller and just remove this try and catch and let's see whether our error gets passed
to the error middleware regardless.
So let me save it.
We don't need to use next anymore so we can remove that sucker as well.
And then let's navigate to a server.
And then right after the dot and V, I'm going to go with import and we simply want to import a package.
That's all we have to do.
So we're going to go here with express async errors.
And I mean, call me crazy, but I always like to restart the server.
Don't ask me why, so I'm just going to go with NPM start just so I don't have to chase some dumb bugs.
And now let's send one more time request from Postman.
So let me navigate here again.
Let me change this maybe around where I will provide the email, but the name will be missing.
Just so you don't think that there's some trickery that I'm just making this up.
So let me go with John at gmail.com.
And then let me remove the name.
So let me do it this way.
Let's send it.
And yep, I still have the error.
Why?
Well, because I didn't provide the name and I did install the package.
So we're using the express async errors.
And by doing so now, we don't need to set up those catch blocks.
And we still pass on our error to our error handler middleware.
And I can clearly see that in a console, because that's the error again, that I'm getting from the
mongoose where essentially I have please provide a name since the name property was missing in my request.