1
00:00:02,170 --> 00:00:04,046
Now, we talked a lot about

2
00:00:04,046 --> 00:00:05,470
404

3
00:00:05,470 --> 00:00:06,830
errors.

4
00:00:06,830 --> 00:00:10,220
That's just one kind of error that could occur, though.

5
00:00:10,220 --> 00:00:13,290
It's an error caused by the user of this website

6
00:00:13,290 --> 00:00:17,400
because the user entered an invalid URL.

7
00:00:17,400 --> 00:00:20,770
It could also be an error introduced by us as a developer,

8
00:00:20,770 --> 00:00:24,350
if we had some invalid link somewhere on our website.

9
00:00:24,350 --> 00:00:26,523
But typically, it's caused by the user.

10
00:00:27,630 --> 00:00:29,600
Now, there is a different kind of error,

11
00:00:29,600 --> 00:00:31,670
which is not caused by the error,

12
00:00:31,670 --> 00:00:35,310
and which we can't always avoid as a developer,

13
00:00:35,310 --> 00:00:39,410
but which we still should be prepared to handle, at least.

14
00:00:39,410 --> 00:00:41,990
And, that's a server side error.

15
00:00:41,990 --> 00:00:43,640
And in this simple website,

16
00:00:43,640 --> 00:00:46,320
we probably won't have too many issues,

17
00:00:46,320 --> 00:00:49,020
but one thing that could fail

18
00:00:49,020 --> 00:00:53,670
is reading or writing to this restaurants.json file.

19
00:00:53,670 --> 00:00:55,640
Of course it typically works,

20
00:00:55,640 --> 00:00:58,310
especially for testing this locally.

21
00:00:58,310 --> 00:01:01,850
But theoretically, if this would be a real website

22
00:01:01,850 --> 00:01:04,440
hosted on some remote machine,

23
00:01:04,440 --> 00:01:07,190
then maybe if too many visitors

24
00:01:07,190 --> 00:01:10,250
are visiting this page at the same time,

25
00:01:10,250 --> 00:01:12,680
there might be multiple conflicting

26
00:01:12,680 --> 00:01:14,730
read or write operations.

27
00:01:14,730 --> 00:01:18,300
And hence, one of these operations could fail.

28
00:01:18,300 --> 00:01:21,920
Now, that is actually a scenario which we can prevent

29
00:01:21,920 --> 00:01:24,790
as a developer by using a proper database,

30
00:01:24,790 --> 00:01:27,120
which we'll do very soon in this course.

31
00:01:27,120 --> 00:01:29,590
But still, it is one possible scenario

32
00:01:29,590 --> 00:01:31,660
where things can go wrong.

33
00:01:31,660 --> 00:01:35,680
And in general, in web development, things can go wrong.

34
00:01:35,680 --> 00:01:38,793
You should never assume that everything just works.

35
00:01:40,060 --> 00:01:44,000
Now, to simulate data reading or writing fails here,

36
00:01:44,000 --> 00:01:46,550
I will rename this file.

37
00:01:46,550 --> 00:01:49,590
Now, that's of course not a realistic scenario,

38
00:01:49,590 --> 00:01:52,720
but it allows us to test what users will see

39
00:01:52,720 --> 00:01:55,163
if reading or writing does fail.

40
00:01:56,040 --> 00:01:58,800
And since I renamed this now, that will fail

41
00:01:58,800 --> 00:02:03,430
because I tried to read a file named restaurants.json,

42
00:02:03,430 --> 00:02:06,863
whilst the file actually is named restaurant.json.

43
00:02:08,660 --> 00:02:10,270
And hence, if we now, for example

44
00:02:10,270 --> 00:02:14,400
go to browse restaurants, we get this error.

45
00:02:14,400 --> 00:02:16,150
And that's a very ugly error,

46
00:02:16,150 --> 00:02:19,340
which we don't wanna show to our users.

47
00:02:19,340 --> 00:02:20,690
Instead, we again,

48
00:02:20,690 --> 00:02:24,283
typically want to show a more customized error message.

49
00:02:26,040 --> 00:02:30,720
Now, we could prepare a standard server side error page,

50
00:02:30,720 --> 00:02:34,440
just as we have a standard 404.ejs file,

51
00:02:34,440 --> 00:02:35,273
or,

52
00:02:35,273 --> 00:02:40,040
we simply send back different responses if we get errors.

53
00:02:41,980 --> 00:02:45,510
Now, I will go for the standardized error page here,

54
00:02:45,510 --> 00:02:47,990
because we'll dive into more specific,

55
00:02:47,990 --> 00:02:50,713
customized error handling later.

56
00:02:52,080 --> 00:02:53,970
Now, to have a standardized page,

57
00:02:53,970 --> 00:02:56,010
which can often be very useful,

58
00:02:56,010 --> 00:02:57,587
I'll add a 500

59
00:02:57,587 --> 00:02:59,563
.ejs file here.

60
00:03:00,720 --> 00:03:03,480
Five-hundred is the standard error code

61
00:03:03,480 --> 00:03:06,310
for server side errors.

62
00:03:06,310 --> 00:03:08,220
A 500 status code

63
00:03:08,220 --> 00:03:11,893
means that something went wrong on the server.

64
00:03:13,630 --> 00:03:18,630
Now, here I'll copy that 404.ejs content and paste that in.

65
00:03:18,930 --> 00:03:23,430
And then, I'll just say, "Something went wrong!"

66
00:03:23,430 --> 00:03:27,410
And here, we could now add some extra explanation

67
00:03:27,410 --> 00:03:28,243
where we said,

68
00:03:28,243 --> 00:03:33,000
"Unfortunately, something went wrong on the server -

69
00:03:33,000 --> 00:03:34,020
we're...

70
00:03:34,020 --> 00:03:34,920
doing

71
00:03:34,920 --> 00:03:36,100
our best...

72
00:03:37,482 --> 00:03:38,780
to fix it

73
00:03:38,780 --> 00:03:39,880
as quickly

74
00:03:39,880 --> 00:03:41,247
as possible!"

75
00:03:42,210 --> 00:03:43,210
Something like that.

76
00:03:45,060 --> 00:03:47,780
Now, I wanna send back that page,

77
00:03:47,780 --> 00:03:49,260
that template here,

78
00:03:49,260 --> 00:03:53,150
whenever something does go wrong on the server.

79
00:03:53,150 --> 00:03:55,383
And how could we achieve this now?

80
00:03:57,190 --> 00:03:59,930
Now, there are different ways of achieving this

81
00:03:59,930 --> 00:04:01,800
as always in development,

82
00:04:01,800 --> 00:04:04,200
but it's such a standard scenario

83
00:04:04,200 --> 00:04:07,410
that something goes wrong in one of your routes

84
00:04:07,410 --> 00:04:11,190
that Express actually has built-in support for that,

85
00:04:11,190 --> 00:04:14,523
and makes it very easy for you to handle such errors.

86
00:04:15,370 --> 00:04:18,000
We can, again, work with our own middleware,

87
00:04:18,000 --> 00:04:21,100
just not with a standard middleware,

88
00:04:21,100 --> 00:04:24,380
like we have it here for the 404 page.

89
00:04:24,380 --> 00:04:28,490
But instead, a middleware that will only execute

90
00:04:28,490 --> 00:04:32,090
if an error occurred on your server.

91
00:04:32,090 --> 00:04:33,740
Because that's what's happening here,

92
00:04:33,740 --> 00:04:35,810
and Express will detect this.

93
00:04:35,810 --> 00:04:39,800
And then, if you added your own error handling middleware,

94
00:04:39,800 --> 00:04:41,560
Express will call,

95
00:04:41,560 --> 00:04:44,603
will execute that middleware for you.

96
00:04:46,340 --> 00:04:51,340
For this, we can add another app.use method call here,

97
00:04:51,410 --> 00:04:54,770
and pass a function to app.use.

98
00:04:54,770 --> 00:04:58,720
But this function now works a bit differently,

99
00:04:58,720 --> 00:05:00,030
because this function

100
00:05:00,030 --> 00:05:03,390
will now not just receive request and response,

101
00:05:03,390 --> 00:05:05,650
but instead four parameters,

102
00:05:05,650 --> 00:05:08,780
and it must receive four parameters,

103
00:05:08,780 --> 00:05:11,870
because that signals to Express

104
00:05:11,870 --> 00:05:15,170
that this is this special default

105
00:05:15,170 --> 00:05:18,010
error handler middleware function

106
00:05:18,010 --> 00:05:20,690
that should be invoked by Express

107
00:05:20,690 --> 00:05:25,690
if some error occurs anywhere in your Express application.

108
00:05:27,240 --> 00:05:30,530
And the four parameters which you should accept here,

109
00:05:30,530 --> 00:05:33,480
is an error object.

110
00:05:33,480 --> 00:05:36,520
Then, request and response as we have it here.

111
00:05:36,520 --> 00:05:40,373
And then, a fourth parameter, which is called next.

112
00:05:42,080 --> 00:05:44,590
Now, this error object is generated

113
00:05:44,590 --> 00:05:47,740
and populated automatically by Express.

114
00:05:47,740 --> 00:05:50,320
So, by that third-party library.

115
00:05:50,320 --> 00:05:52,360
And it simply contains more information

116
00:05:52,360 --> 00:05:54,820
about the error that occurred.

117
00:05:54,820 --> 00:05:56,870
You could use that extra information

118
00:05:56,870 --> 00:06:00,300
to handle different errors in different ways.

119
00:06:00,300 --> 00:06:02,130
For the moment, we won't use it,

120
00:06:02,130 --> 00:06:05,720
but we need to accept all four parameters here.

121
00:06:05,720 --> 00:06:08,090
So, we need to write this function like this

122
00:06:08,090 --> 00:06:10,200
for Express to understand

123
00:06:10,200 --> 00:06:14,053
that this is this special error handling function.

124
00:06:15,470 --> 00:06:19,180
Next is actually a parameter which you could accept

125
00:06:19,180 --> 00:06:24,180
in all your routes and the standard middleware here as well.

126
00:06:24,380 --> 00:06:26,480
But we haven't done this up to this point

127
00:06:26,480 --> 00:06:29,890
because it's a bit more advanced and hard to explain

128
00:06:29,890 --> 00:06:32,970
if you don't have a use case for it yet,

129
00:06:32,970 --> 00:06:35,320
which we don't have at this point.

130
00:06:35,320 --> 00:06:38,960
But we'll see next later in the course again.

131
00:06:38,960 --> 00:06:42,730
In the end, next allows you to have multiple middlewares

132
00:06:42,730 --> 00:06:44,360
that will work together.

133
00:06:44,360 --> 00:06:47,790
And when you call next inside of a middleware,

134
00:06:47,790 --> 00:06:50,460
it allows the request to move on

135
00:06:50,460 --> 00:06:55,430
to the next middleware or route handler in line.

136
00:06:55,430 --> 00:06:57,820
But we'll see it later.

137
00:06:57,820 --> 00:06:59,973
For the moment, we just need to accept it.

138
00:07:01,100 --> 00:07:05,280
And then, here we can render this "500" template

139
00:07:05,280 --> 00:07:06,363
that we prepared.

140
00:07:08,090 --> 00:07:10,420
And with that, if you save everything

141
00:07:10,420 --> 00:07:13,490
and we added this default error handler,

142
00:07:13,490 --> 00:07:15,670
if you now reload this page,

143
00:07:15,670 --> 00:07:17,780
which crashes at the moment,

144
00:07:17,780 --> 00:07:20,733
you get this standard 500 page.

145
00:07:21,600 --> 00:07:22,950
And that's huge,

146
00:07:22,950 --> 00:07:23,800
because now,

147
00:07:23,800 --> 00:07:25,540
to your end user,

148
00:07:25,540 --> 00:07:28,430
your site doesn't crash.

149
00:07:28,430 --> 00:07:30,450
Sure, something went wrong,

150
00:07:30,450 --> 00:07:32,520
but we are still on our page

151
00:07:32,520 --> 00:07:35,010
and we can still generally use it,

152
00:07:35,010 --> 00:07:38,660
and just parts of the page are not working.

153
00:07:38,660 --> 00:07:41,680
And that is something we typically wanna do.

154
00:07:41,680 --> 00:07:44,650
We wanna provide a good user experience

155
00:07:44,650 --> 00:07:46,730
and if something goes wrong,

156
00:07:46,730 --> 00:07:48,300
we at least wanna handle it

157
00:07:48,300 --> 00:07:50,653
with a custom page and message.

158
00:07:51,800 --> 00:07:52,890
Of course here,

159
00:07:52,890 --> 00:07:56,080
the entire scenario was simulated,

160
00:07:56,080 --> 00:07:56,913
because, of course,

161
00:07:56,913 --> 00:08:01,170
you typically don't rename files that you shouldn't rename,

162
00:08:01,170 --> 00:08:02,960
but it allowed me to show you

163
00:08:02,960 --> 00:08:05,870
this server side error scenario,

164
00:08:05,870 --> 00:08:08,090
and how you can handle that.

165
00:08:08,090 --> 00:08:11,410
And that is also something we'll encounter later again,

166
00:08:11,410 --> 00:08:14,840
when we dive into bigger course projects again,

167
00:08:14,840 --> 00:08:17,350
when we build our online shop later,

168
00:08:17,350 --> 00:08:18,270
because there,

169
00:08:18,270 --> 00:08:21,180
we of course all do wanna handle cases

170
00:08:21,180 --> 00:08:22,853
where things go wrong.

171
00:08:23,850 --> 00:08:26,070
And hence, I'll bring back the "S" here,

172
00:08:26,070 --> 00:08:30,440
but I'll keep that server side error handling in place.

173
00:08:30,440 --> 00:08:32,740
Now, of course everything works again, though.

