1
00:00:01,900 --> 00:00:03,040
In this video.

2
00:00:03,040 --> 00:00:09,250
Let's go ahead and refactor our error handling inside our root.

3
00:00:09,460 --> 00:00:15,880
You can see that any time you want to throw an error unless you go through this process.

4
00:00:15,880 --> 00:00:20,080
So let's find a way where we can centralize this one.

5
00:00:20,080 --> 00:00:23,230
So let's go ahead and create a folder for this.

6
00:00:23,230 --> 00:00:26,440
So I'm going to create a folder called Utils.

7
00:00:27,270 --> 00:00:32,729
And inside I'm going to create a file called App Error.

8
00:00:33,000 --> 00:00:38,610
I make use of uppercase because we are going to create this one using a class.

9
00:00:38,610 --> 00:00:45,270
So in case you are not familiar with classes in JavaScript, don't worry, I will create a different

10
00:00:45,270 --> 00:00:49,410
function where we are going to use normal function syntax.

11
00:00:49,410 --> 00:00:55,290
So first let's use normal function syntax here and then after that I'm going to show you how we can

12
00:00:55,290 --> 00:01:00,120
make use of classes in JavaScript to achieve the same concept.

13
00:01:00,120 --> 00:01:01,470
So here we go.

14
00:01:01,500 --> 00:01:07,500
Before we start the implementation, let's look at the arguments or the properties the function needs.

15
00:01:07,680 --> 00:01:14,340
First of all, it needs the status code, the message, and this are the most important thing here.

16
00:01:14,370 --> 00:01:21,330
For starters, we always know it is failed so we can choose not to provide and we can give it as a default

17
00:01:21,330 --> 00:01:22,710
value for that.

18
00:01:22,710 --> 00:01:28,650
So let's send this to arguments starters and then the message.

19
00:01:28,740 --> 00:01:30,330
All right, So here we go.

20
00:01:30,330 --> 00:01:37,050
So make use of costs and then the app error is equal to error function.

21
00:01:37,050 --> 00:01:45,540
And for this function, I'm going to take in the message of the error and then the status code that

22
00:01:45,540 --> 00:01:46,890
we want to provide.

23
00:01:48,410 --> 00:01:57,200
So inside here we are going to create instance of the error from the error class, which is equal to

24
00:01:57,200 --> 00:02:00,140
new and then the error.

25
00:02:00,410 --> 00:02:07,130
And for this we are going to pass in the message because for this error, the only argument it takes

26
00:02:07,130 --> 00:02:09,590
is a message we pass into this class.

27
00:02:09,740 --> 00:02:13,670
So now here next is let's add the status code.

28
00:02:13,670 --> 00:02:21,950
So the error dot status code, which is equal to the status code that we pass in.

29
00:02:22,250 --> 00:02:26,420
Lastly, we need to return the value for this function.

30
00:02:26,420 --> 00:02:32,060
So going to be the error and let's export this module.

31
00:02:32,210 --> 00:02:36,290
Dart's export is equal to our error.

32
00:02:36,440 --> 00:02:42,560
Now inside the server file, any way I want to throw an error, I'm going to call that function.

33
00:02:42,560 --> 00:02:52,010
So let's require the error function here and now we are going to remove all this code from here and

34
00:02:52,040 --> 00:02:54,560
are going to call the app error.

35
00:02:54,560 --> 00:02:58,670
Then you pass in our message, which is network.

36
00:02:59,580 --> 00:03:00,230
Error.

37
00:03:00,240 --> 00:03:05,460
And then the second argument is going to be the status code, which is 500.

38
00:03:05,610 --> 00:03:10,170
So what comes back from this one is the actual error.

39
00:03:10,170 --> 00:03:19,080
So cost the error here and next is going to pass in to the next maneuver, which is down here.

40
00:03:19,200 --> 00:03:22,440
So now let's make the request and let's see.

41
00:03:22,440 --> 00:03:25,500
We can see that we have network error.

42
00:03:25,500 --> 00:03:33,240
And then with the status code 500, that is internal server error and we can call the same function

43
00:03:33,240 --> 00:03:34,530
here in any root.

44
00:03:34,530 --> 00:03:36,840
For example, this log in here.

45
00:03:36,870 --> 00:03:45,930
So now I'm going to call the function here and here the message is going to be as you are not logged

46
00:03:45,930 --> 00:03:49,950
in and 401 meaning not authorized.

47
00:03:49,980 --> 00:03:54,600
Now let's go to the file and let's hit send.

48
00:03:54,930 --> 00:03:56,660
And there we go.

49
00:03:56,670 --> 00:03:57,900
We got a message.

50
00:03:57,900 --> 00:04:03,510
But the problem is we are not getting the status code is always 500.

51
00:04:03,690 --> 00:04:05,580
So let's see what is wrong.

52
00:04:06,300 --> 00:04:07,020
Oh, sorry.

53
00:04:07,020 --> 00:04:11,030
We need to send the error for this function call.

54
00:04:11,040 --> 00:04:13,410
So now let's remove this one.

55
00:04:13,410 --> 00:04:16,649
And next is let's pass in the error here.

56
00:04:16,860 --> 00:04:18,750
So let's make the request.

57
00:04:18,839 --> 00:04:22,740
And we got the status code for one with a message.

58
00:04:22,740 --> 00:04:25,920
You are not logged in so well from this one.

59
00:04:26,100 --> 00:04:29,790
Let me make sure I type in the correct.

60
00:04:29,820 --> 00:04:30,290
Yeah.

61
00:04:31,020 --> 00:04:31,290
Okay.

62
00:04:31,380 --> 00:04:33,510
So again, let me show you one more time.

63
00:04:33,510 --> 00:04:35,460
And indeed, we have it.

64
00:04:35,610 --> 00:04:40,890
So this is one way where we can create a function to create an error.

65
00:04:40,890 --> 00:04:45,450
But if we want to use class based one, let me show you this one or so.

66
00:04:45,480 --> 00:04:47,820
So let me comment this one.

67
00:04:48,180 --> 00:04:49,680
So here we go.

68
00:04:49,710 --> 00:04:57,150
Make use of class and then uppercase up error because we are going to create an instance of this error

69
00:04:57,240 --> 00:05:02,310
and we need to extend from the error class as that.

70
00:05:02,610 --> 00:05:04,050
And next is I don't.

71
00:05:04,860 --> 00:05:08,730
And next is our brain and mind function called constructor.

72
00:05:08,790 --> 00:05:16,200
And it's going to be called when an object is created from this class and I'll pass in the message of

73
00:05:16,200 --> 00:05:19,890
the error and then the status code also.

74
00:05:20,920 --> 00:05:23,890
Next is we will call the super.

75
00:05:23,920 --> 00:05:29,100
Then you pass in the message because for this it takes in only one agreement.

76
00:05:29,110 --> 00:05:30,370
That is a message.

77
00:05:30,370 --> 00:05:37,450
So let's go ahead and add the status code to it, which is equal to the status code.

78
00:05:37,450 --> 00:05:39,910
So let's go ahead and export this one.

79
00:05:40,480 --> 00:05:44,380
We can remove this one and make use of app error here.

80
00:05:44,530 --> 00:05:49,150
So next is inside any route I want to call, I'm going to make use of that.

81
00:05:49,150 --> 00:05:53,110
So let me change this one to uppercase app error.

82
00:05:53,110 --> 00:06:00,470
So next is let's modify this function or let's change this one to uppercase A, And because it's a class,

83
00:06:00,470 --> 00:06:04,030
we're going to make use of new to create an instance of this class.

84
00:06:04,030 --> 00:06:05,320
So now that is it.

85
00:06:05,320 --> 00:06:09,010
So let's go ahead and make the requests and now let's check it out.

86
00:06:09,010 --> 00:06:13,090
You can see that network error and is working fine.

87
00:06:13,180 --> 00:06:15,550
The same thing applies to the profile.

88
00:06:15,760 --> 00:06:22,780
Make use of this uppercase up error and then name equal to that.

89
00:06:22,780 --> 00:06:33,010
And let's send the requests and we have the network error and also we call something stock trace stock

90
00:06:33,010 --> 00:06:38,290
trace is a code on a file where that particular error occurs.

91
00:06:38,290 --> 00:06:42,430
So for this one, we can see that it is giving us the message.

92
00:06:42,430 --> 00:06:49,330
But if you want to know what file does this error okay, you're going to use what is called the stock

93
00:06:49,330 --> 00:06:49,960
trace.

94
00:06:49,960 --> 00:06:57,250
So inside the error handler here where we are catching the error, we can also throw the stock trace

95
00:06:57,250 --> 00:06:57,670
also.

96
00:06:57,670 --> 00:06:59,500
So here are going to be the stock.

97
00:07:00,260 --> 00:07:05,390
And we have access to the error dot stock.

98
00:07:06,060 --> 00:07:08,790
Now save it and let's make the request.

99
00:07:08,790 --> 00:07:15,660
And now we can see that this time around I get beautiful message with the stock, meaning the file that

100
00:07:15,660 --> 00:07:17,960
this error occurred and what line.

101
00:07:17,970 --> 00:07:24,270
So if you look through, have a look can say that this is the file and then if you look through it,

102
00:07:24,270 --> 00:07:28,530
you're going to see the particular line that this particular error.

103
00:07:28,530 --> 00:07:29,280
Okay.

104
00:07:29,490 --> 00:07:33,060
So it's somewhere unless you take your time and then look through.

105
00:07:33,060 --> 00:07:38,970
Well, you can see that it starts from here and then it ends somewhere here.

106
00:07:38,970 --> 00:07:44,070
That is my server dot just filed on line number ten.

107
00:07:44,160 --> 00:07:48,810
So if I check my server file, line number ten, let's see.

108
00:07:48,810 --> 00:07:50,970
And this is where the error.

109
00:07:50,970 --> 00:07:56,370
Okay, so start trace, help us to debug our code where a particular error.

110
00:07:56,370 --> 00:07:57,030
Okay.

111
00:07:57,980 --> 00:08:04,640
So the next step is how can we handle asynchronous errors, for example, when we are dealing with database?

112
00:08:04,640 --> 00:08:08,210
So let me show you how we are going to do this one in the next video.

