1
00:00:01,698 --> 00:00:05,680
So before we add sessions, let's add error handling.

2
00:00:05,680 --> 00:00:07,610
We actually got an error here,

3
00:00:07,610 --> 00:00:09,890
thanks to the missing session set up,

4
00:00:09,890 --> 00:00:13,690
but there also are other things that could go wrong.

5
00:00:13,690 --> 00:00:14,523
For example,

6
00:00:14,523 --> 00:00:17,980
if we have a look at D off the controller,

7
00:00:17,980 --> 00:00:19,880
there I'm calling user signup

8
00:00:21,260 --> 00:00:24,540
and that signup method in my model,

9
00:00:24,540 --> 00:00:27,150
that of course could also fail because there,

10
00:00:27,150 --> 00:00:30,210
I'm hashing the password, this could fail,

11
00:00:30,210 --> 00:00:33,570
and I'm trying to insert a user. This could also fail.

12
00:00:33,570 --> 00:00:34,403
For example,

13
00:00:34,403 --> 00:00:37,403
if we temporarily lost the database connection,

14
00:00:38,470 --> 00:00:42,040
and therefore we wanna handle such errors.

15
00:00:42,040 --> 00:00:45,660
Now, thankfully express gives us a default error

16
00:00:45,660 --> 00:00:47,260
handling mechanism

17
00:00:47,260 --> 00:00:48,530
In app JS,

18
00:00:48,530 --> 00:00:51,190
We can register a special middleware.

19
00:00:51,190 --> 00:00:53,790
Typically we do that at the very end here,

20
00:00:53,790 --> 00:00:56,313
which is a error handling middleware.

21
00:00:57,320 --> 00:00:58,430
We could do this here,

22
00:00:58,430 --> 00:01:00,640
but since I already have a middlewares folder,

23
00:01:00,640 --> 00:01:02,630
I'll add it in there.

24
00:01:02,630 --> 00:01:06,223
Error dash handler JS sounds like a fitting file name.

25
00:01:08,010 --> 00:01:10,140
In there, I'll trade a function because

26
00:01:10,140 --> 00:01:12,120
all middlewares are functions

27
00:01:12,120 --> 00:01:15,130
and this function will receive a name of

28
00:01:15,130 --> 00:01:16,920
handle errors

29
00:01:18,590 --> 00:01:19,433
like this.

30
00:01:20,270 --> 00:01:23,404
But then this is a special middleware function because it

31
00:01:23,404 --> 00:01:26,790
does not just receive free parameter values,

32
00:01:26,790 --> 00:01:31,650
request, response and next, but it actually receives four.

33
00:01:31,650 --> 00:01:34,020
It receives the special error parameter

34
00:01:34,020 --> 00:01:35,740
value as a first value

35
00:01:36,710 --> 00:01:39,070
and expressible call this function

36
00:01:39,070 --> 00:01:43,300
whenever we have an error in one of the other middlewares or

37
00:01:43,300 --> 00:01:44,883
route handler functions,

38
00:01:45,920 --> 00:01:49,094
and then this first parameter value will be an error objects

39
00:01:49,094 --> 00:01:52,403
with more details about the error that occured.

40
00:01:53,390 --> 00:01:57,113
Now it's in here where I now wanna handle the case that we

41
00:01:57,113 --> 00:02:01,474
have an error on one of our pages for this in here,

42
00:02:01,474 --> 00:02:05,246
we can console lock the error so that we have some log on

43
00:02:05,246 --> 00:02:08,923
the server. This will not be visible to our users.

44
00:02:09,820 --> 00:02:11,020
And then

45
00:02:11,020 --> 00:02:12,450
I wanna render

46
00:02:12,450 --> 00:02:13,380
a template

47
00:02:13,380 --> 00:02:14,720
a error page,

48
00:02:14,720 --> 00:02:17,726
which I want to show in case of such errors.

49
00:02:17,726 --> 00:02:19,940
And I don't just want to render it.

50
00:02:19,940 --> 00:02:24,220
I also want to set a specific status code in such cases,

51
00:02:24,220 --> 00:02:26,810
and I'll set the status code to 500,

52
00:02:26,810 --> 00:02:29,780
which indicates a service site error.

53
00:02:29,780 --> 00:02:33,160
We also have oper errors like a not found error,

54
00:02:33,160 --> 00:02:36,600
but these errors will be handled in different places.

55
00:02:36,600 --> 00:02:40,960
This error handling middleware with this line of code should

56
00:02:40,960 --> 00:02:42,250
become active,

57
00:02:42,250 --> 00:02:45,380
whenever something went wrong on the server and we're not

58
00:02:45,380 --> 00:02:47,660
able to recover from it.

59
00:02:47,660 --> 00:02:48,493
So in this case,

60
00:02:48,493 --> 00:02:51,150
I'll set the status code off the response to 500.

61
00:02:51,150 --> 00:02:53,230
So our response will be sent,

62
00:02:53,230 --> 00:02:56,150
but it will indicate that something went wrong.

63
00:02:56,150 --> 00:02:58,592
And I want to render an error page.

64
00:02:58,592 --> 00:02:59,550
At moment,

65
00:02:59,550 --> 00:03:03,950
we have no error page and therefore back in my views folder,

66
00:03:03,950 --> 00:03:07,950
I'll add a third sub folder next to admin and customer,

67
00:03:07,950 --> 00:03:10,620
which will name base because in there I want to have some

68
00:03:10,620 --> 00:03:14,850
general views, which are not admin or customer specific,

69
00:03:14,850 --> 00:03:18,213
or actually let's name it shared to make this even clearer.

70
00:03:19,760 --> 00:03:24,760
And in this shared folder, I'll add a 500 EJS file,

71
00:03:24,960 --> 00:03:28,537
which will be my template for 500 errors.

72
00:03:28,537 --> 00:03:33,170
Now in there I'll then copy the structure from

73
00:03:33,170 --> 00:03:34,370
the signup page

74
00:03:36,700 --> 00:03:37,810
like this.

75
00:03:37,810 --> 00:03:40,370
But of course, I don't want to have any form in there.

76
00:03:40,370 --> 00:03:42,533
I'll get rid of that form here.

77
00:03:43,530 --> 00:03:48,330
I just want to have the include of the head part,

78
00:03:48,330 --> 00:03:52,760
change the page title to an error

79
00:03:52,760 --> 00:03:53,593
occurred.

80
00:03:54,660 --> 00:03:57,010
Don't import forums and off CSS.

81
00:03:57,010 --> 00:04:01,020
I don't need that here include the header though.

82
00:04:01,020 --> 00:04:03,010
And then here in the H one element,

83
00:04:03,010 --> 00:04:06,320
I'll just say something went wrong

84
00:04:07,670 --> 00:04:09,490
and I'll add a paragraph

85
00:04:09,490 --> 00:04:14,020
where I say, unfortunately, something went wrong.

86
00:04:14,020 --> 00:04:17,113
Please try again later.

87
00:04:19,029 --> 00:04:22,552
I'll also add a second paragraph with an anchor tag below it

88
00:04:22,552 --> 00:04:26,860
which will lead back to the starting page and which I'll

89
00:04:26,860 --> 00:04:28,281
give a clause of BTN.

90
00:04:28,281 --> 00:04:31,840
So here I'm using my button styles again for this anchor tag

91
00:04:31,840 --> 00:04:34,870
now, and as a caption for this anchor tag,

92
00:04:34,870 --> 00:04:37,600
I'll say back to safety.

93
00:04:37,600 --> 00:04:41,450
So that's now my default error template,

94
00:04:41,450 --> 00:04:46,270
which should be rendered if I have a server site error and

95
00:04:46,270 --> 00:04:48,581
therefore back in the era handler JS file,

96
00:04:48,581 --> 00:04:53,581
I want to render this. So I want to render shared slash 500.

97
00:04:54,800 --> 00:04:57,540
That's the path to this specific template here

98
00:04:59,470 --> 00:05:00,720
and actually

99
00:05:01,880 --> 00:05:04,690
here in this 500 EJS file.

100
00:05:04,690 --> 00:05:08,924
My includes pops here won't work because they're,

101
00:05:08,924 --> 00:05:12,240
I'm trying to go up one level and then into an includes

102
00:05:12,240 --> 00:05:15,573
folder, but that doesn't exist here.

103
00:05:16,760 --> 00:05:17,660
Instead here,

104
00:05:17,660 --> 00:05:20,900
I would now have to go into the customer or admin includes

105
00:05:20,900 --> 00:05:25,811
folder based on which role I have and to make this a bit

106
00:05:25,811 --> 00:05:27,070
easier.

107
00:05:27,070 --> 00:05:30,380
And because we'll share head header and footer between

108
00:05:30,380 --> 00:05:32,313
customer and admin, anyways,

109
00:05:32,313 --> 00:05:35,850
I will actually grab the includes folder in the customer

110
00:05:35,850 --> 00:05:39,830
folder and bring that over into the shared folder.

111
00:05:39,830 --> 00:05:40,703
I'll move it.

112
00:05:42,090 --> 00:05:46,780
So then the pops here and 500 EJS are correct again.

113
00:05:46,780 --> 00:05:48,550
And now in the customer folder,

114
00:05:48,550 --> 00:05:50,860
I'll have to update the puffs in the sign up

115
00:05:50,860 --> 00:05:53,210
and log in EJS files.

116
00:05:53,210 --> 00:05:56,097
Here we now need to go up one level

117
00:05:56,097 --> 00:05:58,330
and then another level.

118
00:05:58,330 --> 00:05:59,163
So out of,

119
00:05:59,163 --> 00:06:02,819
off into customer and then interviews and then dive into

120
00:06:02,819 --> 00:06:06,144
shared and then into includes.

121
00:06:06,144 --> 00:06:10,350
And this segment now needs to be added to all the include

122
00:06:10,350 --> 00:06:11,770
pops I have here

123
00:06:13,530 --> 00:06:14,473
like this,

124
00:06:15,610 --> 00:06:16,890
and also in log in,

125
00:06:16,890 --> 00:06:19,520
EJS there, by the way,

126
00:06:19,520 --> 00:06:23,120
I will already change the page title to log in to fix this

127
00:06:23,120 --> 00:06:28,120
as well, update the include path here, and then also here.

128
00:06:29,570 --> 00:06:31,470
And with that, that should be working.

129
00:06:32,659 --> 00:06:36,170
Now I of course want to make sure that my error handling

130
00:06:36,170 --> 00:06:39,440
middleware is exposed and available outset of this error

131
00:06:39,440 --> 00:06:44,123
handler JS file as well. So I will export it like this.

132
00:06:45,660 --> 00:06:49,150
And then in app JS here, we can

133
00:06:49,150 --> 00:06:50,850
import this.

134
00:06:50,850 --> 00:06:55,560
We can add the error handler middleware

135
00:06:55,560 --> 00:06:57,510
by requiring dot slash

136
00:06:59,687 --> 00:07:02,523
middlewares slash error handler.

137
00:07:04,760 --> 00:07:07,159
And then at the very bottom, as mentioned before,

138
00:07:07,159 --> 00:07:10,683
I want to use this error handler middleware,

139
00:07:13,820 --> 00:07:16,100
if I'll now reload this page,

140
00:07:16,100 --> 00:07:20,160
I get a different error because I have an error in one of my

141
00:07:20,160 --> 00:07:22,590
pops here, as it seems,

142
00:07:22,590 --> 00:07:24,990
in 500 EJS

143
00:07:24,990 --> 00:07:25,823
yeah, here,

144
00:07:25,823 --> 00:07:28,730
I'm still going up one level to then dive into includes.

145
00:07:28,730 --> 00:07:29,840
This is not wrong.

146
00:07:29,840 --> 00:07:34,840
Includes is next to 500 EJS so it should be just includes

147
00:07:34,866 --> 00:07:39,866
head includes header and includes footer in the 500 EJS file

148
00:07:43,850 --> 00:07:47,070
with that. If I reload, we now get this error page,

149
00:07:47,070 --> 00:07:49,690
which is handling that CSR F error,

150
00:07:49,690 --> 00:07:52,480
which of course is an error we'll still have to fix,

151
00:07:52,480 --> 00:07:55,589
but we can tell that error handling is working here and that

152
00:07:55,589 --> 00:07:57,673
is an important first step.

