WEBVTT

00:03.790 --> 00:05.320
Hey, did everyone a day share?

00:05.320 --> 00:07.600
And welcome to another video of this section.

00:07.600 --> 00:13.210
Yes, I know we are going quite a lot in the theoretical section and this video is also kind of a mix

00:13.210 --> 00:15.040
of theory and a bit of a code itself.

00:15.040 --> 00:17.440
So let's go ahead and talk mode on to this part.

00:17.620 --> 00:23.320
So the next one we're going to talk about is errors now errors and how you handle them gracefully or

00:23.320 --> 00:27.130
just by default is totally up to you and how your application behaves.

00:27.130 --> 00:31.690
Now, by default, the node error handling is like really, really amazing and you don't need to do

00:31.690 --> 00:32.950
too much of the stuff.

00:32.950 --> 00:39.400
But still I like I see a lot of time that people like to handle their errors by their own and they also

00:39.400 --> 00:43.660
like to keep it different for production grade errors as well as in the development errors in the production,

00:43.660 --> 00:46.270
they send the minimum of the messages in the development.

00:46.270 --> 00:48.610
They like to keep more of the information.

00:48.610 --> 00:53.440
But I personally think that handling the error via the node itself is really fantastic.

00:53.440 --> 00:54.820
There is no such need of it yet.

00:54.820 --> 00:58.720
I'll show you that because there's just a couple of lines of code, so no big deal there.

00:58.720 --> 01:02.470
So again, it can be different for production environment as well as for development.

01:02.470 --> 01:08.950
But how you can read more about these errors, assuming that probably you haven't taken the node specialized

01:08.950 --> 01:13.150
course or not, that's up to you, but this is how we are going to study them.

01:13.150 --> 01:16.570
So go ahead and open this simple link that I've given you.

01:16.570 --> 01:18.520
This is actually a Node.js documentation.

01:18.520 --> 01:23.290
Whatever the version you are using, any advance or something not going to bother too much because these

01:23.290 --> 01:24.520
are just basic class.

01:24.520 --> 01:28.870
So you can see we have a whole lot of error class that come up like syntax errors and all of that.

01:28.870 --> 01:32.590
But I want you to bring your attention onto the very basic, which is a class error.

01:32.590 --> 01:37.720
This is the basic one which you are going to use in case you are writing a custom error class for yourself.

01:37.720 --> 01:42.100
So what you're going to notice that you can just go ahead and say, Hey, new error and you can just

01:42.100 --> 01:44.380
pass on a message to this one, and that's it.

01:44.380 --> 01:47.710
It creates automatically a kind of a message for you.

01:47.710 --> 01:54.310
It also has a property of error, code error, dot stack trace limit at a dot capture stack, trace,

01:54.580 --> 01:55.870
message and stack.

01:55.870 --> 01:58.450
And you can even customize your error code and all of that.

01:58.450 --> 01:59.620
So you can go ahead and do that.

01:59.620 --> 02:03.010
So click on here and just go ahead and try to read up here.

02:03.010 --> 02:05.680
So notice here if I want to capture this track trace.

02:05.680 --> 02:11.230
Also, it has created a simple message that, hey, this is my error and in case I want to handle this,

02:11.230 --> 02:14.500
let's just say the default is just sending me the error code itself.

02:14.890 --> 02:18.970
So what I can do is I can just write a function which takes the existing error.

02:18.970 --> 02:19.930
This is the existing error.

02:19.930 --> 02:25.330
And you can just add a method which says capture stack trace and now it will give you on what file number

02:25.330 --> 02:26.500
the errors are coming in.

02:26.500 --> 02:31.900
So this is the basic example and really I don't want to put this course into a node specific course.

02:31.900 --> 02:37.180
I want to be at a production grade back end developer, not just specific on the on the node here.

02:37.180 --> 02:41.380
But again, it is my recommendation that in case you don't understand the errors pretty well, this

02:41.380 --> 02:42.880
entire page is worth reading.

02:42.880 --> 02:45.190
And again, notice here it says new and error.

02:45.190 --> 02:47.320
And this is how you can create.

02:47.620 --> 02:52.480
You can also create a kind of a superclass on top of this one that, hey, this is my custom error handler

02:52.480 --> 02:53.920
and I just want to go like that.

02:53.920 --> 02:59.230
I want to get more information, I want to get the capture stack trace and I want to go for a trace

02:59.230 --> 02:59.560
limit.

02:59.560 --> 03:04.060
And I also want to manage my error code on my own and all of that so you can go ahead and just use that

03:04.060 --> 03:04.390
one.

03:04.390 --> 03:08.980
So creating a class on top of this is like really the basic way of how people do it, and I'll show

03:08.980 --> 03:10.030
you that as well.

03:10.120 --> 03:10.640
Okay.

03:10.990 --> 03:12.970
Now again, is it necessary?

03:12.970 --> 03:13.930
No, I don't think so.

03:13.930 --> 03:15.250
I really don't think so.

03:15.250 --> 03:20.170
I have written production, grade application, and they don't use any kind of these custom classes.

03:20.170 --> 03:24.400
The default Node.js error was able to fully handle everything on its own.

03:24.400 --> 03:25.390
So I don't need to do that.

03:25.390 --> 03:29.080
But still I'll show you that in case you are interested in that thing.

03:29.230 --> 03:34.900
Now, I would also like to give you an abstract code example of how things are there, and I would like

03:34.900 --> 03:36.580
to open up something interesting for you.

03:36.580 --> 03:40.120
So let me go ahead and open up the Mongoose documentation here as well.

03:40.120 --> 03:46.990
So let me go on to the Google and we're going to go ahead and say, Hey, Mongoose, please go ahead

03:46.990 --> 03:47.740
and open up.

03:48.520 --> 03:49.450
And there we go.

03:49.450 --> 03:51.520
So this is the thing of my browser.

03:51.520 --> 03:52.420
I type mongoose.

03:52.420 --> 03:56.380
I don't get the animal, but rather I get the mongoose ODM first.

03:56.530 --> 04:00.430
Okay, let's go ahead and read the docs and I wanted to show you something really, really interesting

04:00.430 --> 04:01.030
up here.

04:01.030 --> 04:06.640
So, okay, so I had to search a little bit to show you this, but this is really one of the most important

04:06.640 --> 04:08.890
thing that how we are going to handle errors.

04:08.890 --> 04:11.560
It actually makes sense when you come on to the mongoose itself.

04:11.560 --> 04:16.270
So as you know, that mongoose is a layer added on top of MongoDB to make server life easier.

04:16.270 --> 04:20.830
But if you just search for promises up here, so go ahead and just look for the promises that exactly

04:20.830 --> 04:21.490
what I did.

04:21.490 --> 04:24.490
I go ahead and hit search for that and it gives you all the links.

04:24.490 --> 04:28.330
The second one is most important that how Mongo is actually handle these promises.

04:28.330 --> 04:32.020
This will bring us our conversation that why we are all doing all of that.

04:32.020 --> 04:34.810
So this is the promise now it is built in promises.

04:34.810 --> 04:39.040
Yes, you can inject your promises like Bluebird and all of that, but we won't go that much off in

04:39.040 --> 04:39.460
depth.

04:39.460 --> 04:44.950
So Mongoose is a sting operation like save and queries returns the enable the word the enable simply

04:44.950 --> 04:48.700
means it returns you a promise and you can use a dot then and dot catch there.

04:48.820 --> 04:55.600
This means that you can do things like my model dot find one dot then and away dot my model dot five

04:55.630 --> 04:58.810
find one dot executed here.

04:58.810 --> 05:01.630
So what is the difference between all going up here now the.

05:01.740 --> 05:06.510
Thing is that if you're going to just run this one, this actually gives you a door then and catch back.

05:06.510 --> 05:10.260
But if you're going for the weight, it actually executes that function.

05:10.260 --> 05:15.270
And in the recent version of the Mongoose, you don't even need to write the XY because it just automatically

05:15.270 --> 05:16.130
does it for you.

05:16.140 --> 05:20.640
But the thing is, a think of it, so I know this is a little bit confusing, so let me rather give

05:20.640 --> 05:22.890
you a better example of how things actually work.

05:22.890 --> 05:27.360
So let me find up my VS code and give you a better example of how things are there.

05:27.360 --> 05:29.550
And again, I won't be giving you these files.

05:29.550 --> 05:33.000
They are not part of exercise file, they are just abstract code.

05:33.000 --> 05:37.890
I'm trying to write to just show you some stuff here so that it is easier for you to understand.

05:38.040 --> 05:42.690
Okay, so let's just say we go ahead and create a simple my user.

05:42.690 --> 05:48.060
This is going to be a simple object and it has a property of name and I go ahead and present the name.

05:48.180 --> 05:51.330
Now I want to save this name into the database.

05:51.750 --> 05:55.140
That's really as simplest of the job, what we want to do here.

05:55.320 --> 05:58.410
So how we're going to do that, let's just go ahead and create a method.

05:58.410 --> 06:00.870
So we go ahead and call this one as save user.

06:00.870 --> 06:02.130
That's how we do that.

06:02.130 --> 06:06.900
We go ahead and say, hey, request and response some kind of and again, this is abstraction code.

06:06.900 --> 06:10.140
So I'm not working on whether it is absolutely working or not.

06:10.140 --> 06:13.920
This is kind of algorithmic design or loosely algorithm.

06:13.920 --> 06:15.210
So don't pinpoint me.

06:15.210 --> 06:19.920
Please don't be like that comment section of YouTube and you are already taking a professional course,

06:19.920 --> 06:21.270
so you are very nice.

06:21.600 --> 06:23.340
Okay, so how do we do this one?

06:23.340 --> 06:27.570
Let's just say this is my model that I called up and on this model I go ahead and run the method, which

06:27.570 --> 06:30.810
is a create method which creates a new entry into the database.

06:30.930 --> 06:34.230
I go ahead and come up here and I pass on this, my user here.

06:34.380 --> 06:39.570
Now the whole idea behind that mongoose is that this entirety of the thing is actually tenable.

06:39.570 --> 06:44.430
That means you can go ahead and say dot then and you can go ahead and say Dot catch.

06:44.430 --> 06:46.410
So this is how you go ahead and do this.

06:46.560 --> 06:50.370
Now what you want to further do after this one, this all depends on you.

06:50.370 --> 06:55.470
So if the user was successfully created, it will actually return back a user to you and you can go

06:55.470 --> 06:59.070
ahead and call back like this and you can go ahead and do more stuff.

06:59.070 --> 07:03.660
So more stuff, whatever you want to do after that, maybe send a message to the user on the front end,

07:03.660 --> 07:04.650
whatever you want to do.

07:04.650 --> 07:08.040
This is one of the strategy that you might want to do.

07:08.190 --> 07:08.550
Okay.

07:08.550 --> 07:11.190
What is the another strategy that we can actually go ahead and do?

07:11.190 --> 07:13.080
Let's go ahead and try that one more time.

07:13.080 --> 07:16.680
So we're going to go ahead and say, hey, let's go ahead and define the same method.

07:16.710 --> 07:17.610
Save user.

07:17.670 --> 07:20.910
It is going to be simply request and response.

07:21.030 --> 07:24.870
Okay, further down the road, I go ahead and work on with that.

07:24.870 --> 07:27.810
So I go ahead and I want to obviously grab this user.

07:27.810 --> 07:31.170
So whatever he's saving into the user, I want to store that into available.

07:31.170 --> 07:31.890
So pretty simple.

07:31.890 --> 07:37.890
I go ahead and say this is the variable and I go ahead and say, hey user, please go ahead and create

07:37.890 --> 07:38.400
a user.

07:38.400 --> 07:44.700
For me using this, my user pretty obvious, but we saw in one of the earlier section that this operation

07:44.700 --> 07:47.010
doesn't happen like at the instant moment.

07:47.010 --> 07:51.900
So that is why in the documentation they mention this that hey, go ahead and use it await there as

07:51.900 --> 07:55.920
soon as you await there this entire method needs to be become a sync there.

07:56.820 --> 08:03.510
So this is how the basic stuff but this is not all this is not all the reason why this is not all because

08:03.510 --> 08:06.930
here you are actually handling the case where things go wrong.

08:06.930 --> 08:09.300
Here you are not handling the things which goes wrong.

08:09.300 --> 08:12.150
You are just handling the time sensitivity part of it.

08:12.150 --> 08:17.460
So how this works in this case, you are going to see that the authorized way are kind of a recommended

08:17.460 --> 08:17.730
way.

08:17.760 --> 08:21.960
Not authorized is actually to wrap all of this into a trigger block.

08:21.960 --> 08:25.980
So you need to go ahead and do some kind of try catch and just like that.

08:25.980 --> 08:31.470
So this is how things goes in and into the tribe log you go ahead and pass on this information and anything

08:31.470 --> 08:31.950
goes wrong.

08:31.950 --> 08:34.080
You actually rub that up in the try catch.

08:34.320 --> 08:39.810
But the thing is, on top of this, some time you still need to handle the promises.

08:39.810 --> 08:44.970
So even although you have written this as saying, you have written the try and catch, and if further

08:44.970 --> 08:50.640
down the road you actually go ahead and say, Hey, I still need you to handle the promises and you're

08:50.640 --> 08:53.520
going to see some console.log warning that, hey, go ahead and do that.

08:53.700 --> 08:54.930
So which one is good?

08:55.680 --> 08:57.510
Again, there is no right or wrong here.

08:57.510 --> 09:00.090
You can go ahead and go for this entirety of the flow.

09:00.090 --> 09:05.790
But the problem is sometimes you drill down so much inside of the code that some variables are not easily

09:05.790 --> 09:06.390
accessible.

09:06.390 --> 09:09.930
You need to pass on a reference of them and a whole bunch of this actually comes in.

09:09.930 --> 09:12.270
And then we have these arrow functions so we can.

09:12.420 --> 09:14.070
So there's a lot of things up here.

09:14.070 --> 09:18.540
So what you're going to see in most of the production grade code is happen something like this.

09:18.540 --> 09:21.540
So people like to go ahead and create a simple helper function.

09:21.540 --> 09:23.850
And what this helper function does is nothing.

09:23.850 --> 09:29.940
It just wraps around your entirety of the code or function that you're passing up and just puts a kind

09:29.940 --> 09:31.080
of a promise around it.

09:31.080 --> 09:32.640
So that's basically it.

09:32.640 --> 09:38.820
So let's just call this one as simply I usually call this one in all of my code as a big promise because

09:38.820 --> 09:40.110
it's kind of a big brother.

09:40.110 --> 09:45.120
So what you do is you obviously are going to receive a function on this because this entire request

09:45.120 --> 09:48.330
and response, whatever is going on, you just pass on it to as a test.

09:48.330 --> 09:50.850
So this is my function that I receive up here.

09:50.850 --> 09:56.160
Now, as soon as I received this one, I take that as a kind of an argument and I pass it on further

09:56.160 --> 09:57.270
down the road to this one.

09:57.270 --> 09:59.070
So request response and next.

09:59.070 --> 10:01.560
Next is very important in writing this help.

10:01.670 --> 10:03.650
To function further down the road.

10:03.650 --> 10:09.170
I go ahead and say, Hey, I will create my promise myself and promise.

10:09.170 --> 10:12.260
So whatever the default promise you are going to use, it's going to come in.

10:12.260 --> 10:14.140
And this promise has two property.

10:14.150 --> 10:17.870
The first one is resolve and this is how the resolve works.

10:17.870 --> 10:19.970
And the second one is dot catch.

10:20.630 --> 10:22.700
So in the catch I go ahead and say next.

10:22.700 --> 10:26.660
So that further whoever is actually running this can actually handle this one.

10:26.780 --> 10:32.450
But in the case of resolve, obviously the function that you're passing me up, I need to execute that

10:32.450 --> 10:33.440
inside this one.

10:33.440 --> 10:35.330
So I go ahead and say, hey, function.

10:35.330 --> 10:39.350
And obviously in this function you'll give me the parameters that are coming in, request, response

10:39.350 --> 10:40.640
and next.

10:40.640 --> 10:41.480
So that's it.

10:41.600 --> 10:42.830
That is it.

10:43.340 --> 10:49.190
I know this is a little bit kind of a mode for you to understand, but in case you have taken my classes

10:49.190 --> 10:53.610
on the YouTube of JavaScript of higher order functions and all of that, this is how it works.

10:53.630 --> 10:57.890
Now, once you have defined this higher order function, what is going to happen now?

10:57.890 --> 11:00.560
After that, things actually becomes a lot more easier.

11:00.560 --> 11:06.980
And you can just wrap this up around entirety of the thing into a simple big promise up here.

11:07.910 --> 11:13.850
I know this is a lot for you to digest, so go ahead, watch this video, read a little bit of the documentation.

11:13.850 --> 11:19.090
And this is kind of important thing for you to understand, either go with entirely of the dot dot catch

11:19.100 --> 11:23.780
syntax if you want to use something like holding the reference data weight and all of that, then Big

11:23.780 --> 11:28.100
Promise is definitely going to come in and we will be using that into the next section.

11:28.100 --> 11:29.370
So don't you worry too much.

11:29.390 --> 11:31.160
I'll explain that one more time.

11:31.160 --> 11:33.140
So that is easier for you to understand.

11:33.350 --> 11:38.330
I know this was this video was a little bit heavy, so go ahead, rewatch it or probably just wait for

11:38.330 --> 11:39.550
the next section to come up.

11:39.560 --> 11:41.480
Let's go ahead and catch up in the next video.
