1
00:00:00,680 --> 00:00:01,850
In this video.

2
00:00:01,850 --> 00:00:07,310
Let's see how we can handle errors gracefully inside our application.

3
00:00:07,880 --> 00:00:13,130
And for error handling, we have a section where we talk about it in detail.

4
00:00:13,130 --> 00:00:18,110
So let's see how we are going to implement that one inside our application.

5
00:00:18,970 --> 00:00:19,390
All right.

6
00:00:19,390 --> 00:00:22,110
So let's see how we are going to implement that.

7
00:00:22,120 --> 00:00:30,280
So inside the metaverse, I'm going to create one file called Global Error handler for error handling

8
00:00:30,280 --> 00:00:31,300
in experts.

9
00:00:31,300 --> 00:00:37,180
This how we created count and going to be as global.

10
00:00:39,050 --> 00:00:43,010
Is equal to function core as that.

11
00:00:43,340 --> 00:00:53,180
And inside here, instead of two or three arguments, we are going to parse in for a given error request

12
00:00:53,510 --> 00:00:54,620
response.

13
00:00:54,620 --> 00:00:55,940
And next.

14
00:00:56,060 --> 00:01:04,340
So any time experts see this kind of function, it health experts that this is error handling.

15
00:01:04,760 --> 00:01:13,190
So inside here we are going to return the error that has been okay inside our application.

16
00:01:13,190 --> 00:01:17,390
So let's have the properties that we want to send back to the user.

17
00:01:17,390 --> 00:01:19,700
First one going to be the starters.

18
00:01:19,700 --> 00:01:28,280
And for this we can send our custom message, for example, failed or something happened.

19
00:01:30,930 --> 00:01:37,260
Or server error so we can send any type of these messages.

20
00:01:37,500 --> 00:01:41,730
The next one is going to be the actual message.

21
00:01:41,730 --> 00:01:48,280
And this one, who is the actual error that has occurred inside our application?

22
00:01:48,300 --> 00:01:55,830
For example, if a user is trying to log in with invalid login credentials, we have access to that

23
00:01:55,830 --> 00:02:04,380
on the message and I will show you as you move on the next one going to be the stack the stack represents

24
00:02:04,380 --> 00:02:10,889
which file a particular error occurred and which line of code that error occurred.

25
00:02:10,919 --> 00:02:13,780
So let's build up these properties.

26
00:02:13,780 --> 00:02:22,620
So we're going to be as const stock is equal to and we have access to this properties on the error that

27
00:02:22,620 --> 00:02:23,500
we threw.

28
00:02:23,520 --> 00:02:28,560
So inside the controls, let me show you where we are throwing an error.

29
00:02:28,590 --> 00:02:31,580
Let's get back to the users, which is this.

30
00:02:31,590 --> 00:02:33,330
We can see that here.

31
00:02:33,480 --> 00:02:39,810
If a user is being found, we are through in what is called status with this and then the message.

32
00:02:39,810 --> 00:02:45,260
So we have access to this properties inside the global arrow handler.

33
00:02:45,270 --> 00:02:52,050
But before we can have access to the arrows inside the global arrow handler here, unless we create

34
00:02:52,050 --> 00:02:53,610
a specific function for it.

35
00:02:53,610 --> 00:02:58,080
For the meantime, let's see how we can cut and send in the arrow.

36
00:02:58,110 --> 00:02:58,590
All right.

37
00:02:58,590 --> 00:03:07,080
So here on the arrow that came back, we have access to the stock as error dot stock.

38
00:03:07,590 --> 00:03:13,350
The next one going to be the actual message, cast message.

39
00:03:13,350 --> 00:03:25,650
And we have it on the error dot message and the starter is going to be starters is equal to the error.

40
00:03:27,890 --> 00:03:30,770
Dart starters.

41
00:03:30,770 --> 00:03:39,080
So in case a user provides starters, then you go ahead and use the error starters.

42
00:03:39,970 --> 00:03:47,790
Otherwise we are going to use the default or failed and we can also send the status code.

43
00:03:47,800 --> 00:03:53,890
So it's going to be status code is equal to the error.

44
00:03:53,890 --> 00:04:03,160
If the user provide the status code upon throwing an error, we are going to use the error dot status

45
00:04:03,160 --> 00:04:03,790
code.

46
00:04:03,880 --> 00:04:11,830
Otherwise we use 500 million server error and let's send the response.

47
00:04:12,130 --> 00:04:21,959
It's going to be as red dot status and we are going to use the status code and we are going to chain

48
00:04:21,970 --> 00:04:31,780
with the JSON and we are going to send the message, then the stock and then the starters about the

49
00:04:31,780 --> 00:04:32,320
message.

50
00:04:32,320 --> 00:04:34,060
So here, let me rearrange.

51
00:04:36,240 --> 00:04:38,220
This one status.

52
00:04:38,220 --> 00:04:41,760
So let me bring the starters before the message.

53
00:04:42,330 --> 00:04:44,220
The arrangement doesn't matter here.

54
00:04:44,430 --> 00:04:47,700
Last step is we need to export module.

55
00:04:47,700 --> 00:04:51,750
The exports is equal to global error handler.

56
00:04:51,840 --> 00:04:53,970
So let's make use of that.

57
00:04:53,970 --> 00:05:01,770
So let's get back to the set of file, which is this, and let's require the global error handler.

58
00:05:02,010 --> 00:05:07,800
And like I said, we need to bring the error handler below all your route.

59
00:05:07,800 --> 00:05:16,650
And this is a comment we have down here and make use of app don't use the pass in the global arrow handler.

60
00:05:17,400 --> 00:05:23,850
This one anywhere in our code where we throw an error are going to have access to that and send back

61
00:05:23,850 --> 00:05:25,620
as a response to the user.

62
00:05:25,620 --> 00:05:32,040
So let's try to register a user with the same details and let's see what we're going to have.

63
00:05:32,220 --> 00:05:37,230
And you can see that we have what is called user already exists.

64
00:05:37,230 --> 00:05:45,300
And this is not an error because here we have the status quo, meaning everything is okay and we go

65
00:05:45,300 --> 00:05:48,420
back this and this is an error.

66
00:05:48,420 --> 00:05:57,180
So let's see how we can through rare error or some rare error whereby our global error handler will

67
00:05:57,210 --> 00:05:57,960
catch it.

68
00:05:57,990 --> 00:06:00,210
Let's get into that in the next video.

