1
00:00:00,570 --> 00:00:01,800
In this video.

2
00:00:01,800 --> 00:00:07,090
Let's go ahead and improve the way we are handling this kind of error.

3
00:00:07,110 --> 00:00:09,360
That is the validation error.

4
00:00:09,750 --> 00:00:16,620
Remember, the global error handler handles any error that might occur in our application.

5
00:00:16,950 --> 00:00:23,910
But let's say that we want to handle a specific error type, for example, validation error, and we

6
00:00:23,910 --> 00:00:28,710
want to send some customized message based on this kind of error.

7
00:00:28,890 --> 00:00:36,000
In that case, we can create a function to do that, or we can maintain this way because this one is

8
00:00:36,000 --> 00:00:38,420
more clear than the before.

9
00:00:38,430 --> 00:00:43,810
But let me show you how we can create a specific error message for specific error type.

10
00:00:43,830 --> 00:00:47,830
So here we go inside the utils folder.

11
00:00:47,850 --> 00:00:51,150
Let's create one file core handle.

12
00:00:53,770 --> 00:01:01,570
Validation error in MongoDB DB for short as that.

13
00:01:01,690 --> 00:01:16,090
So here is a function const handle validation error DB is equal to error function.

14
00:01:16,270 --> 00:01:23,590
For this function we are going to pass in the entire error object as we see in the catch here.

15
00:01:23,920 --> 00:01:30,760
So for that object we are going to extract some properties and add our custom message to it.

16
00:01:30,760 --> 00:01:33,130
So our pass in the error here.

17
00:01:33,430 --> 00:01:40,270
And before we continue, let's go ahead and continue that log, the error that we passing here upon

18
00:01:40,270 --> 00:01:41,440
calling the function.

19
00:01:41,590 --> 00:01:46,210
So let's export the function here as that.

20
00:01:46,210 --> 00:01:51,700
So now inside this file, let's go ahead and require the function.

21
00:01:54,100 --> 00:02:00,760
So inside this route that is on the catch, let's remove this one for now or we can comment that one

22
00:02:00,760 --> 00:02:05,830
and we call the function here and then we pass in the entire error.

23
00:02:06,280 --> 00:02:10,449
So let's check our terminal after we make the request.

24
00:02:10,449 --> 00:02:11,920
So here we go.

25
00:02:12,310 --> 00:02:13,840
Our head send.

26
00:02:15,560 --> 00:02:19,060
And you can see that this is an error object.

27
00:02:19,070 --> 00:02:25,880
And what we want is that we want to extract the properties and value and customize it.

28
00:02:26,450 --> 00:02:30,360
So inside a function, let's see how we can get that one done.

29
00:02:30,380 --> 00:02:38,540
We are going to create an object called arrows, and it's equal to we're going to use object values

30
00:02:38,540 --> 00:02:41,840
to get the values from the properties of the arrow.

31
00:02:42,110 --> 00:02:46,370
So make use of objects, dot values.

32
00:02:46,670 --> 00:02:53,330
So here we pass in the entire arrow objects, and on that we have a property called arrows.

33
00:02:53,720 --> 00:02:57,800
Next step is let's go ahead and look through, use it map.

34
00:02:57,890 --> 00:03:01,880
And for this one, I have access to the individual element.

35
00:03:01,880 --> 00:03:04,850
And on that we have what is called message.

36
00:03:06,530 --> 00:03:10,970
So the next step is that I'm going to create a variable core message.

37
00:03:11,690 --> 00:03:17,120
And on that, I'm going to add my custom message by saying that invalid.

38
00:03:20,970 --> 00:03:21,960
Imputes.

39
00:03:24,290 --> 00:03:25,070
Data.

40
00:03:25,580 --> 00:03:33,950
And then I would inject the actual message and I'm going to use dollar sign to replaces the entire error

41
00:03:33,950 --> 00:03:34,820
objects.

42
00:03:34,820 --> 00:03:40,100
Then I'm going to join with dot as that.

43
00:03:40,950 --> 00:03:46,080
Next step is let's return the message from this function.

44
00:03:46,320 --> 00:03:52,980
The next step is that inside the server file, let's go ahead and then require that call handler validation

45
00:03:52,980 --> 00:03:53,520
error.

46
00:03:53,550 --> 00:03:55,170
We have it already.

47
00:03:55,170 --> 00:03:57,420
So now here is the handler.

48
00:03:57,420 --> 00:04:05,430
So if I go ahead and send the request now, let's see, you can see that it is loading because we are

49
00:04:05,430 --> 00:04:08,340
not retaining or passing to the next function.

50
00:04:08,610 --> 00:04:17,100
So what we need to do is that we are going to convert or pass this message to the global error handler.

51
00:04:17,250 --> 00:04:21,029
So we are going to bring in the app error class here.

52
00:04:21,540 --> 00:04:22,470
App error.

53
00:04:22,950 --> 00:04:26,160
And before sending, I want to customize it.

54
00:04:26,160 --> 00:04:32,100
So here I'll make use of new and then create instance of the app error.

55
00:04:32,190 --> 00:04:38,460
And next I'll pass in the message, the one that I've customized, which is this one and next is I'll

56
00:04:38,460 --> 00:04:41,100
pass in the status quo score 400.

57
00:04:41,340 --> 00:04:48,240
So for this one, we are sending our customized message to the global arrow handler as that.

58
00:04:48,600 --> 00:04:55,110
So what we are going to do is that we are going to send this message to the next handler.

59
00:04:55,110 --> 00:04:56,490
So here we go.

60
00:04:56,490 --> 00:05:00,450
Let's remove everything from here and I'll bring it next.

61
00:05:00,450 --> 00:05:03,990
And for that I'll pass in the function as that.

62
00:05:04,320 --> 00:05:08,280
So when I say it now, let's go ahead and make the request.

63
00:05:08,280 --> 00:05:12,660
And this time around you can see that we have our custom message code invalid.

64
00:05:12,660 --> 00:05:18,480
I think I made some type of mistake here, so here is going to be as invalid.

65
00:05:19,510 --> 00:05:20,140
As that.

66
00:05:20,140 --> 00:05:28,000
So let's see, when I hit ST and you can see that we have a customized message that is invalid input

67
00:05:28,000 --> 00:05:28,720
data.

68
00:05:28,720 --> 00:05:34,450
So if we want to maintain by using the global method of 100 arrows, we can do that.

69
00:05:34,450 --> 00:05:41,380
Or if we want to stick to a custom message for a specific arrow type, then this is a way to go.

70
00:05:41,560 --> 00:05:42,910
So the next video.

71
00:05:42,910 --> 00:05:48,010
Let's see how we can handle Arrow for example, invalid object ID.

72
00:05:48,040 --> 00:05:49,420
Let's get into that.

73
00:05:49,440 --> 00:05:50,500
The next video.

