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