1
00:00:01,550 --> 00:00:02,630
In this video.

2
00:00:02,630 --> 00:00:10,580
Let's go ahead and then improve this function a little bit by extracting the function to it on file.

3
00:00:10,580 --> 00:00:14,690
And then we're going to require it inside the server file.

4
00:00:14,690 --> 00:00:19,940
And that way we're going to have less code on the server JS file.

5
00:00:20,120 --> 00:00:23,750
And for this one tool, we can improve this one a little bit.

6
00:00:23,750 --> 00:00:29,030
So let's start with the how we can refactor this one and going to be pretty simple.

7
00:00:29,030 --> 00:00:34,580
Remember, this one is a middleware, so we are going to create a folder for that.

8
00:00:34,580 --> 00:00:36,920
So here we go in middle.

9
00:00:40,140 --> 00:00:43,890
Where and for me where we can have different types.

10
00:00:43,890 --> 00:00:49,920
For example, middleware for handling authentication or authorization, a middleware for handling errors.

11
00:00:49,920 --> 00:00:54,720
So I'm going to create one folder for arrows, and here we go.

12
00:00:54,720 --> 00:01:01,170
And I'll call this one as global error handler.

13
00:01:04,440 --> 00:01:07,490
Dart JS and inside is a fire.

14
00:01:07,500 --> 00:01:10,350
We are waiting to catch the call back here.

15
00:01:10,380 --> 00:01:16,020
So inside the to use, we are going to cut everything inside that.

16
00:01:16,200 --> 00:01:20,120
So when I cut it, you can see that everything has been removed.

17
00:01:20,130 --> 00:01:22,010
And now back to this file.

18
00:01:22,020 --> 00:01:27,180
I'm going to paste that one and I'm going to name it as const.

19
00:01:29,420 --> 00:01:30,260
Global.

20
00:01:32,200 --> 00:01:33,990
Error handler.

21
00:01:34,030 --> 00:01:37,210
The name is apt you when I save it.

22
00:01:37,210 --> 00:01:41,880
The next step is I need to export from this file module.

23
00:01:41,890 --> 00:01:47,670
Dot exports is equal to global error handler.

24
00:01:47,680 --> 00:01:48,520
Middle world.

25
00:01:48,910 --> 00:01:49,930
So that is it.

26
00:01:49,930 --> 00:01:54,880
So inside the server file I will go ahead and then require that.

27
00:01:54,880 --> 00:02:02,710
So here we go to next step is our pass as a callback function to this.

28
00:02:04,310 --> 00:02:04,850
As that.

29
00:02:04,850 --> 00:02:05,600
So save it.

30
00:02:05,600 --> 00:02:10,550
And now let's try again to initiate some error if we're going to happen.

31
00:02:10,550 --> 00:02:18,170
So for this one, let's send some wrong ID and let's send and indeed everything is working fine.

32
00:02:18,470 --> 00:02:22,010
The next step is about the photo for error handler.

33
00:02:22,010 --> 00:02:30,560
So back to the server file for this one to we can reduce the number of code here because we have the

34
00:02:30,560 --> 00:02:37,040
function called the app error, where we can create instance of error with a customized message.

35
00:02:37,640 --> 00:02:42,680
We can do the same thing inside the photo for error handler here.

36
00:02:42,680 --> 00:02:44,450
So this how are we going to do?

37
00:02:44,540 --> 00:02:50,090
We are going to create a variable and assign the entire message into that.

38
00:02:50,780 --> 00:02:55,580
So let's comment this one and let's begin with the implementation.

39
00:02:55,580 --> 00:02:56,780
So here we go.

40
00:02:56,780 --> 00:03:04,250
Cast error is equal to then I'll bring in the app error class.

41
00:03:04,520 --> 00:03:09,410
And then for this one, I'm going to pass in the message, which is this.

42
00:03:12,290 --> 00:03:21,230
So our policy as first agreement to this and lastly, I'll pass in the status code for oh four.

43
00:03:21,380 --> 00:03:28,820
So now I can remove this one and last that is, I need to pass this message to the next.

44
00:03:28,820 --> 00:03:30,560
So here we go.

45
00:03:30,830 --> 00:03:32,570
You're going to be us next.

46
00:03:32,600 --> 00:03:35,870
Now pass in the arrow and that is it.

47
00:03:36,620 --> 00:03:41,150
So let's go ahead and make a request to Iraq, which has been defined.

48
00:03:41,150 --> 00:03:51,620
So, for example, for a slash log E or for a slash account, if I hit send, you can see that I got

49
00:03:51,620 --> 00:03:55,130
this message for a slight account not found.

50
00:03:55,160 --> 00:03:58,360
Now everything is working fine.

