1
00:00:00,140 --> 00:00:00,590
All right.

2
00:00:00,590 --> 00:00:04,000
And up next, let's discuss async errors.

3
00:00:04,010 --> 00:00:11,600
How are they different from synchronous errors and what are going to be our approaches to handle them?

4
00:00:11,600 --> 00:00:18,440
So first, let's navigate to job controller and let's establish that job dot create.

5
00:00:19,100 --> 00:00:21,110
Effectively is asynchronous.

6
00:00:21,110 --> 00:00:23,720
So what happens if there's some kind of error?

7
00:00:23,900 --> 00:00:25,640
So at the moment, yeah, everything is nice.

8
00:00:25,640 --> 00:00:28,540
We pass in the req.body and we're good to go.

9
00:00:28,550 --> 00:00:35,240
But what happens, for example, if I pass here the string, the create is looking for object.

10
00:00:35,240 --> 00:00:36,590
I can tell you that right away.

11
00:00:36,590 --> 00:00:38,900
But what if I go with something?

12
00:00:38,990 --> 00:00:41,560
Let's see the result we get.

13
00:00:41,570 --> 00:00:47,570
So let me navigate to Thunder Client Create job doesn't really matter what you send over here.

14
00:00:48,080 --> 00:00:53,510
The main point is following notice we'll get this massive error in the console.

15
00:00:53,540 --> 00:00:54,260
Okay.

16
00:00:54,320 --> 00:00:57,500
But then our entire application crashed.

17
00:00:58,200 --> 00:01:08,010
So unlike the previous setup for synchronous errors, yes, we log still the error, but we nicely send

18
00:01:08,010 --> 00:01:13,480
back the response and also our server is still running.

19
00:01:13,530 --> 00:01:19,050
So yeah, we handle the error, but then we keep accepting the requests.

20
00:01:19,080 --> 00:01:20,670
It's not the case right now.

21
00:01:20,670 --> 00:01:24,300
So pretty much every time we'll have some kind of error happening.

22
00:01:24,420 --> 00:01:30,000
Well, our whole server is going to go down and of course we want to fix that.

23
00:01:30,000 --> 00:01:32,600
That's not the application we want.

24
00:01:32,610 --> 00:01:35,320
And essentially we have two approaches.

25
00:01:35,340 --> 00:01:39,120
We can set up, try and catch manually, which we're going to do in a second.

26
00:01:39,120 --> 00:01:44,550
Also, we can install package so that way we don't have to do it pretty much for every controller.

27
00:01:44,550 --> 00:01:47,330
So let's try the manual approach first.

28
00:01:47,340 --> 00:01:49,920
So I'm going to go here with try and catch.

29
00:01:50,830 --> 00:01:53,650
Then let me move these two lines of code up.

30
00:01:54,070 --> 00:01:57,600
And then as far as the catch one, let's just send something generic.

31
00:01:57,610 --> 00:02:00,400
I'm going to go with status 500.

32
00:02:02,150 --> 00:02:02,780
That.

33
00:02:02,780 --> 00:02:05,090
And we're going to go with Jason.

34
00:02:06,020 --> 00:02:08,539
And let's go with message and server.

35
00:02:09,630 --> 00:02:10,050
Error.

36
00:02:10,080 --> 00:02:11,310
Let's save it.

37
00:02:11,310 --> 00:02:13,440
And now let's try it out one more time.

38
00:02:13,500 --> 00:02:16,500
So let me send and check it out.

39
00:02:16,650 --> 00:02:19,770
We get back the 500 response.

40
00:02:20,390 --> 00:02:22,360
Message server error.

41
00:02:22,370 --> 00:02:28,310
And since we are working with Morgan, we can also see in the console that we made a post request and

42
00:02:28,310 --> 00:02:30,300
the response is 500.

43
00:02:30,320 --> 00:02:34,670
But the main point here is that our server is still running.

44
00:02:34,670 --> 00:02:41,420
So I can go to, for example, get all jobs and yes, of course I'll get those hardcoded jobs, but

45
00:02:41,420 --> 00:02:43,100
everything still works.

46
00:02:43,100 --> 00:02:44,840
This is still 200.

47
00:02:44,870 --> 00:02:48,860
Now if we want to log the error again, we'll just do it manually for now.

48
00:02:49,250 --> 00:02:54,170
We'll say over here, log an error and we're pretty much good to go again.

49
00:02:54,170 --> 00:02:55,780
I'll create the job.

50
00:02:55,790 --> 00:02:57,440
Yes, I'll get 500.

51
00:02:57,470 --> 00:03:00,770
Yes, I'll get the log in the console.

52
00:03:00,800 --> 00:03:04,880
But again, the main point is our server is running.

53
00:03:04,910 --> 00:03:05,390
Okay.

54
00:03:05,390 --> 00:03:06,440
Hopefully that is clear.

55
00:03:06,440 --> 00:03:13,040
So essentially since we're performing asynchronous operation, we want to wrap it in, try and catch.

56
00:03:13,160 --> 00:03:15,470
But your next question probably is following.

57
00:03:15,470 --> 00:03:15,800
Okay.

58
00:03:15,830 --> 00:03:21,060
Does that mean that pretty much we'll have to repeat this logic for every controller?

59
00:03:21,390 --> 00:03:24,030
And the answer is, well, yes and no.

60
00:03:24,210 --> 00:03:27,300
Technically, you can do it this way.

61
00:03:27,300 --> 00:03:34,260
So essentially you set up the try and catch for every controller, which is a little bit annoying,

62
00:03:34,260 --> 00:03:41,520
especially if you have, I don't know, 30 controllers or we can install a package that effectively

63
00:03:41,520 --> 00:03:43,200
will do that for us.

64
00:03:43,350 --> 00:03:46,020
And essentially this is what I'm going to do right now.

65
00:03:46,020 --> 00:03:53,610
So let's navigate to our read me and let me show you that there is a package by the name of express

66
00:03:53,640 --> 00:04:04,080
async errors, which essentially just provides that error to our error middleware in the server or here

67
00:04:04,080 --> 00:04:07,890
because like you just saw, it's not happening by default.

68
00:04:07,920 --> 00:04:09,930
Now, one last thing that I want to mention.

69
00:04:09,930 --> 00:04:15,570
Make sure that the import is at the very, very, very top on the server.

70
00:04:15,570 --> 00:04:19,260
Some students have complained that functionality doesn't work.

71
00:04:19,260 --> 00:04:20,790
If that's not the case.

72
00:04:20,790 --> 00:04:23,700
So in order to install this package, you want to go to NPM.

73
00:04:23,700 --> 00:04:28,890
I then express async errors and this is the version that I'm using.

74
00:04:28,890 --> 00:04:33,390
And then once you install the package, you want to go with import.

75
00:04:33,390 --> 00:04:35,430
And notice in this case we have the syntax.

76
00:04:35,430 --> 00:04:43,440
So import and then pretty much the package name again, please place it at the very top and you'll avoid

77
00:04:43,440 --> 00:04:45,060
some unnecessary bugs.

78
00:04:45,060 --> 00:04:50,850
So in here, let's go with import then we're going to go with express async errors.

79
00:04:50,850 --> 00:04:58,830
Just to stay on the safe side, I'll actually stop my server manually clear everything npm run dev and

80
00:04:58,830 --> 00:05:06,510
then let's go to create job in the job controller and let's remove those try catches and let's see what

81
00:05:06,510 --> 00:05:09,600
happens because again, we still have the error.

82
00:05:09,630 --> 00:05:13,080
This should be an object and I'm actually passing here the string.

83
00:05:13,080 --> 00:05:15,480
So let me create the job one more time.

84
00:05:15,480 --> 00:05:19,200
I send and notice my server is still working.

85
00:05:19,200 --> 00:05:25,920
I'm getting this 500 as far as the response and I also nicely can see my log over here.

86
00:05:25,920 --> 00:05:26,490
Why?

87
00:05:26,520 --> 00:05:30,780
Well, because in my error middleware we're logging it right now.

88
00:05:30,780 --> 00:05:35,040
So essentially the express async errors package.

89
00:05:35,810 --> 00:05:42,480
Handles any asynchronous errors and passes them to our middleware.

90
00:05:42,500 --> 00:05:47,090
Since it's not happening by default and since I don't want to type.

91
00:05:47,120 --> 00:05:51,050
Try and catch for every controller we're about to create.

92
00:05:51,170 --> 00:05:54,260
Well, that's why we'll use this awesome package.

93
00:05:54,260 --> 00:05:55,580
Just make sure.

94
00:05:56,610 --> 00:06:00,540
To place the import at the very, very top in the server.js file.

