1
00:00:00,330 --> 00:00:07,970
And once the postman is ready to go and before we deep dive into the Mangoush now, let me showcase

2
00:00:07,980 --> 00:00:16,110
something in our application where if you take a look at the middleware, you'll notice that there is

3
00:00:16,110 --> 00:00:17,430
now a single wrapper.

4
00:00:18,000 --> 00:00:21,120
And if you remember, we needed to use something.

5
00:00:21,570 --> 00:00:25,800
Either we set up try catch pretty much in every route.

6
00:00:26,130 --> 00:00:26,580
Correct.

7
00:00:26,970 --> 00:00:30,980
Or of course, the other option was using that async wrapper.

8
00:00:31,470 --> 00:00:36,930
But when we were setting it up, I also mentioned that there are packages that do that for us.

9
00:00:37,320 --> 00:00:44,490
And that's why if you take a look at the package on, you know, this package, the express async errors,

10
00:00:44,820 --> 00:00:52,320
and essentially the only thing we need to do is just go back to App Jass online, where we have a comment

11
00:00:52,320 --> 00:00:53,700
for async errors.

12
00:00:54,120 --> 00:00:55,580
Just import the package.

13
00:00:55,620 --> 00:00:55,970
That's it.

14
00:00:55,980 --> 00:00:57,030
That's all we have to do.

15
00:00:57,300 --> 00:00:58,200
So let's go here.

16
00:00:58,200 --> 00:00:59,790
A choir and line.

17
00:00:59,790 --> 00:01:05,459
Of course, we're looking for a package of express async errors and just stay on the safe side.

18
00:01:05,459 --> 00:01:09,690
I'll stop the server and I'll open it up one more time.

19
00:01:10,080 --> 00:01:17,760
And now you'll notice that if I go back to the product one and or I'm sorry, the product in the controller,

20
00:01:18,120 --> 00:01:19,560
I'm actually going to close the routes.

21
00:01:19,570 --> 00:01:24,630
I don't think we'll need that anymore as well as the objects were pretty much done over here.

22
00:01:24,930 --> 00:01:32,100
And then if in any of the controllers, you'll go here and throw the error, which you'll notice, that

23
00:01:32,100 --> 00:01:36,770
will actually be able to access that error in our custom error handler.

24
00:01:37,050 --> 00:01:44,160
And essentially what that means is that whenever Mangoush is going to spin back that error, unlike

25
00:01:44,160 --> 00:01:50,370
the previous time where initially we set up trying and catch and then we built our own async wrapper,

26
00:01:50,580 --> 00:01:52,930
in this case, the package takes care of that.

27
00:01:53,130 --> 00:02:00,720
So in here, let me just go throw and I'll call this new error and I'll say testing package, more testing,

28
00:02:00,720 --> 00:02:05,340
I think errors, whatever you want, testing, async errors.

29
00:02:05,700 --> 00:02:11,610
And just to showcase that in the error handler, I have the console log for error.

30
00:02:11,880 --> 00:02:15,940
And of course, in here I'm sending back some kind of hard coded message.

31
00:02:16,200 --> 00:02:22,550
So first, what I want to do is from the postman navigate to my static one just to show.

32
00:02:23,090 --> 00:02:30,210
Of course, we are catching those errors nicely with our package and then I'll cover the package in

33
00:02:30,210 --> 00:02:30,770
more detail.

34
00:02:31,050 --> 00:02:35,570
So let me go back to the postman, unfortunately, all the way on the left hand side.

35
00:02:35,580 --> 00:02:37,470
So there's a little bit of jump in here.

36
00:02:37,740 --> 00:02:39,630
And I'm looking for the static one.

37
00:02:39,890 --> 00:02:43,120
I simply want to invoke the route and now check it out.

38
00:02:43,290 --> 00:02:44,560
Something went wrong.

39
00:02:44,850 --> 00:02:46,380
Please try again.

40
00:02:46,830 --> 00:02:54,770
And if I ever get back all the way here in the bottom, I should see error testing, async errors.

41
00:02:54,990 --> 00:03:01,450
And if you're interested in finding out more about the package, just go to Google and type express

42
00:03:01,470 --> 00:03:02,310
async errors.

43
00:03:02,310 --> 00:03:05,850
And of course, one of the links is going to lead back to their docs.

44
00:03:06,240 --> 00:03:13,260
And effectively, if you want to use it in your own project, you'll go with NPM, install Xpress async

45
00:03:13,260 --> 00:03:20,550
errors that we keep on scrolling and notice how the only thing we need to do is the required and then

46
00:03:20,550 --> 00:03:21,970
we're good to go now.

47
00:03:21,990 --> 00:03:27,270
One thing you probably noticed that I did not use next, and essentially the reason why I didn't do

48
00:03:27,270 --> 00:03:33,990
that is because if you read their docs, they say that instead of using next, we simply need to throw

49
00:03:34,020 --> 00:03:34,530
error.

50
00:03:34,860 --> 00:03:41,430
And of course, since I can see the error in console, that means that I am getting that error in my

51
00:03:41,430 --> 00:03:42,530
custom handler.

52
00:03:42,750 --> 00:03:45,030
So the functionality still works.

53
00:03:45,240 --> 00:03:52,500
So long story short, instead of setting up try catch, instead of setting up our own middleware, we

54
00:03:52,500 --> 00:03:56,310
effectively just used a package that does all the work for us.

55
00:03:56,490 --> 00:04:04,920
And as a result we can eat and have our cake too, meaning we get the benefits of just using asynchronous

56
00:04:04,920 --> 00:04:12,270
code and we don't need to worry about setting up try catches or our own middleware on the package.

57
00:04:12,270 --> 00:04:21,019
Name that does that for us is a express errors and we simply need to require it in the ABC's.

