1
00:00:00,380 --> 00:00:04,590
So what's the difference between the 404 middleware?

2
00:00:04,610 --> 00:00:10,550
Basically, the resource not found middleware and the error middleware.

3
00:00:10,760 --> 00:00:13,880
And essentially, I like this explanation the best.

4
00:00:13,910 --> 00:00:24,410
Not found errors are going to be triggered If there is a request for a resource that doesn't exist.

5
00:00:24,620 --> 00:00:28,340
Something we're going to cover again one more time in one second.

6
00:00:28,670 --> 00:00:36,200
But when it comes to the error middleware, it gets triggered by the existing root.

7
00:00:36,350 --> 00:00:40,730
And this is probably confusing because the error one is the last one.

8
00:00:40,880 --> 00:00:48,680
So quite often students ask, Hey, but if I have, for example, invalid request, how come it gets

9
00:00:48,680 --> 00:00:52,250
triggered over here but it never makes it to the error one.

10
00:00:52,970 --> 00:00:59,270
And again, the reason for that is because of this middleware will take care of the requests which don't

11
00:00:59,270 --> 00:01:01,180
match anything in our server.

12
00:01:01,190 --> 00:01:08,810
But this one, the error one is going to get triggered by our existing controllers.

13
00:01:08,990 --> 00:01:17,120
Effectively, it's going to be a valid request and the error is going to pop up when we try to implement

14
00:01:17,120 --> 00:01:18,210
the functionality.

15
00:01:18,230 --> 00:01:21,680
So let me show you a good example first.

16
00:01:21,710 --> 00:01:27,320
Again, let's navigate to our Thunder client and one more time, let's make this request.

17
00:01:27,320 --> 00:01:33,080
So notice if we go to jobs with an extra S, we'll have this 404.

18
00:01:33,080 --> 00:01:39,380
So if the resource does not exist, yes, we'll trigger that 404 middleware.

19
00:01:39,440 --> 00:01:44,150
Now let's go back to the regular jobs request.

20
00:01:44,150 --> 00:01:50,360
So this should yield the list of jobs and back in the server.

21
00:01:50,360 --> 00:01:58,250
Let's make a typo over here instead of the request, let's say I want to log, but instead of jobs again,

22
00:01:58,250 --> 00:02:01,270
I'm going to go with extra s, Let's save it.

23
00:02:01,280 --> 00:02:09,289
You'll notice that actually we don't trigger the error yet, but if I go back to get all jobs and again

24
00:02:09,289 --> 00:02:14,600
send a request, I'm going to get back this 500.

25
00:02:14,600 --> 00:02:15,680
So notice.

26
00:02:16,500 --> 00:02:22,160
Again, the error middleware gets triggered by our existing root.

27
00:02:22,170 --> 00:02:28,080
So in this case, the URL, the method, all of those things were correct.

28
00:02:28,230 --> 00:02:36,330
The reason why we have 500 is because in here we had the error and essentially we'll have two types

29
00:02:36,330 --> 00:02:36,990
of errors.

30
00:02:37,020 --> 00:02:39,570
We'll have errors that are there by mistake.

31
00:02:39,570 --> 00:02:46,770
So let's say again, I just mistyped something and therefore I trigger this error or we'll have errors

32
00:02:46,770 --> 00:02:49,260
that actually we set up.

33
00:02:49,260 --> 00:02:50,910
So what am I talking about?

34
00:02:51,060 --> 00:02:53,400
Notice over here this get single job.

35
00:02:53,670 --> 00:02:56,850
So we're sending back this response of 404.

36
00:02:57,060 --> 00:02:57,810
Correct.

37
00:02:57,810 --> 00:03:00,990
But alternatively, here's what we can do.

38
00:03:01,170 --> 00:03:03,550
I can throw a new error.

39
00:03:03,570 --> 00:03:07,860
So if the ID does not match, blah, blah, blah, blah, blah.

40
00:03:07,890 --> 00:03:16,990
Instead of sending the response of 404, I will trigger the error, which in turn will trigger the error

41
00:03:16,990 --> 00:03:20,050
middleware and then we'll send back the error response.

42
00:03:20,050 --> 00:03:23,950
And if this is confusing again, we will build this a little bit later on.

43
00:03:23,950 --> 00:03:30,070
But just to give you an idea, let me go back to the get single job and instead of.

44
00:03:30,460 --> 00:03:32,710
Sending the 404.

45
00:03:32,740 --> 00:03:34,450
Notice the moment I type.

46
00:03:34,480 --> 00:03:36,370
Throw new error.

47
00:03:36,790 --> 00:03:40,270
Pretty much everything gets grayed out over here.

48
00:03:40,270 --> 00:03:40,560
Why?

49
00:03:40,570 --> 00:03:42,670
Well, because this code is not going to run.

50
00:03:42,820 --> 00:03:44,440
And let's write the same message.

51
00:03:44,440 --> 00:03:47,530
No job with that ID.

52
00:03:47,890 --> 00:03:56,470
And essentially, the moment we have this line of Code Express will handle that error right over here

53
00:03:56,470 --> 00:03:58,420
in our error middleware.

54
00:03:58,540 --> 00:04:00,110
So let me showcase that.

55
00:04:00,130 --> 00:04:06,670
Remember, every time we restart the server, we set up that array from the scratch and therefore mean

56
00:04:06,670 --> 00:04:07,990
most likely this.

57
00:04:08,680 --> 00:04:09,900
It is going to be wrong.

58
00:04:09,910 --> 00:04:15,980
And notice I click on send and I'm not getting the 404.

59
00:04:16,000 --> 00:04:21,820
Actually, I'm getting the error response because again, this middleware is going to get triggered

60
00:04:21,820 --> 00:04:32,870
in existing routes where 404 gets triggered if the resource is not available on the server.

61
00:04:32,890 --> 00:04:34,120
Hopefully that is clear.

