1
00:00:00,300 --> 00:00:06,100
All right, and once we have the not found middleware in place, we want to do the same thing, we the

2
00:00:06,390 --> 00:00:06,820
middle way.

3
00:00:07,650 --> 00:00:12,720
And let me just briefly kind of go over the biggest differences between the two.

4
00:00:13,320 --> 00:00:19,860
And most likely, once we start setting up the controllers, we'll swing back because there's going

5
00:00:19,860 --> 00:00:21,480
to be easier for me to show you.

6
00:00:21,900 --> 00:00:30,630
But effectively the main difference is that not found one will be looking for requests that do not match

7
00:00:30,630 --> 00:00:32,700
any of our current route.

8
00:00:33,030 --> 00:00:33,460
Correct.

9
00:00:33,540 --> 00:00:35,010
Like I just said in the last video.

10
00:00:35,580 --> 00:00:42,150
But when it comes to the arrow handler, it's actually looking for errors that are happening in our

11
00:00:42,150 --> 00:00:42,960
existing world.

12
00:00:43,500 --> 00:00:47,490
And you'll see that in a second because we'll throw an error here in our home run.

13
00:00:47,730 --> 00:00:54,240
Therefore, we need to follow the rules of expression, and the rules for the error handler are following

14
00:00:54,690 --> 00:01:02,160
where essentially we'll do the same thing set up a function error handler in the middleware folder will

15
00:01:02,160 --> 00:01:03,270
create that as a function.

16
00:01:03,630 --> 00:01:06,870
But in this case, we're looking for four parameters.

17
00:01:07,410 --> 00:01:10,640
So right away, Express will know if you're looking for four parameters.

18
00:01:10,650 --> 00:01:15,800
That means that first one will be error, so it will pass in that error non-market.

19
00:01:16,020 --> 00:01:20,700
In the beginning, we'll just log it just so we can see whether everything works, and we're going to

20
00:01:20,700 --> 00:01:25,870
go with pretty generic response where we'll go with Materne 500.

21
00:01:25,890 --> 00:01:29,400
So status five hundred and nine in the JSON we'll just set up.

22
00:01:29,670 --> 00:01:30,630
There was an error.

23
00:01:30,960 --> 00:01:32,520
Now we still want to import that.

24
00:01:32,520 --> 00:01:38,490
We still want to use Jass extension, and I'm pretty much going to stop leaving this step over here.

25
00:01:38,730 --> 00:01:44,610
We'll have to repeat that to pretty much every time as we're working here in node line, I think at

26
00:01:44,610 --> 00:01:49,860
some point becomes self-explanatory and we want to place this one last one.

27
00:01:50,130 --> 00:01:55,470
And again, it's going to be easier for me to show you once we have some async errors already in our

28
00:01:55,470 --> 00:01:56,310
controllers.

29
00:01:56,310 --> 00:01:59,040
So that's why I'll put down one on ice for now.

30
00:01:59,370 --> 00:02:05,760
But please, please, please do place this one last one, even though the moment it might not look like

31
00:02:05,760 --> 00:02:06,270
a big deal.

32
00:02:06,750 --> 00:02:13,620
And if you watched my note express course, eventually our error handler pretty much is going to look

33
00:02:13,620 --> 00:02:15,690
like the one we were using throughout the course.

34
00:02:16,260 --> 00:02:18,570
The one that we were taking from project to project.

35
00:02:18,900 --> 00:02:21,150
But in this case, we'll set up everything from scratch.

36
00:02:21,150 --> 00:02:23,070
So yes, we'll start with a very basic one.

37
00:02:23,070 --> 00:02:28,140
And then each and every time we have that custom mongoose error, that's when we'll handle that.

38
00:02:28,150 --> 00:02:29,730
So this is really up to you.

39
00:02:29,730 --> 00:02:34,590
If you want to save yourself a little bit of time, you can copy and paste the code from there and you're

40
00:02:34,590 --> 00:02:35,000
good to go.

41
00:02:35,010 --> 00:02:38,880
If not, we can build it together because that's essentially what we're going to be doing.

42
00:02:39,150 --> 00:02:42,480
And yes, the last step over here showcase with a sync errors.

43
00:02:42,750 --> 00:02:48,150
So like I was saying, once we already have some logic in the controllers, it's just going to be easier

44
00:02:48,150 --> 00:02:50,730
for me to showcase the difference between the two.

45
00:02:51,210 --> 00:02:58,800
We're not found is going to be looking for all the requests that do not match any of our current route.

46
00:02:59,190 --> 00:03:05,880
And the error handler one will handle the errors that are happening in the existing route.

47
00:03:06,300 --> 00:03:10,680
And it might not sound like a big difference, but trust me, it is a big one and therefore we want

48
00:03:10,680 --> 00:03:14,970
to place the erosion all the way at the end.

49
00:03:15,240 --> 00:03:17,720
So let's actually go through the steps.

50
00:03:17,940 --> 00:03:19,590
We want to create the error handler one.

51
00:03:20,070 --> 00:03:23,970
Let's just go here with error and then hyphen and.

52
00:03:24,510 --> 00:03:25,890
Now that is going to be jealous.

53
00:03:26,340 --> 00:03:32,100
And as far as the function, I'm going to go again with the long name because my preference in the server

54
00:03:32,580 --> 00:03:34,320
is to have these names like this.

55
00:03:34,350 --> 00:03:37,230
Basically, I can do the auto import in that case as well.

56
00:03:37,750 --> 00:03:42,060
And the reason for that is because that clearly signals to me what is happening.

57
00:03:42,180 --> 00:03:44,430
Of course, this is an export default.

58
00:03:44,430 --> 00:03:46,950
So technically I can name it whatever I want.

59
00:03:46,950 --> 00:03:52,980
But in my case, I like this middleware part because that way I can clearly know what the function is

60
00:03:52,980 --> 00:03:53,310
doing.

61
00:03:53,520 --> 00:03:59,610
And then since I set that up in the file in the non-farm, I can right away use the auto import, as

62
00:03:59,610 --> 00:04:00,000
always.

63
00:04:00,090 --> 00:04:01,430
That is just my preference.

64
00:04:01,440 --> 00:04:02,610
You don't have to do that.

65
00:04:02,820 --> 00:04:04,290
So that is just a side note.

66
00:04:04,560 --> 00:04:11,010
And therefore, here I'm going to go with Konst and then error and lower and lower and lower.

67
00:04:12,040 --> 00:04:15,940
In Delaware, like I said, this function is going to be looking for for things.

68
00:04:16,329 --> 00:04:20,950
So the error first enteric virus and then the next.

69
00:04:21,160 --> 00:04:26,500
So now Express announced, OK, so this is going to be a error handler and then the first step is just

70
00:04:26,500 --> 00:04:30,900
comes along in the air and then we want to go with some kind of generic response.

71
00:04:30,910 --> 00:04:39,980
So whereas and let's set up a status that still will be 500 basically a general server error.

72
00:04:40,630 --> 00:04:47,020
We'll go here with 500 and then let's add a JSON one and then let's set up a message and we're going

73
00:04:47,020 --> 00:04:50,440
to go with what was the message over that wasn't to double check.

74
00:04:50,770 --> 00:04:51,910
So there was an error.

75
00:04:52,060 --> 00:04:56,490
So that's the one that I said might as well use that one is also there was an error.

76
00:04:57,250 --> 00:04:59,110
And then let's explore this one.

77
00:04:59,110 --> 00:05:00,550
So export.

78
00:05:01,610 --> 00:05:04,910
Default, and then we're looking for error handler.

79
00:05:05,300 --> 00:05:14,150
And now let's hop back to Sergius, look for the ninth problem and right after, not again, we're going

80
00:05:14,150 --> 00:05:21,290
to go with that use and then we'll pass in the error handler mature and we just need to make sure that

81
00:05:21,290 --> 00:05:22,340
we move this one down.

82
00:05:22,590 --> 00:05:27,980
And it looks like that in this case, it actually right away also added the jazz extension as well,

83
00:05:28,190 --> 00:05:29,420
which is really, really cool.

84
00:05:29,810 --> 00:05:34,520
So now we can test that out, and the way we'll do that is in the home run.

85
00:05:34,550 --> 00:05:35,810
Effectively, we'll throw the error.

86
00:05:35,810 --> 00:05:38,180
So say throw new error.

87
00:05:38,660 --> 00:05:44,390
And I mean, the message here is really unimportant because we already have the response in place.

88
00:05:44,730 --> 00:05:45,530
There was an error.

89
00:05:45,800 --> 00:05:50,090
So I just say here error so we can save ourselves a little bit of typing.

90
00:05:50,450 --> 00:05:51,350
So let's go back.

91
00:05:51,620 --> 00:05:53,900
Now I'm not going to be looking for for it's not shallow.

92
00:05:54,230 --> 00:05:55,460
The Iran does not exist.

93
00:05:55,850 --> 00:06:00,230
Actually, I'm going to be looking for a home Roth, and we should see there was an hour.

94
00:06:00,590 --> 00:06:04,850
And if that's what you see on a screen, beautiful, we're moving in the right direction.

95
00:06:05,120 --> 00:06:11,750
And then in the terminal, you should see the error and notice that is coming from the server address.

96
00:06:12,200 --> 00:06:19,190
And with these two middleware in place now, it can start setting up the connection to MongoDB.

