1
00:00:01,350 --> 00:00:02,460
In this video.

2
00:00:02,460 --> 00:00:06,810
Let's see how we can handle invalid object ID.

3
00:00:06,840 --> 00:00:09,690
Let me show you an example of such error.

4
00:00:09,900 --> 00:00:17,760
Inside the server file I have added one root where we are going to fetch a particular post by ID and

5
00:00:17,760 --> 00:00:26,250
I pass in the query as red dot params dot id and I'm sending the response for success and error here.

6
00:00:26,280 --> 00:00:33,510
So with this one, let's try to make a request and see the kind of error if we fail to pass in the valid

7
00:00:33,510 --> 00:00:34,950
MongoDB ID.

8
00:00:35,280 --> 00:00:42,420
So inside our request here I have added one endpoint call for a slot post and then forwards like the

9
00:00:42,420 --> 00:00:43,170
ID.

10
00:00:43,200 --> 00:00:47,900
And let me add any ID here to be first when I hit send.

11
00:00:47,910 --> 00:00:55,080
And here is a message you can see that we have some random string of characters here and this message

12
00:00:55,080 --> 00:00:59,460
is not all that descriptive, even if you are new to MongoDB.

13
00:00:59,490 --> 00:01:06,300
So we are going to customize this one and take this message and add our own custom message to it.

14
00:01:06,960 --> 00:01:13,860
And for this message, we can see that the type of the error is called cast error.

15
00:01:14,010 --> 00:01:19,520
So we can create a function called handle cast error to handle this.

16
00:01:19,530 --> 00:01:23,370
So back to our folder that is under utils.

17
00:01:23,400 --> 00:01:31,730
We can create one file to contain all this error types or we can create a specific file for each.

18
00:01:31,740 --> 00:01:34,590
So let's break it down into error types.

19
00:01:34,590 --> 00:01:37,260
So here our mix of handle.

20
00:01:40,490 --> 00:01:41,300
Past.

21
00:01:43,160 --> 00:01:49,850
Error dot JS and this is how we are going to create its cost handle.

22
00:01:53,870 --> 00:01:57,220
Test error is equal to differently.

23
00:01:57,230 --> 00:02:02,120
Our pass in the entire error object here as we did before.

24
00:02:02,150 --> 00:02:03,530
Here we go.

25
00:02:03,530 --> 00:02:10,970
So here let's add our custom error message and going to be as const, and then I'll assign to a variable

26
00:02:11,000 --> 00:02:12,080
called error.

27
00:02:12,350 --> 00:02:17,900
And here we go back tick and then our add invalid.

28
00:02:17,990 --> 00:02:23,570
And then to be able to know, I want to grab the path and the value.

29
00:02:23,600 --> 00:02:28,820
And the path is the ID and the value is what is this one?

30
00:02:28,820 --> 00:02:35,410
So I can combine these two to send some meaningful message than using this message here.

31
00:02:35,420 --> 00:02:38,780
So this I'm going to do for the error objects.

32
00:02:38,780 --> 00:02:45,800
We have the path as the last sign Kerberos is the error and then dot path.

33
00:02:46,130 --> 00:02:54,650
And next is I will add colon and then I will inject the value for that going to be as the error.

34
00:02:54,650 --> 00:02:57,380
And on that we have the value.

35
00:02:57,380 --> 00:03:01,970
So here I need to send this error to the global error handler.

36
00:03:01,970 --> 00:03:05,030
So I'll bring in the app error here.

37
00:03:05,870 --> 00:03:10,100
So I think the auto import is not working to make sure that it's working.

38
00:03:10,130 --> 00:03:15,380
Go ahead and then save again and come back to the file where you want to import.

39
00:03:15,380 --> 00:03:17,570
And I think it should be working.

40
00:03:17,660 --> 00:03:19,430
And this is the magic.

41
00:03:19,460 --> 00:03:20,300
It's working.

42
00:03:20,300 --> 00:03:25,910
So here I'm going to send the entire response as message to cast.

43
00:03:25,910 --> 00:03:29,630
And then the message is going to be as this.

44
00:03:29,630 --> 00:03:33,470
So I will call the instance of the app error.

45
00:03:33,500 --> 00:03:40,280
Then I'll pass in the message that the customized message and then the status code, which is 400.

46
00:03:41,090 --> 00:03:45,190
And next is let me return the entire message.

47
00:03:45,200 --> 00:03:54,020
So last step is let's go ahead and then export this file or this function as handle cast error.

48
00:03:54,170 --> 00:04:00,020
So let's get back to the thing and pass in the error.

49
00:04:00,020 --> 00:04:00,310
Rather.

50
00:04:00,320 --> 00:04:02,710
Sorry, you're going to be the error here.

51
00:04:02,720 --> 00:04:03,200
Yeah.

52
00:04:04,040 --> 00:04:04,390
Good.

53
00:04:04,400 --> 00:04:10,520
So inside the root where I want to handle such cases, that is the where is the server file?

54
00:04:10,760 --> 00:04:11,820
Which is this one.

55
00:04:11,840 --> 00:04:16,490
Let me close this up error and this is where I want to handle such error.

56
00:04:16,579 --> 00:04:20,480
So here I'll bring in the cost error here.

57
00:04:20,480 --> 00:04:26,630
So let me require it here as handle cast error.

58
00:04:27,050 --> 00:04:35,930
The auto import is working fine and inside here, as we did for the handle validation error, we are

59
00:04:35,930 --> 00:04:37,370
going to do the same things.

60
00:04:37,370 --> 00:04:41,750
Our call next and I'll pass in the handle cast.

61
00:04:41,750 --> 00:04:46,740
Then I'll pass in the entire error object and that is it.

62
00:04:46,760 --> 00:04:50,480
So now let's go ahead and make a request and see.

63
00:04:50,480 --> 00:04:57,230
And this time around you can see that we have some meaningful message as invalid ID, which is this

64
00:04:57,230 --> 00:05:00,710
one if I pass in the correct ID.

65
00:05:01,560 --> 00:05:07,090
So let's make a request to create a post so that we can have the valid ID.

66
00:05:07,110 --> 00:05:16,170
So let's pass in the payload as that title and I will say h HTML and then for the content.

67
00:05:22,150 --> 00:05:24,070
I would say the best.

68
00:05:26,220 --> 00:05:27,840
So let's end.

69
00:05:27,840 --> 00:05:34,740
And now it has been created and let me copy the ID and get back to the single here and make requests.

70
00:05:34,740 --> 00:05:37,130
And now it works fine.

71
00:05:37,140 --> 00:05:43,560
So these include any time you want to handle a customized error for a specific error type.

72
00:05:43,560 --> 00:05:50,760
So first of all, look at the error type and see how we can customize it after that, send that customized

73
00:05:50,760 --> 00:05:55,290
message to the global error handler and you'll be good to go.

