1
00:00:02,000 --> 00:00:05,000
Now that we implemented our MongoDB logic,

2
00:00:05,000 --> 00:00:08,000
let's wrap up this course section

3
00:00:08,000 --> 00:00:13,000
by adding some error handling to the API routes.

4
00:00:13,000 --> 00:00:17,000
Because data is also something you should know how to do.

5
00:00:17,000 --> 00:00:19,000
When you run server side code,

6
00:00:19,000 --> 00:00:21,000
things don't always go right.

7
00:00:21,000 --> 00:00:25,000
And at the moment we assume that everything works.

8
00:00:25,000 --> 00:00:25,000
Like here.

9
00:00:25,000 --> 00:00:27,000
When we store a newsletter.

10
00:00:27,000 --> 00:00:28,000
When we do that,

11
00:00:28,000 --> 00:00:30,000
we check the request method,

12
00:00:30,000 --> 00:00:32,000
check that it's a post method.

13
00:00:32,000 --> 00:00:36,000
And if it is, we basically just validate the input.

14
00:00:36,000 --> 00:00:38,000
That's one case

15
00:00:38,000 --> 00:00:42,000
where we send back an error status code if it's not valid.

16
00:00:42,000 --> 00:00:47,000
But for working with the database we just assume that

17
00:00:47,000 --> 00:00:49,000
everything works.

18
00:00:49,000 --> 00:00:51,000
And that's unrealistic.

19
00:00:51,000 --> 00:00:53,000
Things won't always work.

20
00:00:53,000 --> 00:00:56,000
Connecting to the database could fail

21
00:00:56,000 --> 00:00:58,000
or even if we are connected,

22
00:00:58,000 --> 00:01:01,000
inserting data could fail for whatever reason.

23
00:01:01,000 --> 00:01:05,000
Because the database servers went down temporarily

24
00:01:05,000 --> 00:01:08,000
just when we tried to insert data.

25
00:01:08,000 --> 00:01:10,000
Things can fail.

26
00:01:10,000 --> 00:01:13,000
No matter how unlikely that might be.

27
00:01:13,000 --> 00:01:16,000
So how do we deal with that then?

28
00:01:16,000 --> 00:01:18,000
Well, since I'm using a single weight here,

29
00:01:18,000 --> 00:01:20,000
we can use, try catch

30
00:01:20,000 --> 00:01:24,000
to try certain operations, catch errors

31
00:01:24,000 --> 00:01:28,000
and then send back a response if things go wrong.

32
00:01:28,000 --> 00:01:30,000
And here I actually wanna work with

33
00:01:30,000 --> 00:01:33,000
two different try catch blocks.

34
00:01:33,000 --> 00:01:35,000
I wanna try connecting.

35
00:01:35,000 --> 00:01:38,000
And if that fails, I wanna send back an error.

36
00:01:38,000 --> 00:01:41,000
But if it succeeds, I want to try this code.

37
00:01:41,000 --> 00:01:43,000
So inserting, I wanna try that.

38
00:01:43,000 --> 00:01:44,000
And if that fails,

39
00:01:44,000 --> 00:01:46,000
I want to send back an error,

40
00:01:46,000 --> 00:01:47,000
a different error.

41
00:01:47,000 --> 00:01:51,000
And I then also still wanna close the open connection.

42
00:01:51,000 --> 00:01:54,000
And to do this in an elegant way

43
00:01:54,000 --> 00:01:56,000
I'll work with some helper functions.

44
00:01:57,000 --> 00:01:59,000
For that I'll add a new function,

45
00:01:59,000 --> 00:02:02,000
which will name, connect database.

46
00:02:02,000 --> 00:02:04,000
The name is up to you of course.

47
00:02:04,000 --> 00:02:07,000
And this function here,

48
00:02:07,000 --> 00:02:12,000
will just hold this code for establishing a connection.

49
00:02:12,000 --> 00:02:14,000
So I'll put that in here,

50
00:02:15,000 --> 00:02:18,000
and then just return the client.

51
00:02:18,000 --> 00:02:20,000
And I'll add another of function,

52
00:02:20,000 --> 00:02:24,000
which I'll name, insert document,

53
00:02:24,000 --> 00:02:27,000
where I expect to get access to the client.

54
00:02:27,000 --> 00:02:30,000
And where I get the document that should

55
00:02:30,000 --> 00:02:32,000
be inserted as parameters.

56
00:02:32,000 --> 00:02:35,000
And inside of insert document,

57
00:02:35,000 --> 00:02:39,000
I then wanna run this code here.

58
00:02:39,000 --> 00:02:42,000
So copy that into insert document.

59
00:02:42,000 --> 00:02:44,000
And of course in order to use a weight

60
00:02:44,000 --> 00:02:46,000
there should be an async function

61
00:02:46,000 --> 00:02:50,000
and this here should be an async function as well.

62
00:02:50,000 --> 00:02:52,000
Connected database.

63
00:02:52,000 --> 00:02:57,000
Now, with that done down here in our main handler function,

64
00:02:57,000 --> 00:03:01,000
I get the client by calling connect database

65
00:03:02,000 --> 00:03:06,000
and then I will call insert document,

66
00:03:07,000 --> 00:03:09,000
pass my client as a first argument.

67
00:03:09,000 --> 00:03:13,000
And that document here as a second argument.

68
00:03:15,000 --> 00:03:19,000
Now I wanna await connect database

69
00:03:19,000 --> 00:03:21,000
because it is a async function

70
00:03:21,000 --> 00:03:23,000
so it returns a promise

71
00:03:23,000 --> 00:03:26,000
and we need to wait for disconnection to be established.

72
00:03:26,000 --> 00:03:27,000
And for the same reason

73
00:03:27,000 --> 00:03:30,000
we also want to await inserted document

74
00:03:30,000 --> 00:03:33,000
because that's also an async operation.

75
00:03:33,000 --> 00:03:36,000
Now with that outsourced to make this code

76
00:03:36,000 --> 00:03:38,000
a bit more readable,

77
00:03:38,000 --> 00:03:40,000
I will wrap connecting

78
00:03:40,000 --> 00:03:45,000
with try and catch potential errors here.

79
00:03:48,000 --> 00:03:51,000
And I'll then do the same for inserting here.

80
00:03:51,000 --> 00:03:56,000
I'll also wrap this with try and catch errors here.

81
00:03:58,000 --> 00:04:00,000
Now I wanna call a client close

82
00:04:00,000 --> 00:04:02,000
if we did connect successfully,

83
00:04:02,000 --> 00:04:05,000
but we failed to insert.

84
00:04:05,000 --> 00:04:08,000
So I wanna call client close instead

85
00:04:08,000 --> 00:04:11,000
of this try block here as well.

86
00:04:11,000 --> 00:04:13,000
And to make sure that client is available here,

87
00:04:13,000 --> 00:04:16,000
I'll set it as a variable here,

88
00:04:16,000 --> 00:04:19,000
and set it to a value in the first try block.

89
00:04:19,000 --> 00:04:21,000
And if we make it past this block,

90
00:04:21,000 --> 00:04:24,000
client will be the connected database.

91
00:04:24,000 --> 00:04:27,000
So calling close will work then.

92
00:04:27,000 --> 00:04:29,000
And then here we got the different errors.

93
00:04:29,000 --> 00:04:31,000
If we fail to connect,

94
00:04:31,000 --> 00:04:32,000
I wanna send back a response

95
00:04:32,000 --> 00:04:35,000
where I set the status code to 500.

96
00:04:35,000 --> 00:04:38,000
Which indicates that something went wrong on this server.

97
00:04:38,000 --> 00:04:40,000
Which clearly is the case here.

98
00:04:40,000 --> 00:04:43,000
And I'll send back a object as json

99
00:04:43,000 --> 00:04:45,000
which has a message where I say,

100
00:04:45,000 --> 00:04:50,000
connecting to the database failed.

101
00:04:50,000 --> 00:04:51,000
And if that failed,

102
00:04:51,000 --> 00:04:55,000
I don't wanna continue with the other code.

103
00:04:55,000 --> 00:04:58,000
I don't want to continue with this try-catch block.

104
00:04:58,000 --> 00:05:01,000
And I certainly don't want to send back this response.

105
00:05:01,000 --> 00:05:02,000
Eventually,

106
00:05:02,000 --> 00:05:04,000
hence here all returned in

107
00:05:04,000 --> 00:05:07,000
to prevent fervor function execution.

108
00:05:08,000 --> 00:05:11,000
I'll do something similar here if we fail to insert.

109
00:05:11,000 --> 00:05:13,000
I'll copy that code

110
00:05:13,000 --> 00:05:18,000
but I'll set the message to, inserting data failed.

111
00:05:19,000 --> 00:05:21,000
And that's how we can handle errors here.

112
00:05:21,000 --> 00:05:24,000
If we make it past both try-catch blocks.

113
00:05:24,000 --> 00:05:27,000
So if we don't go into any of these catch blocks,

114
00:05:27,000 --> 00:05:28,000
to be precise,

115
00:05:28,000 --> 00:05:31,000
we will still send back that success response.

116
00:05:33,000 --> 00:05:35,000
Now to check whatever didn't works.

117
00:05:35,000 --> 00:05:39,000
We can temporarily break disconnect database function

118
00:05:40,000 --> 00:05:43,000
by using incorrect credentials

119
00:05:43,000 --> 00:05:45,000
like a username that doesn't exist.

120
00:05:45,000 --> 00:05:46,000
We save that

121
00:05:46,000 --> 00:05:50,000
and I go back to the newsletter part here.

122
00:05:50,000 --> 00:05:55,000
If I try to sign up with some dummy newsletter

123
00:05:55,000 --> 00:05:57,000
we see that we get back an error here,

124
00:05:57,000 --> 00:05:59,000
in the network tab we also see it.

125
00:05:59,000 --> 00:06:01,000
We see that we got back a response

126
00:06:01,000 --> 00:06:04,000
with a status code of 500

127
00:06:04,000 --> 00:06:07,000
where the message we get is connecting

128
00:06:07,000 --> 00:06:09,000
to the database failed.

129
00:06:09,000 --> 00:06:12,000
Now, as mentioned before, we don't have any feedback

130
00:06:12,000 --> 00:06:15,000
on the user interface and you might of course

131
00:06:15,000 --> 00:06:17,000
want to add such feedback also

132
00:06:17,000 --> 00:06:19,000
for the loading states and on.

133
00:06:19,000 --> 00:06:21,000
I just didn't focus on this at the moment

134
00:06:21,000 --> 00:06:26,000
because I want to thoroughly dive into API routes.

135
00:06:26,000 --> 00:06:29,000
And there we can see that our error handling works.

136
00:06:29,000 --> 00:06:31,000
If I go back to a correct username

137
00:06:32,000 --> 00:06:37,000
and we try that same request again,

138
00:06:37,000 --> 00:06:38,000
I still get an error.

139
00:06:38,000 --> 00:06:41,000
Inserting data failed is what I get now.

140
00:06:42,000 --> 00:06:45,000
That's unintended but it's good to know.

141
00:06:45,000 --> 00:06:47,000
Let's see what went wrong there.

142
00:06:47,000 --> 00:06:50,000
And the problem here is a simple error I made

143
00:06:50,000 --> 00:06:53,000
in this extra inserts document function.

144
00:06:53,000 --> 00:06:55,000
I'm accepting client and document,

145
00:06:55,000 --> 00:06:58,000
while, I should be using that document

146
00:06:58,000 --> 00:06:59,000
for inserting data.

147
00:06:59,000 --> 00:07:01,000
Instead of having my hard coded document there

148
00:07:01,000 --> 00:07:03,000
as I had before

149
00:07:03,000 --> 00:07:05,000
where I referenced user email,

150
00:07:05,000 --> 00:07:07,000
which is just not available

151
00:07:07,000 --> 00:07:11,000
I should be using the document we get as a parameter.

152
00:07:11,000 --> 00:07:14,000
So still nice to see that this error handling also worked.

153
00:07:14,000 --> 00:07:16,000
But with that fixed,

154
00:07:16,000 --> 00:07:17,000
if we now try this again.

155
00:07:19,000 --> 00:07:21,000
Now that's looking better.

156
00:07:21,000 --> 00:07:24,000
We got a success response here and it works.

157
00:07:24,000 --> 00:07:27,000
So that's how we can add error handling here.

158
00:07:27,000 --> 00:07:31,000
Now let's also add it to the other API route.

