All right.
So our initial functionality works somewhat OK if we provide all the values we create the user and then
we send it back.
If there's an error, we send back five hundred.
Now, first of all, let's understand where is this error coming from?
And I just coming from the Mongoose.
Why?
Well, because when we were setting up the user, we were setting up the name, email and password required.
And also email is unique.
And then we also have this specific one for the valid email.
So those are our validators.
So if those values will be missing, we'll be getting back the error.
Correct.
So hopefully we're clear on that and not at the moment.
We're pretty much just hard coding.
This is going to be five hundred and there was an error.
But remember when we talked about the error handler when we were singing up quite a few videos ago,
I mentioned that we need to place at last one because in Express will be able to pass our error from
the controller to our error handler.
So remember all the way in the bottom, we set up error handling middleware right after not found.
And for the time being, we are just gone, longing the error, and we're sending back this generic
response.
And the way we can pass the error from our controller, basically from our out to our handler is fallen.
If you take a look at the express ducks, essentially if you have the try and catch, you can go with
next.
You just need to add it to your function parameter and then you pass in the error.
So in the process, what we can do, we can eliminate of this code.
Now it's not much, but it's a good start.
So we don't have to each and every time typing this, we can just say, Hey, go with next and then
pass it to the error handler and then we'll figure out what's happening.
So let's try doing that.
First of all, we need to set up the next parameter, which passes it on to our next middleware, which
in our case will be our error handler.
And then we want to do is pass in the error.
So we're going to go next and then the error.
And then since we're logging the error in the error handler middleware, we should see the error.
And instead of sending back, there was an error.
I can also just take this error and send it back.
So that way, we can clearly see now this is really up to you, but I will right away go with her.
If you want to just console.log it first and then pass it on, it doesn't really matter.
I'm just going to go with message is equal to an error.
And now it's going to happen if I'm trying to create the user without again providing one of the values.
Check it out.
Now we're getting back this massive error from the Mongols now in fear of it.
And basically, we'll set it up that we're sending a more meaningful message because at the moment,
this is giant.
The main idea of this video is for you to understand that if you have errors in the controllers, the
way we handle that, we go in next.
Now, in the next video, it will implement a package that takes care of our try and catch us.
So the setup is going to be a little bit different.
But if you are using this, try and catch approach instead of hard coding those errors for pretty much
each and every controller, what you can do is add this next and then set up the error handler.
Of course, that's a must.
And then go next and then passing the error.
And then in the error handler, eventually we'll set up the logic where I'll be like, OK, is this
our error?
Is this Mongo, sir?
If it's a mongoose error, then let's send back something more meaningful.
Instead of this giant object and all kinds of cool things.
So for now, the biggest takeaway from this video is for you to understand that next, we'll pass it
on to the next middleware.
And in our case, what next passes it on to our error handler, which is located all the way at the
end of our route.