1
00:00:00,150 --> 00:00:01,200
In this video.

2
00:00:01,200 --> 00:00:03,630
Let's finish up with the error handling.

3
00:00:03,630 --> 00:00:11,340
So we are going to create a function whereby we are going to use it to throw an error and going to catch

4
00:00:11,340 --> 00:00:14,820
inside the global error handler here.

5
00:00:15,060 --> 00:00:23,130
So inside the utils, let's create one file and I'll call this one as app error dot JS.

6
00:00:23,460 --> 00:00:28,470
And for this we can use a class or a normal function.

7
00:00:28,470 --> 00:00:31,950
So let me stick to using normal function.

8
00:00:33,470 --> 00:00:43,340
So we're going to be as cost app error is equal to and for this error function here, we will pass in

9
00:00:43,340 --> 00:00:49,910
the message that we want to send to the global error handling and then the starters code.

10
00:00:50,000 --> 00:00:57,950
And for this, if we don't provide it, we have access to it inside our global error handler here as

11
00:00:57,950 --> 00:00:59,240
500.

12
00:00:59,240 --> 00:01:03,230
So the most important one here is the message.

13
00:01:03,230 --> 00:01:08,630
So we need to add the message to the error class in JavaScript.

14
00:01:08,630 --> 00:01:17,090
So you're going to make use of late and then the error is equal to new than the error class.

15
00:01:17,090 --> 00:01:21,620
And here it takes only one argument and that is a message.

16
00:01:21,890 --> 00:01:26,870
So here we can add additional properties like the stock trace.

17
00:01:26,870 --> 00:01:35,030
So here make use of error dot stock and is equal to the error dot stock.

18
00:01:37,030 --> 00:01:43,630
And the next one going to be the status code, going to be error, the status quo.

19
00:01:43,660 --> 00:01:48,670
I want to add this one also as status code.

20
00:01:49,000 --> 00:01:49,810
All right.

21
00:01:49,810 --> 00:01:53,770
So in case we have one, then you go ahead and use it.

22
00:01:54,340 --> 00:02:00,370
Otherwise, you're going to use 500, as we did inside the global zero hand line.

23
00:02:00,520 --> 00:02:05,890
The last step is that I need to return the error from here.

24
00:02:06,680 --> 00:02:14,500
As tax and less export module exports is equal to the app error.

25
00:02:14,510 --> 00:02:19,050
So before using it, let's look at how it's going to work.

26
00:02:19,070 --> 00:02:27,920
First of all, we have our global error handler here and next is that this handler is going to watch

27
00:02:27,950 --> 00:02:35,120
anywhere in our code where we are returning or catching any error is going to send back the response

28
00:02:35,120 --> 00:02:35,930
to the user.

29
00:02:35,930 --> 00:02:44,930
So this global error handler here is going to look up at the app error and before the global error handler

30
00:02:44,930 --> 00:02:53,390
will get what came back from the app error here, unless we pass what came back from this one to next

31
00:02:53,390 --> 00:02:58,490
and next, we'll pass that response to the global error handler.

32
00:02:58,820 --> 00:03:02,840
So back to the user controller and here we go.

33
00:03:02,870 --> 00:03:05,620
Let's require the app error.

34
00:03:05,630 --> 00:03:11,150
So let me try to reserve it so I can have the auto import.

35
00:03:13,530 --> 00:03:15,510
Wouldn't it be as app error?

36
00:03:15,810 --> 00:03:17,130
So here we go.

37
00:03:17,250 --> 00:03:23,940
So to be able to catch any error, we are going to pass next as a third argument to this.

38
00:03:25,930 --> 00:03:27,340
And inside.

39
00:03:27,340 --> 00:03:28,390
Let me explain it.

40
00:03:28,390 --> 00:03:38,830
And inside here, we are going to return from here and we are going to call next and are going to pass

41
00:03:38,830 --> 00:03:42,550
in the function call as an error.

42
00:03:42,580 --> 00:03:45,040
Then you pass in the message.

43
00:03:45,040 --> 00:03:51,730
So what we want to send is user already exists or pass in here as first element.

44
00:03:51,730 --> 00:03:57,190
And remember this one also take second argument that is status code.

45
00:03:57,190 --> 00:04:05,860
So here we can provide 404 or 403 but for the meantime I will leave the one and I need to remove this

46
00:04:05,860 --> 00:04:06,790
one from here.

47
00:04:07,150 --> 00:04:14,860
So now if I make the requests to register with the same email, let's see.

48
00:04:15,950 --> 00:04:24,500
And now we can see that we have a valid error catching handle and we have the starters and which is

49
00:04:24,500 --> 00:04:28,760
for this one as feel when I make use of something here.

50
00:04:30,950 --> 00:04:33,500
Save it and let's register.

51
00:04:34,570 --> 00:04:41,590
And you can see the starters is called something, meaning that you go back what came back from the

52
00:04:41,590 --> 00:04:42,620
global era?

53
00:04:42,710 --> 00:04:49,210
Angela Yang So let me say again and let's look at the actual error that we want.

54
00:04:49,390 --> 00:04:51,420
Well, we couldn't get the message.

55
00:04:51,430 --> 00:04:54,790
I think we did some typing mistake here.

56
00:04:55,210 --> 00:04:56,770
Let's look hard.

57
00:04:56,800 --> 00:05:00,550
It's supposed to be one as message as that.

58
00:05:00,550 --> 00:05:03,250
So now let's send the request.

59
00:05:04,030 --> 00:05:09,190
And here you can see that we have the message called user already exists.

60
00:05:09,190 --> 00:05:16,180
And this is where we throw the error, which is this, and we have the valid status code.

61
00:05:16,180 --> 00:05:19,030
So this is how we can handle any error.

62
00:05:19,060 --> 00:05:22,480
So anywhere in our code we can throw any error.

