1
00:00:00,760 --> 00:00:01,960
In this video.

2
00:00:01,960 --> 00:00:10,000
Let's see how we can handle saying errors in experts, for example, when dealing with databases.

3
00:00:10,150 --> 00:00:18,640
Because of that, we are going to handle some possible errors when working with MongoDB, for example,

4
00:00:18,640 --> 00:00:27,880
validation errors, duplicate key errors and invalid object ID errors, even though Mongoose handles

5
00:00:27,880 --> 00:00:30,370
these errors for us automatically.

6
00:00:30,370 --> 00:00:37,210
But what we want to do is that we want to send some meaningful messages to the end user.

7
00:00:37,210 --> 00:00:41,890
So let's start it inside the config folder.

8
00:00:42,040 --> 00:00:50,260
We have a file that connect to MongoDB and you can see that I am calling the function automatically

9
00:00:50,260 --> 00:00:52,750
here and without exporting it.

10
00:00:52,750 --> 00:00:57,580
So as soon as I require this file, this function will run.

11
00:00:57,580 --> 00:01:06,920
And inside the model we have one file called Postal JS and on that we have post schema and inside the

12
00:01:06,940 --> 00:01:12,910
schema we have to file title and content and these fields are required.

13
00:01:12,910 --> 00:01:13,420
Field.

14
00:01:13,570 --> 00:01:19,210
So next step is let's go ahead to require the function to connect to MongoDB.

15
00:01:19,240 --> 00:01:21,160
So back to the server file.

16
00:01:21,190 --> 00:01:22,780
Let's go ahead and do that.

17
00:01:22,780 --> 00:01:26,200
I have collapse or the route that we are done with it.

18
00:01:26,200 --> 00:01:32,740
So above here, this is how I'm going to require the file to connect to MongoDB.

19
00:01:34,710 --> 00:01:37,220
And make sure that your server is running.

20
00:01:37,230 --> 00:01:42,660
So let's check it out and you can see that it has been connected to MongoDB.

21
00:01:42,960 --> 00:01:53,970
The next step is let's require the model const posts is equal to required for a PS+ model and then the

22
00:01:53,970 --> 00:01:54,630
posts.

23
00:01:54,630 --> 00:01:57,240
So let's create a route to create post.

24
00:01:57,240 --> 00:02:01,620
So here we're going to be the end point post for a slash post.

25
00:02:01,620 --> 00:02:12,930
So here we go, app dot post, forward slash post, and then the handler requests response and my error

26
00:02:12,930 --> 00:02:17,250
function and let me mark my function as an async.

27
00:02:17,250 --> 00:02:23,340
So insert here I will bring in try and catch inside a patch.

28
00:02:23,340 --> 00:02:25,890
Let's send the error message.

29
00:02:26,720 --> 00:02:30,380
And instead of try, let's go ahead and create the post.

30
00:02:31,780 --> 00:02:34,780
I'll pass in the entire request, buddy.

31
00:02:35,290 --> 00:02:37,300
And here we go.

32
00:02:37,780 --> 00:02:40,750
To be able to receive incoming data from experts.

33
00:02:40,750 --> 00:02:48,550
We need to configure it by making use of upload to use and then Xpress dot Jason and that is it.

34
00:02:48,550 --> 00:02:53,530
So let's go ahead and send the response if everything goes on well.

35
00:02:55,490 --> 00:02:58,100
So let's go ahead and make the request.

36
00:02:58,100 --> 00:03:01,850
And I'm going to use this extension called ten lines.

37
00:03:02,970 --> 00:03:07,140
This is a route and then you're going to be the post mortem.

38
00:03:07,440 --> 00:03:13,530
So when I hit ST with that passing in the title and the contents, let's look at the arrow.

39
00:03:13,590 --> 00:03:15,990
Now this is what we have.

40
00:03:16,320 --> 00:03:22,020
Looking at the structure of this arrow is not all that descriptive to the end user.

41
00:03:22,050 --> 00:03:28,290
You can see we have nested objects before we can even get an actual message.

42
00:03:28,440 --> 00:03:35,910
So our job is that we are going to handle this error and display some meaningful error messages.

43
00:03:36,510 --> 00:03:40,290
Remember, we have created the Arrow class here.

44
00:03:40,320 --> 00:03:46,680
So any time we want to send some meaningful error messages, we are going to pass the arrow to this

45
00:03:46,680 --> 00:03:47,620
arrow class.

46
00:03:47,640 --> 00:03:55,110
So inside the cards we are going to use the error class or the function that we have created.

47
00:03:55,350 --> 00:04:03,030
Because of that, we need to pass in another element called Next because we are going to pass the error

48
00:04:03,030 --> 00:04:03,880
to next.

49
00:04:03,900 --> 00:04:12,840
So inside here we are going to avoid using this way, but instead we are going to call next and remember

50
00:04:12,840 --> 00:04:18,029
next accept the structured error, which is from the up arrow.

51
00:04:18,060 --> 00:04:23,010
So because of that, our make use of new and then our code, the app error.

52
00:04:23,010 --> 00:04:27,750
And here the first element is the error that we want to send.

53
00:04:27,780 --> 00:04:30,990
And that error can be found on this object.

54
00:04:30,990 --> 00:04:34,560
So here our pass in the entire error here.

55
00:04:34,920 --> 00:04:37,470
And then this is code 400.

56
00:04:37,500 --> 00:04:39,970
So with this one, let's look at the structure.

57
00:04:39,990 --> 00:04:49,140
Now, when I hit Saint and you can see that this message is clear than the previous one better so we

58
00:04:49,140 --> 00:04:56,760
can create its own function to have well-structured message than this one because this error that we

59
00:04:56,760 --> 00:04:59,310
pass in is a global error.

60
00:04:59,340 --> 00:05:04,590
But we can create a specific function to handle this kind of error.

61
00:05:04,590 --> 00:05:07,800
For example, validation error as we've seen.

62
00:05:08,220 --> 00:05:14,790
So the takeaway here is that if you want to handle async error, you need to bring it next here and

63
00:05:14,790 --> 00:05:20,100
then inside a catch you pass in the entire error to the app error class.

64
00:05:20,190 --> 00:05:26,040
Or we can create a global function where we are going to take away the catch.

65
00:05:26,170 --> 00:05:33,180
And so any time I want to cut any error, I don't want to bring in here, but instead I will wrap the

66
00:05:33,180 --> 00:05:37,660
entire function inside this global catch error handler.

67
00:05:37,680 --> 00:05:41,330
But to make it more simple, let's maintain in this way.

68
00:05:41,340 --> 00:05:46,820
So this is how we can handle async error in express in the next video.

69
00:05:46,830 --> 00:05:54,930
Let's go ahead and improve the way we are sending error, such as the validation error to the end user.

