In this video.
Let's finish up with the error handling.
So we are going to create a function whereby we are going to use it to throw an error and going to catch
inside the global error handler here.
So inside the utils, let's create one file and I'll call this one as app error dot JS.
And for this we can use a class or a normal function.
So let me stick to using normal function.
So we're going to be as cost app error is equal to and for this error function here, we will pass in
the message that we want to send to the global error handling and then the starters code.
And for this, if we don't provide it, we have access to it inside our global error handler here as
500.
So the most important one here is the message.
So we need to add the message to the error class in JavaScript.
So you're going to make use of late and then the error is equal to new than the error class.
And here it takes only one argument and that is a message.
So here we can add additional properties like the stock trace.
So here make use of error dot stock and is equal to the error dot stock.
And the next one going to be the status code, going to be error, the status quo.
I want to add this one also as status code.
All right.
So in case we have one, then you go ahead and use it.
Otherwise, you're going to use 500, as we did inside the global zero hand line.
The last step is that I need to return the error from here.
As tax and less export module exports is equal to the app error.
So before using it, let's look at how it's going to work.
First of all, we have our global error handler here and next is that this handler is going to watch
anywhere in our code where we are returning or catching any error is going to send back the response
to the user.
So this global error handler here is going to look up at the app error and before the global error handler
will get what came back from the app error here, unless we pass what came back from this one to next
and next, we'll pass that response to the global error handler.
So back to the user controller and here we go.
Let's require the app error.
So let me try to reserve it so I can have the auto import.
Wouldn't it be as app error?
So here we go.
So to be able to catch any error, we are going to pass next as a third argument to this.
And inside.
Let me explain it.
And inside here, we are going to return from here and we are going to call next and are going to pass
in the function call as an error.
Then you pass in the message.
So what we want to send is user already exists or pass in here as first element.
And remember this one also take second argument that is status code.
So here we can provide 404 or 403 but for the meantime I will leave the one and I need to remove this
one from here.
So now if I make the requests to register with the same email, let's see.
And now we can see that we have a valid error catching handle and we have the starters and which is
for this one as feel when I make use of something here.
Save it and let's register.
And you can see the starters is called something, meaning that you go back what came back from the
global era?
Angela Yang So let me say again and let's look at the actual error that we want.
Well, we couldn't get the message.
I think we did some typing mistake here.
Let's look hard.
It's supposed to be one as message as that.
So now let's send the request.
And here you can see that we have the message called user already exists.
And this is where we throw the error, which is this, and we have the valid status code.
So this is how we can handle any error.
So anywhere in our code we can throw any error.