﻿1
00:00:00,860 --> 00:00:03,680
‫Welcome back, now before we go deep

2
00:00:03,680 --> 00:00:06,820
‫into actual error handling, let's first write

3
00:00:06,820 --> 00:00:09,430
‫a handler for undefined routes.

4
00:00:09,430 --> 00:00:12,250
‫So basically for routes that we didn't assign

5
00:00:12,250 --> 00:00:13,403
‫any handler yet.

6
00:00:14,800 --> 00:00:17,820
‫And first up, we need to start our application here

7
00:00:17,820 --> 00:00:21,053
‫which we didn't quit after the recent debugging session.

8
00:00:21,950 --> 00:00:23,363
‫So MPM start.

9
00:00:25,260 --> 00:00:28,290
‫And so let's wait for a database connection.

10
00:00:28,290 --> 00:00:33,290
‫Here it is, and so let's now go to Postman here

11
00:00:33,640 --> 00:00:36,810
‫and try to hit the route that we didn't yet define.

12
00:00:36,810 --> 00:00:39,793
‫So I'm just gonna go ahead, copy this URL here,

13
00:00:41,892 --> 00:00:44,260
‫all right, and so for example

14
00:00:44,260 --> 00:00:46,410
‫let's say that instead of apiV1

15
00:00:48,240 --> 00:00:49,490
‫tours,

16
00:00:49,490 --> 00:00:53,223
‫we would say just api/tours.

17
00:00:54,140 --> 00:00:58,400
‫Okay so in that case we would get this HTML result,

18
00:00:58,400 --> 00:01:01,470
‫all right, so Express automatically sends

19
00:01:01,470 --> 00:01:05,380
‫this HTML code here, along with a 404 Not Found

20
00:01:05,380 --> 00:01:08,700
‫error code in case that there is not any handler

21
00:01:08,700 --> 00:01:12,900
‫for the route that was requested, okay.

22
00:01:12,900 --> 00:01:16,483
‫Or, we could also simply misspell tour here for example.

23
00:01:17,640 --> 00:01:19,850
‫So for example in this case we would

24
00:01:19,850 --> 00:01:21,840
‫still get the same error.

25
00:01:21,840 --> 00:01:25,210
‫Now there's also another situation which is if here

26
00:01:25,210 --> 00:01:29,580
‫after tours we specify something else,

27
00:01:29,580 --> 00:01:31,800
‫let's say just something like this.

28
00:01:31,800 --> 00:01:34,790
‫And so let's take a look at the error that we get,

29
00:01:34,790 --> 00:01:38,470
‫and so now we get that the cast to object ID failed.

30
00:01:38,470 --> 00:01:40,560
‫And that's because we actually have a route that

31
00:01:40,560 --> 00:01:45,390
‫accepts an ID parameter here after the tour/, right.

32
00:01:45,390 --> 00:01:48,770
‫And so MongoDB is basically trying to find a document

33
00:01:48,770 --> 00:01:53,180
‫with this ID, but cannot convert it to a valid MongoDB

34
00:01:53,180 --> 00:01:55,160
‫object ID, all right.

35
00:01:55,160 --> 00:01:58,810
‫So again that is a different situation, let's just

36
00:01:59,820 --> 00:02:02,930
‫keep working with this one, so again, where we get

37
00:02:02,930 --> 00:02:05,550
‫this error but in this HTML form.

38
00:02:05,550 --> 00:02:08,330
‫Now since we're doing an API here, it doesn't make

39
00:02:08,330 --> 00:02:12,070
‫much sense to send back HTML, right, and so let's

40
00:02:12,070 --> 00:02:15,400
‫now fix this and basically create a handle function

41
00:02:15,400 --> 00:02:19,270
‫for all the routes that are not cached by our routers.

42
00:02:19,270 --> 00:02:22,610
‫Okay, so let's go back to our application here

43
00:02:22,610 --> 00:02:25,540
‫and open up app.js.

44
00:02:25,540 --> 00:02:27,950
‫Okay, so that's basically the definition

45
00:02:27,950 --> 00:02:30,180
‫of our Express application.

46
00:02:30,180 --> 00:02:32,530
‫Now before doing anything else

47
00:02:32,530 --> 00:02:35,190
‫let's actually get rid of this middleware here

48
00:02:35,190 --> 00:02:36,700
‫that we do no longer need.

49
00:02:36,700 --> 00:02:39,360
‫So we just used this here in order to demonstrate

50
00:02:39,360 --> 00:02:43,160
‫the concept of middleware, and so at this point

51
00:02:43,160 --> 00:02:45,080
‫we no longer need that.

52
00:02:45,080 --> 00:02:47,980
‫All right now how are we gonna implement a route

53
00:02:47,980 --> 00:02:51,410
‫handler for a route that was not cached by any

54
00:02:51,410 --> 00:02:53,380
‫of our other route handlers?

55
00:02:53,380 --> 00:02:56,160
‫So for doing that remember that all these middleware

56
00:02:56,160 --> 00:02:59,770
‫functions are executed in the order they are in the code.

57
00:02:59,770 --> 00:03:02,930
‫And so the idea is that if we have a request

58
00:03:02,930 --> 00:03:06,210
‫that makes it into this point here of our code

59
00:03:06,210 --> 00:03:08,760
‫then it means that neither the tourRouter

60
00:03:08,760 --> 00:03:12,860
‫nor the userRouter were able to cache it, okay.

61
00:03:12,860 --> 00:03:16,590
‫And so if we add a middleware here after these routers

62
00:03:16,590 --> 00:03:20,060
‫it will only be reached again if not handled by any

63
00:03:20,060 --> 00:03:22,470
‫of our other routers, okay.

64
00:03:22,470 --> 00:03:25,610
‫So let's do that, and then really understand how it works

65
00:03:25,610 --> 00:03:27,550
‫after it's already implemented.

66
00:03:27,550 --> 00:03:29,600
‫So we're gonna implement a route handler,

67
00:03:29,600 --> 00:03:30,923
‫and so we say app.

68
00:03:32,450 --> 00:03:34,540
‫and now the HTTP method for which

69
00:03:34,540 --> 00:03:36,380
‫we want to specify the route.

70
00:03:36,380 --> 00:03:40,630
‫Now we could use get here right, so just like we did before

71
00:03:40,630 --> 00:03:43,410
‫but then what about post requests, or delete,

72
00:03:43,410 --> 00:03:45,030
‫or patch requests?

73
00:03:45,030 --> 00:03:47,730
‫You would then have to write handlers for these as well,

74
00:03:47,730 --> 00:03:50,190
‫and we don't want that, we simply want to handle

75
00:03:50,190 --> 00:03:54,270
‫all the routes, so all the URL's, for all the verbs

76
00:03:54,270 --> 00:03:56,707
‫right here in this one handler, okay.

77
00:03:56,707 --> 00:03:59,710
‫And so in Express, we can use app.all.

78
00:03:59,710 --> 00:04:02,460
‫And so that's then going to run for all the verbs,

79
00:04:02,460 --> 00:04:05,430
‫So all the HTTP method, all right.

80
00:04:05,430 --> 00:04:08,270
‫Next up we specify the URL, and since here

81
00:04:08,270 --> 00:04:10,920
‫we want to handle all the URL's that were not handled

82
00:04:10,920 --> 00:04:13,950
‫before we can use the star here, which is going

83
00:04:13,950 --> 00:04:17,320
‫to stand for everything, all right, and then the

84
00:04:17,320 --> 00:04:19,920
‫rest is just a regular middleware function,

85
00:04:19,920 --> 00:04:21,183
‫just like before.

86
00:04:23,980 --> 00:04:24,893
‫So request,

87
00:04:26,210 --> 00:04:27,883
‫response, and next.

88
00:04:29,270 --> 00:04:32,290
‫Okay, and what do we want to do here?

89
00:04:32,290 --> 00:04:34,700
‫Well we simply want to send back a response

90
00:04:34,700 --> 00:04:38,653
‫in the JSON format, so not the HTML that we have right now.

91
00:04:40,100 --> 00:04:41,573
‫So res.status,

92
00:04:43,100 --> 00:04:46,110
‫and here let's set a 404, so Not Found

93
00:04:48,220 --> 00:04:52,190
‫and then a JSON response, so just like the usual one

94
00:04:52,190 --> 00:04:54,343
‫where we set the status to fail.

95
00:04:57,090 --> 00:05:01,153
‫So just a regular adjacent formatted response.

96
00:05:03,590 --> 00:05:05,980
‫And then some kind of message here, and actually

97
00:05:05,980 --> 00:05:07,580
‫let's do a template string here,

98
00:05:07,580 --> 00:05:09,790
‫because I want to put a variable in there.

99
00:05:09,790 --> 00:05:11,370
‫So can't,

100
00:05:11,370 --> 00:05:12,203
‫find.

101
00:05:13,380 --> 00:05:16,650
‫And then we can use req.originalUrl

102
00:05:18,220 --> 00:05:21,900
‫okay, so that's a property that we have on the request

103
00:05:21,900 --> 00:05:26,233
‫which is as the name says, the URL that was requested,

104
00:05:27,300 --> 00:05:28,270
‫all right.

105
00:05:28,270 --> 00:05:30,610
‫So this new response that we're going to send back now

106
00:05:30,610 --> 00:05:33,230
‫is a lot better than the HTML that

107
00:05:33,230 --> 00:05:37,163
‫we were receiving previously, so let's now test that out.

108
00:05:40,440 --> 00:05:44,020
‫And indeed, now we get a JSON error message back here.

109
00:05:44,020 --> 00:05:47,970
‫And so here we also get the URL that was requested,

110
00:05:47,970 --> 00:05:50,586
‫and indeed it's the one that we tried to access,

111
00:05:50,586 --> 00:05:54,760
‫but it is not available, all right, great.

112
00:05:54,760 --> 00:05:57,240
‫Now again, why did this work?

113
00:05:57,240 --> 00:06:01,200
‫So again the idea is that if we are able to reach

114
00:06:01,200 --> 00:06:04,120
‫this point here then it means that the request

115
00:06:04,120 --> 00:06:06,281
‫response cycle was not yet finished

116
00:06:06,281 --> 00:06:09,100
‫at this point in our code, right.

117
00:06:09,100 --> 00:06:11,780
‫Because remember that middleware is added

118
00:06:11,780 --> 00:06:14,040
‫to the middleware stack in the order that it's

119
00:06:14,040 --> 00:06:16,010
‫defined here in our code.

120
00:06:16,010 --> 00:06:18,810
‫And so basically this code here runs first,

121
00:06:18,810 --> 00:06:21,840
‫and so if the route was matched here in our tourRouter

122
00:06:21,840 --> 00:06:25,230
‫then our request would never even reach this code,

123
00:06:25,230 --> 00:06:27,660
‫and so then this would not get executed.

124
00:06:27,660 --> 00:06:30,050
‫And so this should basically be the last part

125
00:06:30,050 --> 00:06:32,560
‫after all our other routes, all right.

126
00:06:32,560 --> 00:06:35,240
‫And if I were, just to try this out,

127
00:06:35,240 --> 00:06:38,140
‫now put this right at the top of our application

128
00:06:39,230 --> 00:06:43,260
‫then you will see that no matter what request

129
00:06:43,260 --> 00:06:47,750
‫we're gonna do, we will always get this same response.

130
00:06:47,750 --> 00:06:49,653
‫Right, so let's test that,

131
00:06:51,550 --> 00:06:54,600
‫and indeed now we get JS error message,

132
00:06:54,600 --> 00:06:56,600
‫and so that's because all request

133
00:06:56,600 --> 00:06:59,850
‫now reach this route handler here, and it's actually matched

134
00:06:59,850 --> 00:07:04,290
‫because it's a GET request, which is part of all the verbs,

135
00:07:04,290 --> 00:07:08,060
‫right, and then all of the routes, so all of the URL's

136
00:07:08,060 --> 00:07:10,760
‫are cached here, and so of course it handles

137
00:07:10,760 --> 00:07:13,920
‫that URL that we just did here.

138
00:07:13,920 --> 00:07:17,333
‫And the same of course, for example for a delete tour.

139
00:07:18,330 --> 00:07:20,590
‫So the same thing would happen we would always

140
00:07:20,590 --> 00:07:22,573
‫get the same response,

141
00:07:23,740 --> 00:07:24,573
‫all right.

142
00:07:25,430 --> 00:07:28,500
‫So let's of course put it back, but this was

143
00:07:28,500 --> 00:07:33,183
‫just to demonstrate how and why this works,

144
00:07:34,100 --> 00:07:35,670
‫all right.

145
00:07:35,670 --> 00:07:38,890
‫Great so this an important part to make our API

146
00:07:38,890 --> 00:07:42,150
‫a bit more user friendly, but next up let's now

147
00:07:42,150 --> 00:07:44,873
‫start learning about real error handling.

