1
00:00:00,540 --> 00:00:07,350
Once we have set up the async rapper next, I want to work on catching the errors and we're going to

2
00:00:07,350 --> 00:00:11,040
start our journey actually in the express documentation.

3
00:00:11,340 --> 00:00:17,370
So it goes to ExpressJet dot com online in search box, just type error.

4
00:00:17,760 --> 00:00:20,730
And first, let's take a look at the default one.

5
00:00:20,970 --> 00:00:26,370
And if you take a look at the first sentence, Xpress comes with built in error handler that takes care

6
00:00:26,370 --> 00:00:29,460
of any errors that might be encountered in the app.

7
00:00:29,730 --> 00:00:36,120
This default error handling middleware function is added at the end of the middleware function stack.

8
00:00:36,510 --> 00:00:45,390
And if we pass in error to the next and you do not handle it in a custom handler, it will be handled

9
00:00:45,390 --> 00:00:46,700
by the built in one.

10
00:00:47,010 --> 00:00:48,300
Why am I telling you this?

11
00:00:48,300 --> 00:00:50,880
Well, because that answers this question.

12
00:00:51,210 --> 00:00:53,410
Where is this response coming from?

13
00:00:53,700 --> 00:00:57,630
Well, that is coming from the default built in error handler.

14
00:00:57,930 --> 00:00:59,760
That's point number one I want to make.

15
00:01:00,030 --> 00:01:04,830
Otherwise you might be wondering, well, where are we getting this info second next.

16
00:01:04,830 --> 00:01:05,310
Correct.

17
00:01:05,519 --> 00:01:10,320
That's why in the async wrapper we set up this next an error.

18
00:01:10,950 --> 00:01:13,280
So we pass that to the next middleware.

19
00:01:13,290 --> 00:01:17,550
But in our case, of course, we haven't set up that handler yet.

20
00:01:17,850 --> 00:01:22,100
So therefore, it was just eventually passed down to the built in one.

21
00:01:22,440 --> 00:01:26,880
And lastly, if we go back and if we say here, so I'll search one more time.

22
00:01:27,120 --> 00:01:30,660
And in this case, we're looking for writing error handlers.

23
00:01:31,020 --> 00:01:32,460
So this is what we're about to do.

24
00:01:32,760 --> 00:01:35,190
We'll set up another set of middleware.

25
00:01:35,190 --> 00:01:39,690
However, in this case, the middleware will handle errors.

26
00:01:40,050 --> 00:01:45,780
And you can see that if we want to handle errors, we just pass in for arguments.

27
00:01:46,050 --> 00:01:53,540
And then the first one, we will be that error and we want to play that at the very end of our road.

28
00:01:53,880 --> 00:01:55,290
So let's just navigate back.

29
00:01:55,560 --> 00:02:01,590
And of course, in the example, they showcase how they right away set up the middleware in the app.

30
00:02:01,590 --> 00:02:09,090
Just in our case, since we'll have more code, essentially, I want to create another file in the middle

31
00:02:09,090 --> 00:02:10,360
or so in here.

32
00:02:10,410 --> 00:02:15,690
I'm just looking for new file and I'll call this error and then handler.

33
00:02:15,840 --> 00:02:20,520
And instead of the file, I just need to come up with the function and I'm going to go the longer out

34
00:02:20,550 --> 00:02:24,510
where else say Error and Lir middleware.

35
00:02:24,510 --> 00:02:25,970
And that is equal to error.

36
00:02:26,130 --> 00:02:30,900
Again, in this case, we need to have four parameters, request response.

37
00:02:31,200 --> 00:02:33,270
And we also have next.

38
00:02:33,450 --> 00:02:40,290
And as far as the logic will add more code once we set up our own custom, our class.

39
00:02:40,560 --> 00:02:46,110
But for the time being, we just want to go with a return and rise of that status.

40
00:02:46,110 --> 00:02:50,820
And if you remember, that's exactly what we had previously in the try catch block.

41
00:02:51,030 --> 00:02:57,790
And I'm just going to go five hundred and we'll say JSON error and error is equal to error.

42
00:02:57,960 --> 00:03:02,520
Now if you want, you can come to log out, of course, but the result is going to be exactly the same.

43
00:03:02,520 --> 00:03:09,750
Where in here we'll have access to that error, the one that is going to be coming in our case from

44
00:03:09,750 --> 00:03:11,160
this async wrapper.

45
00:03:11,400 --> 00:03:13,710
And then we just need to decide what we want to do.

46
00:03:13,890 --> 00:03:19,800
And I'll just start by saying error equal to her and you're not actually, I want to go with message

47
00:03:20,070 --> 00:03:21,270
is equal to error.

48
00:03:21,600 --> 00:03:26,550
And now we simply want to go back to abcess, where we have not found.

49
00:03:26,700 --> 00:03:30,150
We just copy and paste and now we just need to come up with a name.

50
00:03:30,390 --> 00:03:34,620
And as you know, naming is probably the hardest thing in programming.

51
00:03:34,980 --> 00:03:39,270
So I'm just going to call this error handler and then middleware.

52
00:03:39,600 --> 00:03:46,140
And of course, that one is coming from the file that we just set up, the error handler, and then

53
00:03:46,140 --> 00:03:47,970
we want to scroll all the way down.

54
00:03:48,270 --> 00:03:51,390
And right after not found, we're going to go with abduct use.

55
00:03:51,660 --> 00:03:59,040
And of course, we'll pass in the error handler Middlemore And once we save and we go back to the postman

56
00:03:59,370 --> 00:04:07,800
and try to send the same request, meaning the request with a incomplete data, then we should get a

57
00:04:07,800 --> 00:04:08,580
different response.

58
00:04:08,580 --> 00:04:10,140
But we don't effectively.

59
00:04:10,140 --> 00:04:11,520
It looks like I have a bug.

60
00:04:11,850 --> 00:04:13,590
So let me check the error handler.

61
00:04:13,830 --> 00:04:16,800
And yes, of course, I didn't export the function.

62
00:04:16,800 --> 00:04:19,470
So let me go with module sports.

63
00:04:19,649 --> 00:04:22,350
And that one is equal to a handler one.

64
00:04:22,650 --> 00:04:28,860
And of course, if I tested out right now, I should see the customer response and we can clearly see

65
00:04:28,860 --> 00:04:31,080
that because we have the message.

66
00:04:31,080 --> 00:04:37,230
Now, like I already mentioned previously, if you don't like these big, massive error messages, you

67
00:04:37,230 --> 00:04:38,910
can simply go back over here.

68
00:04:39,090 --> 00:04:45,510
And instead of sending the message equal to an error, you can just hard code some kind of message so

69
00:04:45,510 --> 00:04:48,810
you can go something along the lines of something.

70
00:04:49,860 --> 00:04:56,190
Something went wrong, karma, try again later.

71
00:04:57,390 --> 00:05:04,800
This is what we'll see in Afghanistan and of course, our message ish or so, essentially, this is

72
00:05:04,800 --> 00:05:12,240
how we can set up a custom Arab handler in Xpress where we add another piece of middleware and that

73
00:05:12,240 --> 00:05:14,700
in there we have four arguments on.

74
00:05:14,700 --> 00:05:16,710
The first one is going to be the era.

75
00:05:16,980 --> 00:05:23,340
And of course, in our case, the way we passed the errors down to this custom, our handler is by using

76
00:05:23,340 --> 00:05:26,790
the async wrapper and in there we have the next.

77
00:05:27,030 --> 00:05:31,290
So as you remember from the Turrill with next, we pass it to the next middleware.

78
00:05:31,590 --> 00:05:34,190
And previously we use the built in default one.

79
00:05:34,410 --> 00:05:39,120
And now, of course, we have our own where effectively you can set up whatever logic you want.

80
00:05:39,300 --> 00:05:43,290
If you want to send us as two hundred, nothing stops you from doing that.

81
00:05:43,590 --> 00:05:50,010
But of course, a more meaningful response would be with some kind of error code, with some kind of

82
00:05:50,010 --> 00:05:57,030
meaningful message, whether that is hard coded string or you can pass and the entire error object.

