1
00:00:00,180 --> 00:00:00,540
All right.

2
00:00:00,840 --> 00:00:08,340
So our initial functionality works somewhat OK if we provide all the values we create the user and then

3
00:00:08,340 --> 00:00:09,210
we send it back.

4
00:00:09,690 --> 00:00:14,030
If there's an error, we send back five hundred.

5
00:00:14,530 --> 00:00:19,140
Now, first of all, let's understand where is this error coming from?

6
00:00:19,500 --> 00:00:21,560
And I just coming from the Mongoose.

7
00:00:21,600 --> 00:00:21,990
Why?

8
00:00:22,270 --> 00:00:29,400
Well, because when we were setting up the user, we were setting up the name, email and password required.

9
00:00:29,700 --> 00:00:32,040
And also email is unique.

10
00:00:32,280 --> 00:00:36,120
And then we also have this specific one for the valid email.

11
00:00:36,480 --> 00:00:39,210
So those are our validators.

12
00:00:39,780 --> 00:00:43,860
So if those values will be missing, we'll be getting back the error.

13
00:00:44,370 --> 00:00:44,730
Correct.

14
00:00:45,540 --> 00:00:48,720
So hopefully we're clear on that and not at the moment.

15
00:00:48,720 --> 00:00:50,340
We're pretty much just hard coding.

16
00:00:50,410 --> 00:00:54,780
This is going to be five hundred and there was an error.

17
00:00:55,320 --> 00:01:02,370
But remember when we talked about the error handler when we were singing up quite a few videos ago,

18
00:01:02,640 --> 00:01:10,110
I mentioned that we need to place at last one because in Express will be able to pass our error from

19
00:01:10,110 --> 00:01:12,810
the controller to our error handler.

20
00:01:13,170 --> 00:01:19,170
So remember all the way in the bottom, we set up error handling middleware right after not found.

21
00:01:19,560 --> 00:01:25,830
And for the time being, we are just gone, longing the error, and we're sending back this generic

22
00:01:25,830 --> 00:01:26,340
response.

23
00:01:26,640 --> 00:01:35,430
And the way we can pass the error from our controller, basically from our out to our handler is fallen.

24
00:01:35,790 --> 00:01:41,840
If you take a look at the express ducks, essentially if you have the try and catch, you can go with

25
00:01:41,960 --> 00:01:42,390
next.

26
00:01:42,570 --> 00:01:47,160
You just need to add it to your function parameter and then you pass in the error.

27
00:01:47,790 --> 00:01:52,050
So in the process, what we can do, we can eliminate of this code.

28
00:01:52,350 --> 00:01:54,570
Now it's not much, but it's a good start.

29
00:01:55,320 --> 00:02:01,830
So we don't have to each and every time typing this, we can just say, Hey, go with next and then

30
00:02:01,830 --> 00:02:05,100
pass it to the error handler and then we'll figure out what's happening.

31
00:02:05,370 --> 00:02:06,420
So let's try doing that.

32
00:02:06,690 --> 00:02:11,640
First of all, we need to set up the next parameter, which passes it on to our next middleware, which

33
00:02:11,640 --> 00:02:14,280
in our case will be our error handler.

34
00:02:14,850 --> 00:02:18,360
And then we want to do is pass in the error.

35
00:02:18,480 --> 00:02:21,180
So we're going to go next and then the error.

36
00:02:21,600 --> 00:02:28,020
And then since we're logging the error in the error handler middleware, we should see the error.

37
00:02:28,090 --> 00:02:30,870
And instead of sending back, there was an error.

38
00:02:31,200 --> 00:02:34,080
I can also just take this error and send it back.

39
00:02:34,410 --> 00:02:39,660
So that way, we can clearly see now this is really up to you, but I will right away go with her.

40
00:02:39,690 --> 00:02:43,890
If you want to just console.log it first and then pass it on, it doesn't really matter.

41
00:02:44,730 --> 00:02:46,740
I'm just going to go with message is equal to an error.

42
00:02:47,160 --> 00:02:53,280
And now it's going to happen if I'm trying to create the user without again providing one of the values.

43
00:02:53,700 --> 00:02:54,360
Check it out.

44
00:02:54,690 --> 00:03:00,390
Now we're getting back this massive error from the Mongols now in fear of it.

45
00:03:00,390 --> 00:03:06,540
And basically, we'll set it up that we're sending a more meaningful message because at the moment,

46
00:03:06,640 --> 00:03:07,380
this is giant.

47
00:03:08,040 --> 00:03:13,050
The main idea of this video is for you to understand that if you have errors in the controllers, the

48
00:03:13,050 --> 00:03:15,210
way we handle that, we go in next.

49
00:03:15,510 --> 00:03:20,640
Now, in the next video, it will implement a package that takes care of our try and catch us.

50
00:03:20,970 --> 00:03:22,860
So the setup is going to be a little bit different.

51
00:03:23,220 --> 00:03:30,660
But if you are using this, try and catch approach instead of hard coding those errors for pretty much

52
00:03:30,990 --> 00:03:37,710
each and every controller, what you can do is add this next and then set up the error handler.

53
00:03:37,710 --> 00:03:39,810
Of course, that's a must.

54
00:03:40,080 --> 00:03:42,450
And then go next and then passing the error.

55
00:03:42,750 --> 00:03:49,350
And then in the error handler, eventually we'll set up the logic where I'll be like, OK, is this

56
00:03:49,350 --> 00:03:50,060
our error?

57
00:03:50,070 --> 00:03:51,210
Is this Mongo, sir?

58
00:03:51,390 --> 00:03:55,530
If it's a mongoose error, then let's send back something more meaningful.

59
00:03:55,800 --> 00:03:59,700
Instead of this giant object and all kinds of cool things.

60
00:03:59,880 --> 00:04:05,430
So for now, the biggest takeaway from this video is for you to understand that next, we'll pass it

61
00:04:05,430 --> 00:04:06,510
on to the next middleware.

62
00:04:06,720 --> 00:04:15,990
And in our case, what next passes it on to our error handler, which is located all the way at the

63
00:04:15,990 --> 00:04:17,700
end of our route.

