1
00:00:07,790 --> 00:00:15,860
Before we continue to implement the rest of the functionalities for our API, let's handle any errors

2
00:00:15,860 --> 00:00:18,740
that might occur in our application.

3
00:00:18,860 --> 00:00:26,330
If you have taken some of my courses, you can see that I use a third party package like Express Async

4
00:00:26,330 --> 00:00:27,710
Handler to do that.

5
00:00:27,830 --> 00:00:32,750
But for this we are going to create our custom error handler from scratch.

6
00:00:32,750 --> 00:00:36,200
So let's get going inside the server file.

7
00:00:36,200 --> 00:00:42,590
You can see we have a comment here called error handlers and you need to create your error handling

8
00:00:42,590 --> 00:00:44,180
below all your route.

9
00:00:44,180 --> 00:00:48,380
And experts make it pretty easy to be able to handle errors.

10
00:00:48,470 --> 00:00:56,750
We bring in app dot use and for this we pass in a callback function as that and inside the callback

11
00:00:56,750 --> 00:00:59,750
function we have access to four domains.

12
00:00:59,750 --> 00:01:05,209
The first one is the error request response and then next.

13
00:01:05,209 --> 00:01:13,070
So in the error that might work in our application, we have access on this object core error and the

14
00:01:13,070 --> 00:01:19,850
request contains the request object and then the response contains the response object and then the

15
00:01:19,850 --> 00:01:21,170
next function.

16
00:01:21,170 --> 00:01:27,620
So in any of our route, if we throw an error, we are going to have access to that error inside this

17
00:01:27,620 --> 00:01:28,430
object.

18
00:01:28,670 --> 00:01:31,880
On the error object, we have these properties.

19
00:01:31,880 --> 00:01:35,690
One is the actual message about the error.

20
00:01:35,720 --> 00:01:38,930
The next property is what we call the status.

21
00:01:38,930 --> 00:01:43,280
And then after that we also have what is called the status code.

22
00:01:45,470 --> 00:01:49,550
And lastly, we also have what is called the stack.

23
00:01:49,580 --> 00:01:56,810
The stack determines which file that a particular error OC and what line of code that that error.

24
00:01:57,470 --> 00:01:59,960
And I will show you as you move on.

25
00:02:00,140 --> 00:02:07,190
So for any error that might occur in our application, we are going to receive these properties.

26
00:02:07,490 --> 00:02:10,580
So we are going to create a function on its own.

27
00:02:10,580 --> 00:02:17,360
And then we require it and use it inside this app to use because we want to keep our server file with

28
00:02:17,360 --> 00:02:18,390
less code.

29
00:02:18,410 --> 00:02:21,180
So because of that, let's cut this one.

30
00:02:21,200 --> 00:02:27,410
And next is let's go to the folders here and let's create one folder called middleware.

31
00:02:31,410 --> 00:02:35,630
Here we go for the middle, where I'm going to have a couple of them.

32
00:02:35,640 --> 00:02:45,360
But for now, let's create one for error handling and I'll call this one as global error handler dot

33
00:02:45,360 --> 00:02:56,100
JS and here our paste what I copied and I need to assign to a function called context as global error

34
00:02:56,100 --> 00:02:59,340
handler is equal to that.

35
00:02:59,340 --> 00:03:02,130
So how are we going to send these messages?

36
00:03:02,130 --> 00:03:09,630
So let's see on the error because it's an object, we can add our own properties to the objects.

37
00:03:09,630 --> 00:03:20,340
So first of all, we are going to add a property called status code and then it's equal to error dot

38
00:03:20,370 --> 00:03:21,690
status code.

39
00:03:21,690 --> 00:03:25,020
If there is one, then go ahead and use it.

40
00:03:25,020 --> 00:03:29,580
Otherwise use what is called 500 and next case.

41
00:03:29,580 --> 00:03:32,700
Let's add a property called status.

42
00:03:33,870 --> 00:03:37,950
Equal to error dot status if there is one.

43
00:03:37,980 --> 00:03:39,000
Go ahead and use it.

44
00:03:39,000 --> 00:03:42,090
Otherwise we are going to provide our message.

45
00:03:42,090 --> 00:03:43,060
See error.

46
00:03:43,080 --> 00:03:50,730
It means that when you throw an error and we don't provide these values, we are going to use this default

47
00:03:50,730 --> 00:03:51,540
values.

48
00:03:52,290 --> 00:03:57,080
So next step is let's send the response as rez dot status.

49
00:03:57,090 --> 00:04:07,590
And for this one, we are going to use the error dot status code so we can assign to a variable cost

50
00:04:07,590 --> 00:04:09,510
as status code.

51
00:04:10,580 --> 00:04:18,180
And the same as the status cons as status is equal to that.

52
00:04:18,200 --> 00:04:26,810
So on the rest status, we are going to use the status code in case a user provide upon throwing an

53
00:04:26,810 --> 00:04:27,380
error.

54
00:04:27,380 --> 00:04:33,380
And after that we are going to use the JSON to send the message to the user.

55
00:04:33,380 --> 00:04:34,920
And here we go.

56
00:04:34,940 --> 00:04:37,880
On the status, we're going to use this message.

57
00:04:37,880 --> 00:04:46,370
And then for the message, it's going to be as error dot message.

58
00:04:47,910 --> 00:04:58,680
Or we can even assign to evacuate here as concert message is equal to error dot message.

59
00:04:58,680 --> 00:05:01,020
So here I can remove this one.

60
00:05:02,010 --> 00:05:06,420
Then this property that we want to send is going to be the stock trace.

61
00:05:06,420 --> 00:05:16,680
So we call this one as stuck and we have access to that on the error dot stock and our pass in here

62
00:05:16,680 --> 00:05:18,420
as the stock.

63
00:05:18,870 --> 00:05:28,020
So the next is let's export this middleware from this file as equal to global arrow handler.

64
00:05:28,290 --> 00:05:35,280
So inside our server file, let's go ahead and then require that middleware as global arrow handler.

65
00:05:35,280 --> 00:05:37,830
And here we go.

66
00:05:37,860 --> 00:05:38,550
Perfect.

67
00:05:38,550 --> 00:05:44,520
So now let's pass it inside this arrow middleware here, and that is it.

68
00:05:44,520 --> 00:05:51,840
Now we have configured our global middleware to receive any error that we are going to throw with this

69
00:05:51,840 --> 00:05:52,830
implementation.

70
00:05:52,830 --> 00:05:57,150
I can now throw or catch any error that might be OC.

71
00:05:57,150 --> 00:06:00,420
So let's get to the controllers and let's check it out.

72
00:06:00,420 --> 00:06:05,520
So let's go to the user's controllers and here we go.

73
00:06:05,670 --> 00:06:13,200
You can see that here we are checking if a user is found and if either is found, it means that this

74
00:06:13,200 --> 00:06:14,250
email exists.

75
00:06:14,250 --> 00:06:15,030
In that way.

76
00:06:15,030 --> 00:06:19,970
We are sending this arrow, but this is not the right way of sending arrow.

77
00:06:19,980 --> 00:06:23,970
That's why we have configured our global arrow handler.

78
00:06:24,120 --> 00:06:28,110
So let's see how we are going to throw an arrow to achieve that.

79
00:06:28,110 --> 00:06:31,740
We are going to pass next to the handler as that.

80
00:06:31,740 --> 00:06:37,350
So any error that might work, we are going to pass through the next middleware and then the next middleware

81
00:06:37,350 --> 00:06:40,110
will not handle to our global arrow handler.

82
00:06:40,110 --> 00:06:41,850
So let's check it out.

83
00:06:42,060 --> 00:06:45,630
So here, if a user is found, let's throw an arrow.

84
00:06:45,630 --> 00:06:48,270
So I would like to use this same message.

85
00:06:48,270 --> 00:06:55,290
But for this, I'm going to cut everything from here and let me break my cable here.

86
00:06:55,830 --> 00:07:01,950
So here we are going to connect and then you are going to pass in an arrow and together one are going

87
00:07:01,950 --> 00:07:04,740
to create instance of the error class.

88
00:07:04,740 --> 00:07:10,950
And for this we pass in the message, remember, we need to pass in the message and then the status

89
00:07:10,950 --> 00:07:13,920
code and then the status as well.

90
00:07:13,980 --> 00:07:17,760
But for this, let's pass in this message as that.

91
00:07:17,760 --> 00:07:21,240
So let's remove this one, let's save it.

92
00:07:21,240 --> 00:07:22,950
And now let's check it out.

93
00:07:22,950 --> 00:07:28,410
So inside the tender client, this user has been registered already.

94
00:07:28,410 --> 00:07:35,580
So as soon as I hit send and there we go, this is a valid error case.

95
00:07:35,670 --> 00:07:43,980
And in that we have a status code here which is 500 meaning internal server error and this is a message

96
00:07:43,980 --> 00:07:48,190
or the properties that we pass into the global arrow handler.

97
00:07:48,270 --> 00:07:51,060
The first one is a status as we specify.

98
00:07:51,060 --> 00:07:54,810
Now let me show you where it is inside the middleware.

99
00:07:54,840 --> 00:07:56,880
This is where we are referring to.

100
00:07:56,880 --> 00:08:02,190
So if a user refuses to pass in the status, we are going to use this one.

101
00:08:02,310 --> 00:08:05,520
And the next one is the message.

102
00:08:05,520 --> 00:08:10,590
And the message is what we pass in upon calling next as that.

103
00:08:10,590 --> 00:08:17,520
And then this one going to be the stack trace, which is this one and here is the stack trace.

104
00:08:17,520 --> 00:08:24,510
And for a stack trace it gives us more details about the error, where it works and which file.

105
00:08:24,540 --> 00:08:31,950
So if you read the file, this is where it works on the user controller line number 11, that is where

106
00:08:31,950 --> 00:08:33,270
the error occurred.

107
00:08:33,270 --> 00:08:42,929
So in our route we can throw any arrow and also we can catch any error that might be OC inside the cache

108
00:08:42,929 --> 00:08:43,710
block.

109
00:08:43,710 --> 00:08:47,910
So let's see for the registration, let me collapse this one.

110
00:08:47,910 --> 00:08:54,120
And before I continue, we need to return from this function so that the function will exit.

111
00:08:54,150 --> 00:08:54,630
Good.

112
00:08:54,630 --> 00:09:01,650
So the next point is that inside the cache we can code next and pass in the error.

113
00:09:01,650 --> 00:09:03,600
So now here we go.

114
00:09:03,630 --> 00:09:11,580
We call next and next is we pass a new and then the error class and you pass in the entire error.

115
00:09:11,580 --> 00:09:17,490
So now we are also handling any error that might be okay from MongoDB.

116
00:09:17,520 --> 00:09:24,690
So let's go ahead and then remove these properties and now let's hit st and let's see the error and

117
00:09:24,690 --> 00:09:31,740
this is a message we have which is status quo 200 main everything is okay, meaning that where we are

118
00:09:31,740 --> 00:09:38,400
throwing this error, which is this part, meaning that a user needs to provide a valid data so this

119
00:09:38,400 --> 00:09:40,320
is the right way of handling errors.

120
00:09:40,320 --> 00:09:43,050
So let's remove this one now.

121
00:09:43,050 --> 00:09:45,420
Let's go ahead and make the request again.

122
00:09:45,420 --> 00:09:46,860
And let's send and.

123
00:09:47,020 --> 00:09:48,130
Here we go.

124
00:09:48,430 --> 00:09:50,690
And this is a status code 500.

125
00:09:50,710 --> 00:09:53,590
Which means that is a valid error case.

126
00:09:54,490 --> 00:09:57,440
Let's also handle this arrow nicely.

127
00:09:57,460 --> 00:10:00,070
So I would like to use the same message.

128
00:10:00,070 --> 00:10:10,030
So here I'm going to remove the response and I'm going to bring in the next middleware and pass in new.

129
00:10:10,030 --> 00:10:11,580
And then the error.

130
00:10:11,590 --> 00:10:14,020
Then I'll pass in the message.

131
00:10:14,710 --> 00:10:15,660
All right.

132
00:10:15,670 --> 00:10:17,470
But we have one problem.

133
00:10:17,470 --> 00:10:24,060
We can see that any time you want to send an arrow, we need to use new arrow than it has in the message,

134
00:10:24,070 --> 00:10:27,020
and we can pass in the status code as well.

135
00:10:27,040 --> 00:10:32,590
So we can refactor this one by creating a class or a function for this.

136
00:10:32,590 --> 00:10:34,480
So let's get that one done.

137
00:10:34,960 --> 00:10:40,600
So inside the utils, you are going to create a file called App Error.

138
00:10:43,170 --> 00:10:48,900
Dark jazz, and we can create a class or a function for this.

139
00:10:48,990 --> 00:10:56,820
For the meantime, let me show you how we are going to create a class for that as app error, and then

140
00:10:56,820 --> 00:11:02,220
we are going to extend the error class from JavaScript as that.

141
00:11:02,880 --> 00:11:06,930
And inside here we are going to call the constructor function.

142
00:11:06,930 --> 00:11:11,220
Then you pass in the message and then the status code.

143
00:11:13,420 --> 00:11:14,080
Great.

144
00:11:14,080 --> 00:11:18,690
And inside that we are going to call support and you pass in the message.

145
00:11:18,700 --> 00:11:27,610
And lastly, we need to add the status code as this DOT status code is equal to the status code.

146
00:11:27,610 --> 00:11:33,090
And then for the status tool, we can add as failed or error.

147
00:11:33,100 --> 00:11:34,240
So here we go.

148
00:11:34,240 --> 00:11:42,670
This dot status is equal to fail grade.

149
00:11:42,700 --> 00:11:49,930
Now this is our error class, so I can go ahead and then export this one as module dot export.

150
00:11:53,800 --> 00:11:54,940
For this one to.

151
00:11:54,940 --> 00:11:58,390
I can create a function for this one instead of using a class.

152
00:11:58,390 --> 00:12:03,360
So if you're not familiar with classes in JavaScript, then you can use the function way.

153
00:12:03,370 --> 00:12:04,510
So here we go.

154
00:12:04,510 --> 00:12:12,640
Const as app error is equal to a function and this function are going to pass in the message and then

155
00:12:12,640 --> 00:12:16,630
they start as code and here we go.

156
00:12:16,750 --> 00:12:25,750
So we're going to create an instance of error for JavaScript error class as equal to name and error.

157
00:12:25,750 --> 00:12:27,880
Then you pass in the message.

158
00:12:27,880 --> 00:12:32,860
Remember, the error class accepts a single core message.

159
00:12:32,860 --> 00:12:38,320
That's why we pass in the message to the super of our class.

160
00:12:38,710 --> 00:12:42,610
Then on the object I can add our custom messages to it.

161
00:12:42,820 --> 00:12:52,270
So what we want to add is going to be the status code as error dot status code is equal to status code.

162
00:12:52,270 --> 00:12:54,640
So let me change this one to let.

163
00:12:55,090 --> 00:12:55,540
All right.

164
00:12:55,540 --> 00:13:03,640
So lastly, I need to retain the entire error object from this function and I can export it as that.

165
00:13:03,880 --> 00:13:04,450
Great.

166
00:13:04,450 --> 00:13:12,670
So now inside the users root, let's require the error class and then the app error function.

167
00:13:13,210 --> 00:13:16,120
So first let's use the error class.

168
00:13:16,120 --> 00:13:17,590
So here we go.

169
00:13:17,590 --> 00:13:22,750
So now we are going to refactor this one a little bit by using the class.

170
00:13:22,750 --> 00:13:24,460
So here we go.

171
00:13:24,550 --> 00:13:26,590
I would like to use the same message.

172
00:13:26,590 --> 00:13:30,880
So I'll cut this one and I remove everything from here.

173
00:13:31,300 --> 00:13:37,030
Remember, because it's a class we need to use new keyword here and then the app error.

174
00:13:37,030 --> 00:13:43,660
And for this one you pass it the message and then the status code as 400.

175
00:13:43,660 --> 00:13:57,510
So now with this one let's send the properties as full name is equal to Aram and then the email as Aram

176
00:13:57,880 --> 00:14:02,950
adds gmail.com and then the password.

177
00:14:05,090 --> 00:14:07,100
It's going to be one, two, three, four, five.

178
00:14:07,120 --> 00:14:08,440
So let's hit send.

179
00:14:08,680 --> 00:14:11,890
And you can see that we have the same message.

180
00:14:11,890 --> 00:14:18,700
But instead you provided our custom status code as 400 and everything remains the same.

181
00:14:18,970 --> 00:14:26,530
So with this one, if I refuse to provide the email, let's see, let's send the requests.

182
00:14:26,770 --> 00:14:31,630
And you can see that we have the error message from MongoDB.

183
00:14:31,660 --> 00:14:34,120
So this is one way of using it.

184
00:14:34,120 --> 00:14:39,460
Or if we want to use the function to why not, we can go ahead and then use it.

185
00:14:39,580 --> 00:14:43,720
So let's see how we can use the function or so for that one.

186
00:14:43,720 --> 00:14:49,630
We are going to remove the new and use the function and everything remains the same.

187
00:14:49,660 --> 00:14:50,920
So let's save it.

188
00:14:50,920 --> 00:14:53,140
And now let's make the request.

189
00:14:53,290 --> 00:14:57,150
And you can see everything remains the same.

190
00:14:57,160 --> 00:15:02,240
So let's bring back the email also and let's check and here you go.

191
00:15:02,260 --> 00:15:07,810
User already exists, but just so we can improve the code a little bit.

192
00:15:07,810 --> 00:15:10,840
But I think this one is okay for our likeness.

193
00:15:10,840 --> 00:15:16,960
So for this case, we need to pass in three arguments because the way we are handling errors in our

194
00:15:16,960 --> 00:15:17,800
application.

195
00:15:18,010 --> 00:15:21,250
So let's look through inside the log in.

196
00:15:21,250 --> 00:15:27,020
Also, we have some use cases here where we are sending a message which is an error.

197
00:15:27,040 --> 00:15:31,210
So here I would like to use the same message here.

198
00:15:31,270 --> 00:15:35,110
So let's remove this one and let's break next.

199
00:15:35,110 --> 00:15:40,000
And for the next I'm going to use the error plus and that app error.

200
00:15:40,030 --> 00:15:46,750
Then I'll pass in the message and then the status code ask 400.

201
00:15:46,780 --> 00:15:53,740
And for this one too, we are going to use the same error case and down here we are going to use next

202
00:15:53,740 --> 00:15:56,350
and then I'm going to pass in the error.

203
00:15:58,910 --> 00:16:01,880
I start and there we go.

