In this video.
Let's see how we can catch any error that might occur inside our route.
In a previous video, we were able to catch any error that might occur outside our route, and that
is for for error handling.
So let's forecast the kind of error that might occur inside any route before we continue.
Let's create one more dummy route and this one going to be profile.
So here I'll change this one.
To slash profile is a get request.
And this one also adds profile.
And welcome to your.
Profile page.
So what you are going to do is that inside the profile route here, we are going to check if a user
is logged in.
If you are logged in, then you are going to display this message to you.
Otherwise, we are going to send a message by saying that please log in first.
So how are you going to implement that?
Well, we are going to mimic real log in system here.
So let's see how we are going to implement that.
Let's assume that we are getting this user from database so we can say that let's is logged in equal
to true.
So let's check if the user is not logged in.
So here is the logic.
If it's not logged in, then we are going to return from this function by passing in a new error message
to the error constructor function.
I'll start and we will say that please.
Look in first.
So now let's see back to the request.
Now I'm going to make use of slash profile and hit send.
And you can see that when come to your profile page.
It simply means that it's logged in is true.
So let's change this one to false.
Now, here we go.
Let's try to make the request.
And now you can see that there is no response, meaning that the user is not log in.
So this message is not returning.
So the question is, how can we have access to this message?
That is why we have next here.
So anything that we pass into next, then Express was regarded as an error and then it's going to give
it to the built in error handler.
So now instead of this returning something like this, we are going to pass in the entire message to
next.
So let's cut this message and then we bring in next and then we paste in our message as that when we
save it.
Now let's make the request again.
And this time around we have something incredible, meaning that place logged in because we have through
an error inside our route where we pass to the next and next will pass into the built in error handler.
And that's why we have this beautiful message.
But we can see this is almost the same as the one we did before.
But this time around it is not full.
Oh four, but instead is a logic that is inside our route.
So how can we have access to this error?
And then we can send a beautiful message.
That is why Express has given us a global error handling method.
So for us to send beautiful message or custom message, we are going to use this global.
Error handling.
Middle.
Well, and here it goes.
We bring in app dot use and for use you pass in the callback function and for the callback function
we have access to error, the request objects, the response and then the next.
So the error argument contains the error that we pass in to next, and then the request contains our
request object likewise response.
And then next.
So any time Express sees this kind of middleware, it means that we are trying to catch an error.
So if you try to console the log, the error here, now let's check our terminal here and let's make
the requests and let's send this time around, we can see that we have an error code place logged in.
So how can we send a beautiful message for sending response?
We need to send at least this three properties.
One is the status code of the response.
The next property that we want to send to the user is what we call the status.
So this one, we can provide our custom message, for example, failed or success.
So this one is up to us.
And lastly is the actual message that we want to send for status code.
We can send a message something like four or four.
So any response that we send to the user, whether success or failure, try to send these three properties.
So we are going to send this property to the user, but for the message, it's going to be an error.
But before we continue, we have access to the error, which is this one on a property called message.
So let me show you before we continue.
So if you console.log the error dot.
Darts message.
Now, if you save it now, let's check our terminal here and let's make the requests.
Now you can see that we have the message called Please Locked in so we can use that one as a message.
So now inside our error handling here, let's try to send a beautiful message.
So here we go.
So let's go ahead and send the response to the user.
The first property you want to send is what we call the status code that is status like that, and then
we pass the code.
So now here comes how will I know the kind of code we want to send?
This website contains all the status code with it meaning?
And I'll provide for you and for this it has been categorized and our information, our successful redirection
client error and server error for server error is start from five and then for client error is start
from four and then for redirection is start from three.
Likewise successful that is two and then information that is one here.
So what we want is what you call client error, which means unauthorized or bad requests or forbidden.
So we can use any of this one, but let's use 401, which means what?
Unauthorized.
So now let's get back to the error handler here and here you provide in here as 401 and then the next
one going to be the JSON.
And for this we can pass in any message.
But what we want right now is the status.
So here we provide status.
It can be anything, the property.
And you say that failed.
And then lastly, it's going to be the actual message and here we go is going to be as word error.
And we have access to the message on the property code message, which is the this one.
So with this one, we are all set.
So let's go ahead and then make the request again.
Now have a look.
This time around we have something incredible failed.
Please log in.
So any message that I'll pass in to this one, I'm going to receive it so I can change this one to you
are not logged in.
So again, let's save it and make the request.
And again, you can see that you are not logged in.
So inside the home root, let's say that we have some network issues.
So we say network is live and by default I would say false.
And then we can say that if there is no network, then we can go ahead, return from this function and
then we pass in the error.
You would say that no network, save it and let's make the requests where you can see that there is
no message simply because I need to pass the error to the error class.
So we're going to be name error and then I'll pass in the error.
This time around, you're going to have access to the error and here we go.
Good.
So that is about it.
But guys, look at this one.
We still have what is called unauthorized for or for, even though this one is not authorization but
network issue.
So how can we send a specific status code based on the type of error?
So in the next video, we are going to modify this one a little bit so that it can accept any type of
message with different type of status code.
Where I made some typing error here is supposed to be global.
All right.
So let's go ahead and then modify this middleware in the next video.