Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,090 --> 00:00:04,320
Let's simulate an error.
2
00:00:04,320 --> 00:00:07,063
And for that, we can, for example, remove .json,
3
00:00:07,900 --> 00:00:09,880
something Firebase doesn't like.
4
00:00:09,880 --> 00:00:12,590
And if we do it, and I reload,
5
00:00:12,590 --> 00:00:14,627
we therefore fail to load the data.
6
00:00:14,627 --> 00:00:17,170
And we don't see anything here in our app,
7
00:00:17,170 --> 00:00:20,450
but we see it if I open the developer tools.
8
00:00:20,450 --> 00:00:22,870
There we see that we have an error.
9
00:00:22,870 --> 00:00:24,820
We failed to fetch the data.
10
00:00:24,820 --> 00:00:26,450
And of course our typical users
11
00:00:26,450 --> 00:00:29,220
will not check their console.
12
00:00:29,220 --> 00:00:34,220
Therefore, we wanna show the error also here on this screen.
13
00:00:35,310 --> 00:00:37,430
And it's up to you how you wanna show it.
14
00:00:37,430 --> 00:00:40,110
I at least wanna show an error message down here
15
00:00:40,110 --> 00:00:41,293
instead of loading.
16
00:00:42,600 --> 00:00:45,500
Hence, we need a third state.
17
00:00:45,500 --> 00:00:47,260
We need a state that determines
18
00:00:47,260 --> 00:00:50,150
whether we have an error or not.
19
00:00:50,150 --> 00:00:52,590
So an httpError, for example,
20
00:00:52,590 --> 00:00:54,890
but the state name is up to you.
21
00:00:54,890 --> 00:00:56,450
So here we have that.
22
00:00:56,450 --> 00:01:00,750
And initially the value here is false or maybe null
23
00:01:00,750 --> 00:01:03,060
or simply undefined.
24
00:01:03,060 --> 00:01:05,950
Because it's not just a true-false value,
25
00:01:05,950 --> 00:01:08,770
later, instead, I wanna have the error message here.
26
00:01:08,770 --> 00:01:11,480
And initially I don't wanna have any value.
27
00:01:11,480 --> 00:01:12,980
So an undefined value.
28
00:01:12,980 --> 00:01:17,240
So not passing any default value like this should be fine.
29
00:01:17,240 --> 00:01:20,170
We could also set it to null to be more explicit
30
00:01:20,170 --> 00:01:24,520
about our goal of not having any value initially,
31
00:01:24,520 --> 00:01:26,043
but I'll go with undefined.
32
00:01:27,440 --> 00:01:31,450
Now when we fetch data and we fail,
33
00:01:31,450 --> 00:01:33,577
I wanna, well, set my error.
34
00:01:33,577 --> 00:01:36,100
And for this, we, first of all need to find out
35
00:01:36,100 --> 00:01:37,240
if we failed.
36
00:01:37,240 --> 00:01:42,240
Now, for this, I wanna check if response okay is falsey,
37
00:01:43,330 --> 00:01:46,830
so if it's not okay, hence the exclamation mark.
38
00:01:46,830 --> 00:01:48,380
And we'll make it into this if check
39
00:01:48,380 --> 00:01:52,040
if we have some errors status code reported back
40
00:01:52,040 --> 00:01:53,310
by the server.
41
00:01:53,310 --> 00:01:55,470
And if that should be the case,
42
00:01:55,470 --> 00:01:59,500
I wanna throw a new error here where I say something
43
00:01:59,500 --> 00:02:01,240
went wrong.
44
00:02:01,240 --> 00:02:03,910
And we could also look into the error response
45
00:02:03,910 --> 00:02:08,009
and see if the server gave us a more useful error string.
46
00:02:08,009 --> 00:02:11,660
But here I'll just stick to this generic error message.
47
00:02:11,660 --> 00:02:13,950
And then throwing it as a new error,
48
00:02:13,950 --> 00:02:16,883
which means that the lines thereafter won't execute.
49
00:02:17,720 --> 00:02:19,680
Instead, now we wanna handle this error.
50
00:02:19,680 --> 00:02:21,470
And that's another advantage
51
00:02:21,470 --> 00:02:26,470
of using a separate function for wrapping this fetch logic.
52
00:02:27,280 --> 00:02:30,660
This fetchMeals function will now throw an error
53
00:02:30,660 --> 00:02:33,543
if something goes wrong with sending that request.
54
00:02:34,480 --> 00:02:38,910
Therefore, we can go to the place where we call fetchMeals,
55
00:02:38,910 --> 00:02:41,260
which is still inside of user fact,
56
00:02:41,260 --> 00:02:44,400
but outside of the fetchMeals' function, of course.
57
00:02:44,400 --> 00:02:48,610
And we can simply wrap this with try-catch.
58
00:02:48,610 --> 00:02:50,970
So we can try and catch here.
59
00:02:50,970 --> 00:02:53,300
So we try calling fetchMeals.
60
00:02:53,300 --> 00:02:56,810
But if inside of that function an error is thrown,
61
00:02:56,810 --> 00:02:58,870
as is the case here,
62
00:02:58,870 --> 00:03:01,470
then we make it into this catch block,
63
00:03:01,470 --> 00:03:04,430
which is still inside the effect function
64
00:03:04,430 --> 00:03:07,510
but outside of the fetchMeals function.
65
00:03:07,510 --> 00:03:09,960
And then here we can do whatever we wanna do.
66
00:03:09,960 --> 00:03:12,290
I wanna setIsLoading to false
67
00:03:12,290 --> 00:03:14,070
because we're not loading anymore.
68
00:03:14,070 --> 00:03:16,460
Even if it's an error, we're done loading.
69
00:03:16,460 --> 00:03:18,573
And then wanna set error, setHttpError,
70
00:03:19,771 --> 00:03:20,667
cue the error caught.
71
00:03:22,100 --> 00:03:25,760
So we do get access to that error here when using try-catch,
72
00:03:25,760 --> 00:03:29,390
and therefore I can set the httpError
73
00:03:29,390 --> 00:03:30,570
to the error we're getting.
74
00:03:30,570 --> 00:03:34,430
Or to be precise, to the error message.
75
00:03:34,430 --> 00:03:36,370
We'll get an error object here.
76
00:03:36,370 --> 00:03:39,680
And that object by default has a message property.
77
00:03:39,680 --> 00:03:41,870
When we generate an error like this,
78
00:03:41,870 --> 00:03:44,560
and we pass a string to the constructor,
79
00:03:44,560 --> 00:03:46,310
that string will be stored
80
00:03:46,310 --> 00:03:50,003
in the message property of the created error object.
81
00:03:51,770 --> 00:03:55,560
And with that, we should be setting this error state.
82
00:03:55,560 --> 00:03:58,910
Now we can also utilize that and we can check.
83
00:03:58,910 --> 00:04:02,270
Maybe after we checked for whether we're loading,
84
00:04:02,270 --> 00:04:07,270
we can check if httpError is truthy,
85
00:04:07,710 --> 00:04:11,250
so if it has a string with some text inside of it,
86
00:04:11,250 --> 00:04:12,083
for example.
87
00:04:12,919 --> 00:04:15,770
I mean, in that case, return yet another state.
88
00:04:15,770 --> 00:04:18,660
In that case, I wanna return a section
89
00:04:21,120 --> 00:04:26,120
with a text of httpError, so where I output my error text.
90
00:04:27,390 --> 00:04:31,233
And I'll give this a class of meals error,
91
00:04:32,080 --> 00:04:33,660
which we have yet to add,
92
00:04:33,660 --> 00:04:35,553
and we can quickly add this together.
93
00:04:36,501 --> 00:04:38,370
MealsError here.
94
00:04:38,370 --> 00:04:41,640
Here, I'll also set this to text-align center
95
00:04:41,640 --> 00:04:43,373
but set the color to red.
96
00:04:44,210 --> 00:04:47,090
And that will not be too beautiful.
97
00:04:47,090 --> 00:04:50,730
I just wanna have this little bit more errors-ish style.
98
00:04:50,730 --> 00:04:52,530
You can, of course, fine tune the styling
99
00:04:52,530 --> 00:04:53,963
exactly as you want it.
100
00:04:55,200 --> 00:04:59,350
So now with that, if we save this and I reload here,
101
00:04:59,350 --> 00:05:02,040
we'll notice something interesting.
102
00:05:02,040 --> 00:05:04,463
It's still only says loading here.
103
00:05:05,490 --> 00:05:07,820
Now that can be tricky to spot.
104
00:05:07,820 --> 00:05:11,090
The reason for that is that we're using try-catch,
105
00:05:11,090 --> 00:05:15,863
but keep in mind that fetchMeals is an async function,
106
00:05:16,800 --> 00:05:19,940
and therefore, it always returns a promise.
107
00:05:19,940 --> 00:05:23,150
Now, if we throw an error instead of a promise,
108
00:05:23,150 --> 00:05:28,150
that error will cause that promise to reject.
109
00:05:28,670 --> 00:05:31,310
So we can't use try-catch to wrap it
110
00:05:31,310 --> 00:05:36,310
unless we also await this for which we would need to turn
111
00:05:36,640 --> 00:05:41,250
our Effect function here into an async function,
112
00:05:41,250 --> 00:05:43,470
which we're not allowed to do.
113
00:05:43,470 --> 00:05:45,510
Now, we could still work around that
114
00:05:45,510 --> 00:05:47,950
by putting that into a separate function,
115
00:05:47,950 --> 00:05:49,440
which we call thereafter
116
00:05:49,440 --> 00:05:51,940
so that we have separate functions,
117
00:05:51,940 --> 00:05:54,340
one for sending the HTTP request
118
00:05:54,340 --> 00:05:56,190
and one for error handling.
119
00:05:56,190 --> 00:05:58,460
And that be absolutely fine.
120
00:05:58,460 --> 00:06:02,980
But here, I think it's a bit easier to simply use the fact
121
00:06:02,980 --> 00:06:07,270
that it's a promise and add to catch method on it.
122
00:06:07,270 --> 00:06:08,367
Since that returns a promise,
123
00:06:08,367 --> 00:06:11,690
you can add then to handle success cases
124
00:06:11,690 --> 00:06:14,280
when the promise fulfills successfully.
125
00:06:14,280 --> 00:06:16,995
And we can add catch to handle errors
126
00:06:16,995 --> 00:06:19,860
coming up inside of the promise.
127
00:06:19,860 --> 00:06:21,950
And therefore, instead of try-catch here,
128
00:06:21,950 --> 00:06:25,320
we wanna catch the error we're getting
129
00:06:25,320 --> 00:06:29,050
and move that code here into this catch block.
130
00:06:29,050 --> 00:06:31,800
And that's the traditional promise only way
131
00:06:31,800 --> 00:06:34,500
of handling an error inside of a promise.
132
00:06:34,500 --> 00:06:38,020
And I hope I could make it clear why we need this here.
133
00:06:38,020 --> 00:06:40,810
With that, if we saved this, if I reload,
134
00:06:40,810 --> 00:06:45,010
we see loading briefly and then failed to fetch.
135
00:06:45,010 --> 00:06:47,480
And again, of course, we can definitely improve
136
00:06:47,480 --> 00:06:48,600
the error handling here.
137
00:06:48,600 --> 00:06:51,550
we can show a more to useful error message
138
00:06:51,550 --> 00:06:55,720
but here my focus is to show you how it generally works.
139
00:06:55,720 --> 00:06:58,220
Because what you now render on the screen,
140
00:06:58,220 --> 00:07:01,770
that's of course not related to HTTP requests
141
00:07:01,770 --> 00:07:02,950
or anything like that.
142
00:07:02,950 --> 00:07:06,473
Here, you can now build any user interface you want.
143
00:07:07,460 --> 00:07:11,730
But that therefore is how we could also handle errors.
144
00:07:11,730 --> 00:07:14,800
And with that, I'll read .json here
145
00:07:14,800 --> 00:07:16,720
to get rid of that error.
146
00:07:16,720 --> 00:07:19,040
And once we do that and we reload,
147
00:07:19,040 --> 00:07:22,453
we of course do load the data successfully again.
148
00:07:23,380 --> 00:07:27,910
And therefore, with that, we now implemented
149
00:07:27,910 --> 00:07:31,620
this third feature you're fetching, meals data.
150
00:07:31,620 --> 00:07:35,060
Now, let's work on a checkout and order form
151
00:07:35,060 --> 00:07:37,350
so that we can fetch some user data,
152
00:07:37,350 --> 00:07:39,520
which we then, as a last step,
153
00:07:39,520 --> 00:07:42,693
also submit to the server as an order.
11998
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.