1
00:00:00,300 --> 00:00:00,840
All right.

2
00:00:00,840 --> 00:00:07,710
And once we can successfully pass our error down to our error handler using next.

3
00:00:07,740 --> 00:00:09,000
Now let's deal with this.

4
00:00:09,000 --> 00:00:09,980
Try and catch.

5
00:00:09,990 --> 00:00:15,390
Because as you're looking at this controller, you're probably thinking, okay, so this is nice, but

6
00:00:15,390 --> 00:00:16,560
do I have to repeat this?

7
00:00:16,570 --> 00:00:19,770
Try and catch pretty much in every controller.

8
00:00:20,040 --> 00:00:25,320
And the answer is, well, kind of because it is very useful to use.

9
00:00:25,320 --> 00:00:29,010
Try and catch when you have these async operations.

10
00:00:29,250 --> 00:00:36,300
But there's also another way how we can do that, where essentially we can use a package that handles

11
00:00:36,300 --> 00:00:37,200
that for us.

12
00:00:37,350 --> 00:00:44,550
And as a result, we won't have to set up these trying catches in every controller.

13
00:00:44,700 --> 00:00:45,560
Effectively.

14
00:00:45,570 --> 00:00:49,740
That will be done automatically for us behind the scenes.

15
00:00:50,340 --> 00:00:52,440
Now, there's multiple ways how you can do that.

16
00:00:52,440 --> 00:00:54,990
Actually, you can set up the function yourself.

17
00:00:54,990 --> 00:01:01,680
And again, not to sound redundant, but we did that in the Node Express Project video.

18
00:01:01,710 --> 00:01:08,830
However, in this case we'll just go the fast route right away where we'll install the package.

19
00:01:08,850 --> 00:01:10,660
Now what package am I talking about?

20
00:01:10,680 --> 00:01:16,170
The package name is following Express async our package.

21
00:01:16,320 --> 00:01:22,930
And effectively by using this package we can avoid setting up these trick edge blocks.

22
00:01:22,950 --> 00:01:26,430
And again, the package will do all the heavy lifting behind the scenes.

23
00:01:26,430 --> 00:01:29,280
If you want to find out more info, here's the link.

24
00:01:29,310 --> 00:01:33,750
And before I navigate to the docs, I just want to install so everything is working.

25
00:01:33,750 --> 00:01:35,850
So let me copy and paste.

26
00:01:36,030 --> 00:01:38,820
That should be fast and let me go with npm start.

27
00:01:38,910 --> 00:01:44,760
And the way we use the package is following and these are the docs we install the package.

28
00:01:44,760 --> 00:01:45,830
We just did that.

29
00:01:45,840 --> 00:01:50,490
And essentially in the server you just want to require the package.

30
00:01:50,490 --> 00:01:54,660
Remember we're using a sex module, so syntax is going to be a little bit different in here.

31
00:01:54,660 --> 00:02:01,470
They're using require and they do have a notice about calling next, which at the moment probably is

32
00:02:01,470 --> 00:02:06,180
not going to make much sense since we're not going to implement that, since we're not checking for

33
00:02:06,180 --> 00:02:08,729
things in the controller directly.

34
00:02:08,850 --> 00:02:12,030
So in our case, it's going to work right from the get go anyway.

35
00:02:12,030 --> 00:02:18,060
But eventually, once we start checking for things in the controller instead of using next like we have

36
00:02:18,060 --> 00:02:19,140
in our code right now.

37
00:02:19,880 --> 00:02:22,850
We'll need to throw a new error.

38
00:02:22,880 --> 00:02:28,730
Again, at this point, it probably doesn't make much sense, but it will make sense once we start checking

39
00:02:28,730 --> 00:02:30,620
for things in the controller.

40
00:02:30,650 --> 00:02:36,140
And then, of course, I'll just remind you that it is because we are using the package.

41
00:02:36,170 --> 00:02:42,590
So what we want to do right now, well, we want to go to server JS and you know what?

42
00:02:42,590 --> 00:02:43,550
Actually, no.

43
00:02:43,580 --> 00:02:49,430
Let's go to our controller and just remove this try and catch and let's see whether our error gets passed

44
00:02:49,430 --> 00:02:52,060
to the error middleware regardless.

45
00:02:52,070 --> 00:02:53,180
So let me save it.

46
00:02:53,180 --> 00:02:57,350
We don't need to use next anymore so we can remove that sucker as well.

47
00:02:57,350 --> 00:03:00,590
And then let's navigate to a server.

48
00:03:00,860 --> 00:03:08,240
And then right after the dot and V, I'm going to go with import and we simply want to import a package.

49
00:03:08,360 --> 00:03:09,820
That's all we have to do.

50
00:03:09,830 --> 00:03:13,670
So we're going to go here with express async errors.

51
00:03:13,910 --> 00:03:18,680
And I mean, call me crazy, but I always like to restart the server.

52
00:03:18,710 --> 00:03:23,480
Don't ask me why, so I'm just going to go with NPM start just so I don't have to chase some dumb bugs.

53
00:03:23,630 --> 00:03:26,180
And now let's send one more time request from Postman.

54
00:03:26,180 --> 00:03:28,370
So let me navigate here again.

55
00:03:28,400 --> 00:03:32,630
Let me change this maybe around where I will provide the email, but the name will be missing.

56
00:03:32,630 --> 00:03:37,310
Just so you don't think that there's some trickery that I'm just making this up.

57
00:03:37,310 --> 00:03:40,580
So let me go with John at gmail.com.

58
00:03:41,840 --> 00:03:43,890
And then let me remove the name.

59
00:03:43,910 --> 00:03:45,920
So let me do it this way.

60
00:03:45,950 --> 00:03:47,340
Let's send it.

61
00:03:47,360 --> 00:03:51,150
And yep, I still have the error.

62
00:03:51,170 --> 00:03:51,680
Why?

63
00:03:51,710 --> 00:03:56,100
Well, because I didn't provide the name and I did install the package.

64
00:03:56,120 --> 00:03:59,940
So we're using the express async errors.

65
00:03:59,960 --> 00:04:05,240
And by doing so now, we don't need to set up those catch blocks.

66
00:04:05,240 --> 00:04:10,830
And we still pass on our error to our error handler middleware.

67
00:04:10,850 --> 00:04:16,339
And I can clearly see that in a console, because that's the error again, that I'm getting from the

68
00:04:16,339 --> 00:04:24,800
mongoose where essentially I have please provide a name since the name property was missing in my request.

