1
00:00:00,390 --> 00:00:07,650
And right after we set up our customer handler, let's work on our full of responses to and essentially

2
00:00:07,650 --> 00:00:13,800
I want to set up a custom class which extends from the general JavaScript class.

3
00:00:14,250 --> 00:00:21,450
And that way we can handle all of our four or four responses in our newly created error handler as well.

4
00:00:22,140 --> 00:00:29,700
And we're going to start by navigating back through the controllers and the routes or controllers that

5
00:00:29,700 --> 00:00:31,320
I'm looking for are following.

6
00:00:31,530 --> 00:00:38,040
I want to work on update task here and we send the four or four then the same is for deleting the task

7
00:00:38,400 --> 00:00:40,370
and of course, getting task.

8
00:00:40,830 --> 00:00:42,390
And let's just start over here.

9
00:00:42,780 --> 00:00:49,800
We have getting the task and simply I want to showcase that we can create a new object if we run the

10
00:00:49,800 --> 00:00:56,280
built in JavaScript error constructor and we go here with no surprise, I'm going to call this error

11
00:00:56,490 --> 00:00:57,960
and that is equal to new.

12
00:00:58,350 --> 00:00:59,730
And then we're just go with error.

13
00:01:00,150 --> 00:01:04,739
And here we pass in the message so I can say not found.

14
00:01:05,340 --> 00:01:07,770
And when the next line, I'm just going to go with error.

15
00:01:08,130 --> 00:01:14,070
So now I'm dealing with the object that I just created and I'll set another property on it and I'll

16
00:01:14,070 --> 00:01:14,960
say status.

17
00:01:14,970 --> 00:01:16,260
So that's the name of the property.

18
00:01:16,530 --> 00:01:19,830
And of course, the value will be for all four.

19
00:01:20,250 --> 00:01:20,820
So now.

20
00:01:21,730 --> 00:01:31,900
If you want, you can console it here or we can simply do is use next and pass this guy down to our

21
00:01:32,080 --> 00:01:33,700
custom, our handler.

22
00:01:34,270 --> 00:01:35,750
Remember, that's how it works.

23
00:01:36,010 --> 00:01:39,070
We call next and then we pass in the air.

24
00:01:39,070 --> 00:01:45,370
And of course, in this case, we are creating that air ourselves instead of getting it from the Mongo's.

25
00:01:45,790 --> 00:01:52,060
And the only thing that's missing right now is in our controller because we don't have the next.

26
00:01:52,330 --> 00:01:56,900
So I want to go here with next and then we need to go with return.

27
00:01:56,920 --> 00:02:02,920
Again, I already mentioned why we want to do that, because we don't want JavaScript to keep reading

28
00:02:02,920 --> 00:02:03,460
the code.

29
00:02:03,580 --> 00:02:05,290
And of course, we have this one right now.

30
00:02:05,290 --> 00:02:11,590
But hopefully you get the gist where we need to go with a return and then next and of course, I want

31
00:02:11,590 --> 00:02:15,580
to pass in my newly created error object.

32
00:02:16,000 --> 00:02:24,190
And once I do that, if we go back over here in the error handler, if we simply console log, we should

33
00:02:24,580 --> 00:02:26,790
see our newly created error object.

34
00:02:27,040 --> 00:02:32,500
So I'm going to go over here with console log and I'll look for my error and I'll let me go back to

35
00:02:32,500 --> 00:02:33,180
the postman.

36
00:02:33,610 --> 00:02:36,670
And essentially what I want to do, I want to get all the tasks.

37
00:02:37,630 --> 00:02:44,200
And then I'll look for one of the ideas, but again, I don't want to get that cast there, so I still

38
00:02:44,200 --> 00:02:46,040
want to keep the same structure.

39
00:02:46,300 --> 00:02:48,370
I just want to pass in the wrong room.

40
00:02:48,730 --> 00:02:50,500
So I'll look for a single task.

41
00:02:50,830 --> 00:02:56,210
I'll copy and paste, and then instead of three, I'm going to go with four.

42
00:02:56,440 --> 00:03:02,940
So once we send, of course, we get our custom error message, as vague as it currently is.

43
00:03:03,310 --> 00:03:08,490
And then, of course, in council, I can clearly see that I have not found.

44
00:03:08,710 --> 00:03:10,510
So of course, that is going to be my message.

45
00:03:10,810 --> 00:03:13,600
And I understand that might be a bit confusing.

46
00:03:13,600 --> 00:03:19,750
But effectively, the idea is following where once we create this new object, that property is going

47
00:03:19,750 --> 00:03:20,380
to be on there.

48
00:03:20,650 --> 00:03:26,410
So in order to access the string, value will have to go here and simply go with error.

49
00:03:26,410 --> 00:03:27,700
And then that message.

50
00:03:28,030 --> 00:03:32,860
And as far as the status, well, we can clearly see we're going to have four or four.

51
00:03:33,130 --> 00:03:40,630
So now just for kicks, what are we can do is set up message is equal to error message.

52
00:03:40,640 --> 00:03:43,450
Like I said, there's the property on an object.

53
00:03:43,630 --> 00:03:48,820
And then as far as the status, well, I can simply change this one around where instead of five hundred

54
00:03:48,970 --> 00:03:52,240
I'll pass in error and then status and the worry.

55
00:03:52,250 --> 00:03:53,890
Of course there's more code coming.

56
00:03:54,130 --> 00:04:00,060
And once I have these changes of course we can swing back to our postman and check it out.

57
00:04:00,400 --> 00:04:07,420
Now of course we have for four and the message is not found, so that's pretty cool.

58
00:04:07,870 --> 00:04:14,920
Now of course we just need to go back to the tasks and set it up in such a way where we don't have to

59
00:04:14,920 --> 00:04:16,589
do this all the time manually.

60
00:04:16,959 --> 00:04:24,780
And the idea is following where again will create a new class, a new custom class, and we'll extend

61
00:04:24,790 --> 00:04:31,560
it from the JavaScript error and then we'll create a new instance and yada, yada.

62
00:04:32,050 --> 00:04:35,740
And effectively, the way we want to do that, we want to create a new folder.

63
00:04:35,980 --> 00:04:39,610
And I'll call these errors pretty self-explanatory.

64
00:04:39,610 --> 00:04:43,900
So we're going to go here with new folders and we're going to go with errors.

65
00:04:43,900 --> 00:04:46,870
And inside of it, I'm going to create another new file.

66
00:04:47,320 --> 00:04:51,490
And in this case, I'm going to call this custom and hyphen error.

67
00:04:51,760 --> 00:04:54,340
And of course, I'm going to add the extension.

68
00:04:54,850 --> 00:05:00,130
And then inside of my custom error now I want to extend from the error class.

69
00:05:00,580 --> 00:05:01,380
Now, here's the deal.

70
00:05:01,390 --> 00:05:06,040
In this case, I'll just write out the code and then I'll go line by line of what's happening, because

71
00:05:06,040 --> 00:05:08,590
I think that's going to be a bit more productive.

72
00:05:08,860 --> 00:05:12,300
So let's go here with class and then we just need to come up with a name.

73
00:05:12,670 --> 00:05:15,370
In my case, I'm going to go with the long one.

74
00:05:15,370 --> 00:05:17,770
I'm going to say custom API.

75
00:05:18,130 --> 00:05:22,030
And now we need to use the extended keyword.

76
00:05:22,210 --> 00:05:25,930
And since I want to extend from the error class, I'm going to go here with error.

77
00:05:26,290 --> 00:05:29,410
And as far as the functionality, we want to go with constructor.

78
00:05:29,710 --> 00:05:39,820
And here I'll pass the message and status code and insert the constructor we want to call super, then

79
00:05:39,940 --> 00:05:41,290
pass in the message.

80
00:05:41,530 --> 00:05:45,400
And right after I call Super, I'm going to go with that status code.

81
00:05:46,590 --> 00:05:53,100
And I'll set that one equal to whatever I'm passing in the constructor Sauniere, I'll pass the and

82
00:05:53,100 --> 00:05:53,870
the status code.

83
00:05:53,880 --> 00:06:00,330
And as far as these few lines of code, a constructor method is a special method we invoke when we create

84
00:06:00,330 --> 00:06:02,370
a new instance of a class.

85
00:06:02,730 --> 00:06:08,250
And in our case, it will pass into arguments and our message and a status code.

86
00:06:08,670 --> 00:06:15,870
Then since we're extending basically we're setting up a child class we need to call super method, which

87
00:06:15,870 --> 00:06:19,830
in turn invokes a constructor of a parent class.

88
00:06:20,190 --> 00:06:22,920
And of course we pass in our message.

89
00:06:22,930 --> 00:06:28,660
Value, as a result will have access to all the methods and properties of the parent.

90
00:06:29,070 --> 00:06:36,300
So in our instance, we have the message probably and with this that status code, we create a status

91
00:06:36,300 --> 00:06:37,770
quo property as well.

92
00:06:37,950 --> 00:06:41,810
And that's how we can create our new custom, our class.

93
00:06:42,060 --> 00:06:47,820
And essentially, if you want to create new instances, you can simply go with custom and then appear

94
00:06:48,030 --> 00:06:52,040
and then pass in those two values the message and status code.

95
00:06:52,350 --> 00:06:55,500
But I actually like to set up a new function that does that for me.

96
00:06:55,920 --> 00:07:00,920
And here I'm going to go with const create custom and error.

97
00:07:01,030 --> 00:07:07,350
And this function is going to be looking for two parameters, a message and a status code.

98
00:07:08,730 --> 00:07:14,670
And what I'm going to do is from this function, I'm going to return that new instance, I'm going to

99
00:07:14,670 --> 00:07:19,860
go with the return new and then we're looking for a custom API error.

100
00:07:20,120 --> 00:07:25,440
And then, of course, we'll pass in the message here as well as the status code.

101
00:07:25,790 --> 00:07:30,660
Of course, if you want to keep the same parameter names, you can definitely do so.

102
00:07:30,670 --> 00:07:35,730
But in my case, I'm purposely setting this up so you can see that this is coming from the function

103
00:07:36,060 --> 00:07:37,010
as far as the message.

104
00:07:37,230 --> 00:07:45,420
And of course, I pass into my custom API or more specifically in the constructor as a message.

105
00:07:45,930 --> 00:07:50,820
And then, of course, we just need to export this and we'll have to export both of them.

106
00:07:50,940 --> 00:07:52,290
And you'll see in a second why.

107
00:07:52,560 --> 00:07:57,390
So we're going to go with module exports is equal to an object, and then we're looking for create custom

108
00:07:57,960 --> 00:08:00,350
and then custom API here.

109
00:08:00,720 --> 00:08:07,260
And once we have both, once we have the class as well as the function and we're exporting both now,

110
00:08:07,260 --> 00:08:09,210
of course, we want to go to the tasks.

111
00:08:09,390 --> 00:08:12,480
We just need to remember that, of course, we're exporting object.

112
00:08:12,870 --> 00:08:16,470
So therefore we'll have to provide a specific name.

113
00:08:16,650 --> 00:08:17,420
In our case.

114
00:08:17,430 --> 00:08:24,180
I'm looking here for the function, so I'm going to go I'll set up my Curley's and I'll say create an

115
00:08:24,420 --> 00:08:26,730
custom error.

116
00:08:26,880 --> 00:08:28,650
So of course that is my function.

117
00:08:28,920 --> 00:08:30,540
So it equal to require.

118
00:08:30,780 --> 00:08:32,820
I do need to go two levels up.

119
00:08:33,299 --> 00:08:38,789
So in here we're looking for errors and then more specifically, I'm looking for the custom one.

120
00:08:39,240 --> 00:08:43,020
And then I want to replace all of these three instances.

121
00:08:43,289 --> 00:08:45,750
And of course I want to remove this manual code as well.

122
00:08:46,120 --> 00:08:52,740
So start over here and I'll keep this one for the second just because I want to use the same message

123
00:08:53,070 --> 00:08:56,580
and the areas following where we go with our return.

124
00:08:58,240 --> 00:09:06,200
Next, and then the next we pass in create custom air, and of course, we want to pass in the message.

125
00:09:06,480 --> 00:09:10,250
So that's why I'll just copy this one just because I want to speed this up.

126
00:09:10,680 --> 00:09:16,260
And then the second thing is going to be my status quo, which, of course, is going to be four or

127
00:09:16,260 --> 00:09:16,530
four.

128
00:09:16,950 --> 00:09:22,010
And now we're going to remove the second line and we just want to copy and paste.

129
00:09:22,560 --> 00:09:24,540
So it's the same deal over here.

130
00:09:25,430 --> 00:09:27,980
And the same in the update desk as well.

131
00:09:28,460 --> 00:09:35,720
And once we have all of this in place now, we just want to go to our handler so our customer handler.

132
00:09:36,050 --> 00:09:40,460
And instead of looking for a function, we're looking for the class.

133
00:09:40,880 --> 00:09:48,770
Because in here, I want to check if the instance is equal to our customer one, then of course, I'll

134
00:09:48,770 --> 00:09:51,500
pass in the status code and the error message.

135
00:09:51,770 --> 00:09:56,230
However, if there is any other error, then of course we'll just go with the status.

136
00:09:56,240 --> 00:09:59,440
Five hundred and whatever we had before.

137
00:09:59,810 --> 00:10:03,500
So let me go at the top and I'm going to go with Consed again.

138
00:10:03,650 --> 00:10:05,030
It is name the import.

139
00:10:05,030 --> 00:10:06,560
So we're looking for custom.

140
00:10:07,440 --> 00:10:14,490
API and error, and that is equal to require and same deal we go to levels up.

141
00:10:14,520 --> 00:10:18,000
We're looking for errors and more specifically, custom error.

142
00:10:18,420 --> 00:10:20,940
And let's write or set up the default one.

143
00:10:21,180 --> 00:10:24,060
And this one, I'll set it back to five hundred here.

144
00:10:24,360 --> 00:10:28,350
And then as far as the JSON, let's go with the same message.

145
00:10:28,570 --> 00:10:31,030
I'm just going to say something went wrong.

146
00:10:31,320 --> 00:10:32,160
Please try again.

147
00:10:32,580 --> 00:10:38,790
And above this return, we're going to go with if and then we'll check if the error is the instance

148
00:10:38,790 --> 00:10:47,400
of our custom API error that of course, I want to go with a return and I want to pass in the resident

149
00:10:47,400 --> 00:10:48,090
status.

150
00:10:49,230 --> 00:10:55,960
And that will be equal to a error, that status code, because, of course, that's where the value

151
00:10:55,960 --> 00:10:57,630
is sitting in that property.

152
00:10:57,780 --> 00:11:03,480
And when it comes to the actual message saying, deal, I'm going to go with a message and this one

153
00:11:03,480 --> 00:11:04,830
will be equal to error.

154
00:11:05,190 --> 00:11:08,030
And then we're looking for the message property.

155
00:11:08,490 --> 00:11:11,520
And once we save, let's go and test it out.

156
00:11:11,820 --> 00:11:18,890
Or essentially, if I send it here from the postman, of course, I'll have no task with of this idea.

157
00:11:19,140 --> 00:11:22,290
And if I'm going to mess with the syntax I should get the faster.

158
00:11:22,650 --> 00:11:25,150
Of course I do, because I can clearly see that something went wrong.

159
00:11:25,470 --> 00:11:27,130
Please try again.

160
00:11:27,630 --> 00:11:29,820
Lastly, let me just repeat one more time.

161
00:11:30,360 --> 00:11:37,980
But error handling and validation is something we're going to return to later, most likely in a separate

162
00:11:37,980 --> 00:11:38,340
project.

163
00:11:38,870 --> 00:11:47,010
This is just a general setup just to get you comfortable with overall ideas and later work on more complex

164
00:11:47,130 --> 00:11:50,120
validation and error handling approach.

