1
00:00:00,300 --> 00:00:01,500
In this video.

2
00:00:01,500 --> 00:00:08,160
Let's see how we can catch any error that might occur inside our route.

3
00:00:08,280 --> 00:00:15,630
In a previous video, we were able to catch any error that might occur outside our route, and that

4
00:00:15,630 --> 00:00:18,000
is for for error handling.

5
00:00:18,090 --> 00:00:25,370
So let's forecast the kind of error that might occur inside any route before we continue.

6
00:00:25,410 --> 00:00:30,870
Let's create one more dummy route and this one going to be profile.

7
00:00:31,260 --> 00:00:33,150
So here I'll change this one.

8
00:00:33,150 --> 00:00:36,480
To slash profile is a get request.

9
00:00:36,480 --> 00:00:39,960
And this one also adds profile.

10
00:00:39,960 --> 00:00:42,000
And welcome to your.

11
00:00:42,750 --> 00:00:44,820
Profile page.

12
00:00:45,210 --> 00:00:53,940
So what you are going to do is that inside the profile route here, we are going to check if a user

13
00:00:53,940 --> 00:00:55,230
is logged in.

14
00:00:55,230 --> 00:01:00,420
If you are logged in, then you are going to display this message to you.

15
00:01:00,600 --> 00:01:06,030
Otherwise, we are going to send a message by saying that please log in first.

16
00:01:06,030 --> 00:01:08,270
So how are you going to implement that?

17
00:01:08,280 --> 00:01:12,270
Well, we are going to mimic real log in system here.

18
00:01:12,270 --> 00:01:14,790
So let's see how we are going to implement that.

19
00:01:14,820 --> 00:01:22,860
Let's assume that we are getting this user from database so we can say that let's is logged in equal

20
00:01:22,860 --> 00:01:23,940
to true.

21
00:01:24,510 --> 00:01:28,050
So let's check if the user is not logged in.

22
00:01:28,050 --> 00:01:30,120
So here is the logic.

23
00:01:30,240 --> 00:01:39,660
If it's not logged in, then we are going to return from this function by passing in a new error message

24
00:01:39,660 --> 00:01:42,180
to the error constructor function.

25
00:01:42,180 --> 00:01:45,900
I'll start and we will say that please.

26
00:01:46,870 --> 00:01:48,930
Look in first.

27
00:01:48,940 --> 00:01:51,880
So now let's see back to the request.

28
00:01:51,910 --> 00:01:57,450
Now I'm going to make use of slash profile and hit send.

29
00:01:57,460 --> 00:02:01,240
And you can see that when come to your profile page.

30
00:02:01,270 --> 00:02:05,140
It simply means that it's logged in is true.

31
00:02:05,740 --> 00:02:09,280
So let's change this one to false.

32
00:02:09,550 --> 00:02:11,450
Now, here we go.

33
00:02:11,470 --> 00:02:13,690
Let's try to make the request.

34
00:02:13,690 --> 00:02:20,290
And now you can see that there is no response, meaning that the user is not log in.

35
00:02:20,290 --> 00:02:23,100
So this message is not returning.

36
00:02:23,110 --> 00:02:27,820
So the question is, how can we have access to this message?

37
00:02:27,880 --> 00:02:30,850
That is why we have next here.

38
00:02:31,000 --> 00:02:39,790
So anything that we pass into next, then Express was regarded as an error and then it's going to give

39
00:02:39,790 --> 00:02:42,190
it to the built in error handler.

40
00:02:42,370 --> 00:02:48,820
So now instead of this returning something like this, we are going to pass in the entire message to

41
00:02:48,820 --> 00:02:49,500
next.

42
00:02:49,510 --> 00:02:57,580
So let's cut this message and then we bring in next and then we paste in our message as that when we

43
00:02:57,580 --> 00:02:58,270
save it.

44
00:02:58,270 --> 00:03:01,060
Now let's make the request again.

45
00:03:01,060 --> 00:03:09,340
And this time around we have something incredible, meaning that place logged in because we have through

46
00:03:09,340 --> 00:03:16,740
an error inside our route where we pass to the next and next will pass into the built in error handler.

47
00:03:16,750 --> 00:03:20,430
And that's why we have this beautiful message.

48
00:03:20,440 --> 00:03:25,300
But we can see this is almost the same as the one we did before.

49
00:03:25,300 --> 00:03:27,370
But this time around it is not full.

50
00:03:27,400 --> 00:03:32,380
Oh four, but instead is a logic that is inside our route.

51
00:03:32,380 --> 00:03:35,440
So how can we have access to this error?

52
00:03:35,440 --> 00:03:38,010
And then we can send a beautiful message.

53
00:03:38,020 --> 00:03:43,840
That is why Express has given us a global error handling method.

54
00:03:43,840 --> 00:03:49,630
So for us to send beautiful message or custom message, we are going to use this global.

55
00:03:50,750 --> 00:03:52,340
Error handling.

56
00:03:53,290 --> 00:03:54,220
Middle.

57
00:03:55,720 --> 00:03:58,110
Well, and here it goes.

58
00:03:58,120 --> 00:04:06,340
We bring in app dot use and for use you pass in the callback function and for the callback function

59
00:04:06,340 --> 00:04:13,150
we have access to error, the request objects, the response and then the next.

60
00:04:13,390 --> 00:04:21,190
So the error argument contains the error that we pass in to next, and then the request contains our

61
00:04:21,190 --> 00:04:23,800
request object likewise response.

62
00:04:23,800 --> 00:04:25,300
And then next.

63
00:04:25,300 --> 00:04:31,840
So any time Express sees this kind of middleware, it means that we are trying to catch an error.

64
00:04:31,870 --> 00:04:41,410
So if you try to console the log, the error here, now let's check our terminal here and let's make

65
00:04:41,410 --> 00:04:49,090
the requests and let's send this time around, we can see that we have an error code place logged in.

66
00:04:49,090 --> 00:04:53,740
So how can we send a beautiful message for sending response?

67
00:04:53,740 --> 00:04:57,310
We need to send at least this three properties.

68
00:04:57,310 --> 00:05:01,150
One is the status code of the response.

69
00:05:01,270 --> 00:05:06,880
The next property that we want to send to the user is what we call the status.

70
00:05:06,880 --> 00:05:15,010
So this one, we can provide our custom message, for example, failed or success.

71
00:05:15,220 --> 00:05:17,170
So this one is up to us.

72
00:05:17,170 --> 00:05:22,630
And lastly is the actual message that we want to send for status code.

73
00:05:22,630 --> 00:05:25,840
We can send a message something like four or four.

74
00:05:26,110 --> 00:05:33,340
So any response that we send to the user, whether success or failure, try to send these three properties.

75
00:05:33,340 --> 00:05:39,400
So we are going to send this property to the user, but for the message, it's going to be an error.

76
00:05:39,490 --> 00:05:48,520
But before we continue, we have access to the error, which is this one on a property called message.

77
00:05:48,520 --> 00:05:50,740
So let me show you before we continue.

78
00:05:50,770 --> 00:05:55,960
So if you console.log the error dot.

79
00:05:57,150 --> 00:05:58,740
Darts message.

80
00:05:58,770 --> 00:06:04,260
Now, if you save it now, let's check our terminal here and let's make the requests.

81
00:06:05,680 --> 00:06:14,250
Now you can see that we have the message called Please Locked in so we can use that one as a message.

82
00:06:14,260 --> 00:06:20,200
So now inside our error handling here, let's try to send a beautiful message.

83
00:06:20,200 --> 00:06:21,910
So here we go.

84
00:06:21,910 --> 00:06:25,840
So let's go ahead and send the response to the user.

85
00:06:25,870 --> 00:06:33,220
The first property you want to send is what we call the status code that is status like that, and then

86
00:06:33,220 --> 00:06:34,900
we pass the code.

87
00:06:34,900 --> 00:06:39,910
So now here comes how will I know the kind of code we want to send?

88
00:06:40,180 --> 00:06:44,860
This website contains all the status code with it meaning?

89
00:06:44,860 --> 00:06:55,300
And I'll provide for you and for this it has been categorized and our information, our successful redirection

90
00:06:55,840 --> 00:07:04,990
client error and server error for server error is start from five and then for client error is start

91
00:07:04,990 --> 00:07:10,030
from four and then for redirection is start from three.

92
00:07:10,030 --> 00:07:15,640
Likewise successful that is two and then information that is one here.

93
00:07:15,640 --> 00:07:23,530
So what we want is what you call client error, which means unauthorized or bad requests or forbidden.

94
00:07:23,530 --> 00:07:27,970
So we can use any of this one, but let's use 401, which means what?

95
00:07:27,970 --> 00:07:29,140
Unauthorized.

96
00:07:29,140 --> 00:07:37,030
So now let's get back to the error handler here and here you provide in here as 401 and then the next

97
00:07:37,030 --> 00:07:38,650
one going to be the JSON.

98
00:07:38,650 --> 00:07:41,500
And for this we can pass in any message.

99
00:07:41,500 --> 00:07:44,230
But what we want right now is the status.

100
00:07:44,230 --> 00:07:46,360
So here we provide status.

101
00:07:46,360 --> 00:07:48,820
It can be anything, the property.

102
00:07:50,400 --> 00:07:52,980
And you say that failed.

103
00:07:53,190 --> 00:08:02,370
And then lastly, it's going to be the actual message and here we go is going to be as word error.

104
00:08:02,490 --> 00:08:10,260
And we have access to the message on the property code message, which is the this one.

105
00:08:10,260 --> 00:08:13,370
So with this one, we are all set.

106
00:08:13,380 --> 00:08:16,230
So let's go ahead and then make the request again.

107
00:08:16,260 --> 00:08:17,490
Now have a look.

108
00:08:17,520 --> 00:08:21,660
This time around we have something incredible failed.

109
00:08:21,660 --> 00:08:23,070
Please log in.

110
00:08:23,250 --> 00:08:31,560
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

111
00:08:31,560 --> 00:08:34,620
are not logged in.

112
00:08:34,620 --> 00:08:37,530
So again, let's save it and make the request.

113
00:08:37,530 --> 00:08:40,500
And again, you can see that you are not logged in.

114
00:08:40,620 --> 00:08:44,490
So inside the home root, let's say that we have some network issues.

115
00:08:44,490 --> 00:08:51,240
So we say network is live and by default I would say false.

116
00:08:51,570 --> 00:08:59,970
And then we can say that if there is no network, then we can go ahead, return from this function and

117
00:08:59,970 --> 00:09:02,310
then we pass in the error.

118
00:09:02,340 --> 00:09:08,400
You would say that no network, save it and let's make the requests where you can see that there is

119
00:09:08,400 --> 00:09:13,280
no message simply because I need to pass the error to the error class.

120
00:09:13,280 --> 00:09:19,320
So we're going to be name error and then I'll pass in the error.

121
00:09:19,350 --> 00:09:25,290
This time around, you're going to have access to the error and here we go.

122
00:09:25,380 --> 00:09:26,130
Good.

123
00:09:26,130 --> 00:09:27,450
So that is about it.

124
00:09:27,450 --> 00:09:29,130
But guys, look at this one.

125
00:09:29,160 --> 00:09:36,990
We still have what is called unauthorized for or for, even though this one is not authorization but

126
00:09:36,990 --> 00:09:38,370
network issue.

127
00:09:38,960 --> 00:09:44,210
So how can we send a specific status code based on the type of error?

128
00:09:44,540 --> 00:09:52,550
So in the next video, we are going to modify this one a little bit so that it can accept any type of

129
00:09:52,550 --> 00:09:55,460
message with different type of status code.

130
00:09:55,700 --> 00:09:59,960
Where I made some typing error here is supposed to be global.

131
00:10:00,170 --> 00:10:00,560
All right.

132
00:10:00,560 --> 00:10:04,790
So let's go ahead and then modify this middleware in the next video.

