Before we continue to implement the rest of the functionalities for our API, let's handle any errors
that might occur in our application.
If you have taken some of my courses, you can see that I use a third party package like Express Async
Handler to do that.
But for this we are going to create our custom error handler from scratch.
So let's get going inside the server file.
You can see we have a comment here called error handlers and you need to create your error handling
below all your route.
And experts make it pretty easy to be able to handle errors.
We bring in app dot use and for this we pass in a callback function as that and inside the callback
function we have access to four domains.
The first one is the error request response and then next.
So in the error that might work in our application, we have access on this object core error and the
request contains the request object and then the response contains the response object and then the
next function.
So in any of our route, if we throw an error, we are going to have access to that error inside this
object.
On the error object, we have these properties.
One is the actual message about the error.
The next property is what we call the status.
And then after that we also have what is called the status code.
And lastly, we also have what is called the stack.
The stack determines which file that a particular error OC and what line of code that that error.
And I will show you as you move on.
So for any error that might occur in our application, we are going to receive these properties.
So we are going to create a function on its own.
And then we require it and use it inside this app to use because we want to keep our server file with
less code.
So because of that, let's cut this one.
And next is let's go to the folders here and let's create one folder called middleware.
Here we go for the middle, where I'm going to have a couple of them.
But for now, let's create one for error handling and I'll call this one as global error handler dot
JS and here our paste what I copied and I need to assign to a function called context as global error
handler is equal to that.
So how are we going to send these messages?
So let's see on the error because it's an object, we can add our own properties to the objects.
So first of all, we are going to add a property called status code and then it's equal to error dot
status code.
If there is one, then go ahead and use it.
Otherwise use what is called 500 and next case.
Let's add a property called status.
Equal to error dot status if there is one.
Go ahead and use it.
Otherwise we are going to provide our message.
See error.
It means that when you throw an error and we don't provide these values, we are going to use this default
values.
So next step is let's send the response as rez dot status.
And for this one, we are going to use the error dot status code so we can assign to a variable cost
as status code.
And the same as the status cons as status is equal to that.
So on the rest status, we are going to use the status code in case a user provide upon throwing an
error.
And after that we are going to use the JSON to send the message to the user.
And here we go.
On the status, we're going to use this message.
And then for the message, it's going to be as error dot message.
Or we can even assign to evacuate here as concert message is equal to error dot message.
So here I can remove this one.
Then this property that we want to send is going to be the stock trace.
So we call this one as stuck and we have access to that on the error dot stock and our pass in here
as the stock.
So the next is let's export this middleware from this file as equal to global arrow handler.
So inside our server file, let's go ahead and then require that middleware as global arrow handler.
And here we go.
Perfect.
So now let's pass it inside this arrow middleware here, and that is it.
Now we have configured our global middleware to receive any error that we are going to throw with this
implementation.
I can now throw or catch any error that might be OC.
So let's get to the controllers and let's check it out.
So let's go to the user's controllers and here we go.
You can see that here we are checking if a user is found and if either is found, it means that this
email exists.
In that way.
We are sending this arrow, but this is not the right way of sending arrow.
That's why we have configured our global arrow handler.
So let's see how we are going to throw an arrow to achieve that.
We are going to pass next to the handler as that.
So any error that might work, we are going to pass through the next middleware and then the next middleware
will not handle to our global arrow handler.
So let's check it out.
So here, if a user is found, let's throw an arrow.
So I would like to use this same message.
But for this, I'm going to cut everything from here and let me break my cable here.
So here we are going to connect and then you are going to pass in an arrow and together one are going
to create instance of the error class.
And for this we pass in the message, remember, we need to pass in the message and then the status
code and then the status as well.
But for this, let's pass in this message as that.
So let's remove this one, let's save it.
And now let's check it out.
So inside the tender client, this user has been registered already.
So as soon as I hit send and there we go, this is a valid error case.
And in that we have a status code here which is 500 meaning internal server error and this is a message
or the properties that we pass into the global arrow handler.
The first one is a status as we specify.
Now let me show you where it is inside the middleware.
This is where we are referring to.
So if a user refuses to pass in the status, we are going to use this one.
And the next one is the message.
And the message is what we pass in upon calling next as that.
And then this one going to be the stack trace, which is this one and here is the stack trace.
And for a stack trace it gives us more details about the error, where it works and which file.
So if you read the file, this is where it works on the user controller line number 11, that is where
the error occurred.
So in our route we can throw any arrow and also we can catch any error that might be OC inside the cache
block.
So let's see for the registration, let me collapse this one.
And before I continue, we need to return from this function so that the function will exit.
Good.
So the next point is that inside the cache we can code next and pass in the error.
So now here we go.
We call next and next is we pass a new and then the error class and you pass in the entire error.
So now we are also handling any error that might be okay from MongoDB.
So let's go ahead and then remove these properties and now let's hit st and let's see the error and
this is a message we have which is status quo 200 main everything is okay, meaning that where we are
throwing this error, which is this part, meaning that a user needs to provide a valid data so this
is the right way of handling errors.
So let's remove this one now.
Let's go ahead and make the request again.
And let's send and.
Here we go.
And this is a status code 500.
Which means that is a valid error case.
Let's also handle this arrow nicely.
So I would like to use the same message.
So here I'm going to remove the response and I'm going to bring in the next middleware and pass in new.
And then the error.
Then I'll pass in the message.
All right.
But we have one problem.
We can see that any time you want to send an arrow, we need to use new arrow than it has in the message,
and we can pass in the status code as well.
So we can refactor this one by creating a class or a function for this.
So let's get that one done.
So inside the utils, you are going to create a file called App Error.
Dark jazz, and we can create a class or a function for this.
For the meantime, let me show you how we are going to create a class for that as app error, and then
we are going to extend the error class from JavaScript as that.
And inside here we are going to call the constructor function.
Then you pass in the message and then the status code.
Great.
And inside that we are going to call support and you pass in the message.
And lastly, we need to add the status code as this DOT status code is equal to the status code.
And then for the status tool, we can add as failed or error.
So here we go.
This dot status is equal to fail grade.
Now this is our error class, so I can go ahead and then export this one as module dot export.
For this one to.
I can create a function for this one instead of using a class.
So if you're not familiar with classes in JavaScript, then you can use the function way.
So here we go.
Const as app error is equal to a function and this function are going to pass in the message and then
they start as code and here we go.
So we're going to create an instance of error for JavaScript error class as equal to name and error.
Then you pass in the message.
Remember, the error class accepts a single core message.
That's why we pass in the message to the super of our class.
Then on the object I can add our custom messages to it.
So what we want to add is going to be the status code as error dot status code is equal to status code.
So let me change this one to let.
All right.
So lastly, I need to retain the entire error object from this function and I can export it as that.
Great.
So now inside the users root, let's require the error class and then the app error function.
So first let's use the error class.
So here we go.
So now we are going to refactor this one a little bit by using the class.
So here we go.
I would like to use the same message.
So I'll cut this one and I remove everything from here.
Remember, because it's a class we need to use new keyword here and then the app error.
And for this one you pass it the message and then the status code as 400.
So now with this one let's send the properties as full name is equal to Aram and then the email as Aram
adds gmail.com and then the password.
It's going to be one, two, three, four, five.
So let's hit send.
And you can see that we have the same message.
But instead you provided our custom status code as 400 and everything remains the same.
So with this one, if I refuse to provide the email, let's see, let's send the requests.
And you can see that we have the error message from MongoDB.
So this is one way of using it.
Or if we want to use the function to why not, we can go ahead and then use it.
So let's see how we can use the function or so for that one.
We are going to remove the new and use the function and everything remains the same.
So let's save it.
And now let's make the request.
And you can see everything remains the same.
So let's bring back the email also and let's check and here you go.
User already exists, but just so we can improve the code a little bit.
But I think this one is okay for our likeness.
So for this case, we need to pass in three arguments because the way we are handling errors in our
application.
So let's look through inside the log in.
Also, we have some use cases here where we are sending a message which is an error.
So here I would like to use the same message here.
So let's remove this one and let's break next.
And for the next I'm going to use the error plus and that app error.
Then I'll pass in the message and then the status code ask 400.
And for this one too, we are going to use the same error case and down here we are going to use next
and then I'm going to pass in the error.
I start and there we go.