Hey, did everyone a day share?
And welcome to another video of this section.
Yes, I know we are going quite a lot in the theoretical section and this video is also kind of a mix
of theory and a bit of a code itself.
So let's go ahead and talk mode on to this part.
So the next one we're going to talk about is errors now errors and how you handle them gracefully or
just by default is totally up to you and how your application behaves.
Now, by default, the node error handling is like really, really amazing and you don't need to do
too much of the stuff.
But still I like I see a lot of time that people like to handle their errors by their own and they also
like to keep it different for production grade errors as well as in the development errors in the production,
they send the minimum of the messages in the development.
They like to keep more of the information.
But I personally think that handling the error via the node itself is really fantastic.
There is no such need of it yet.
I'll show you that because there's just a couple of lines of code, so no big deal there.
So again, it can be different for production environment as well as for development.
But how you can read more about these errors, assuming that probably you haven't taken the node specialized
course or not, that's up to you, but this is how we are going to study them.
So go ahead and open this simple link that I've given you.
This is actually a Node.js documentation.
Whatever the version you are using, any advance or something not going to bother too much because these
are just basic class.
So you can see we have a whole lot of error class that come up like syntax errors and all of that.
But I want you to bring your attention onto the very basic, which is a class error.
This is the basic one which you are going to use in case you are writing a custom error class for yourself.
So what you're going to notice that you can just go ahead and say, Hey, new error and you can just
pass on a message to this one, and that's it.
It creates automatically a kind of a message for you.
It also has a property of error, code error, dot stack trace limit at a dot capture stack, trace,
message and stack.
And you can even customize your error code and all of that.
So you can go ahead and do that.
So click on here and just go ahead and try to read up here.
So notice here if I want to capture this track trace.
Also, it has created a simple message that, hey, this is my error and in case I want to handle this,
let's just say the default is just sending me the error code itself.
So what I can do is I can just write a function which takes the existing error.
This is the existing error.
And you can just add a method which says capture stack trace and now it will give you on what file number
the errors are coming in.
So this is the basic example and really I don't want to put this course into a node specific course.
I want to be at a production grade back end developer, not just specific on the on the node here.
But again, it is my recommendation that in case you don't understand the errors pretty well, this
entire page is worth reading.
And again, notice here it says new and error.
And this is how you can create.
You can also create a kind of a superclass on top of this one that, hey, this is my custom error handler
and I just want to go like that.
I want to get more information, I want to get the capture stack trace and I want to go for a trace
limit.
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
one.
So creating a class on top of this is like really the basic way of how people do it, and I'll show
you that as well.
Okay.
Now again, is it necessary?
No, I don't think so.
I really don't think so.
I have written production, grade application, and they don't use any kind of these custom classes.
The default Node.js error was able to fully handle everything on its own.
So I don't need to do that.
But still I'll show you that in case you are interested in that thing.
Now, I would also like to give you an abstract code example of how things are there, and I would like
to open up something interesting for you.
So let me go ahead and open up the Mongoose documentation here as well.
So let me go on to the Google and we're going to go ahead and say, Hey, Mongoose, please go ahead
and open up.
And there we go.
So this is the thing of my browser.
I type mongoose.
I don't get the animal, but rather I get the mongoose ODM first.
Okay, let's go ahead and read the docs and I wanted to show you something really, really interesting
up here.
So, okay, so I had to search a little bit to show you this, but this is really one of the most important
thing that how we are going to handle errors.
It actually makes sense when you come on to the mongoose itself.
So as you know, that mongoose is a layer added on top of MongoDB to make server life easier.
But if you just search for promises up here, so go ahead and just look for the promises that exactly
what I did.
I go ahead and hit search for that and it gives you all the links.
The second one is most important that how Mongo is actually handle these promises.
This will bring us our conversation that why we are all doing all of that.
So this is the promise now it is built in promises.
Yes, you can inject your promises like Bluebird and all of that, but we won't go that much off in
depth.
So Mongoose is a sting operation like save and queries returns the enable the word the enable simply
means it returns you a promise and you can use a dot then and dot catch there.
This means that you can do things like my model dot find one dot then and away dot my model dot five
find one dot executed here.
So what is the difference between all going up here now the.
Thing is that if you're going to just run this one, this actually gives you a door then and catch back.
But if you're going for the weight, it actually executes that function.
And in the recent version of the Mongoose, you don't even need to write the XY because it just automatically
does it for you.
But the thing is, a think of it, so I know this is a little bit confusing, so let me rather give
you a better example of how things actually work.
So let me find up my VS code and give you a better example of how things are there.
And again, I won't be giving you these files.
They are not part of exercise file, they are just abstract code.
I'm trying to write to just show you some stuff here so that it is easier for you to understand.
Okay, so let's just say we go ahead and create a simple my user.
This is going to be a simple object and it has a property of name and I go ahead and present the name.
Now I want to save this name into the database.
That's really as simplest of the job, what we want to do here.
So how we're going to do that, let's just go ahead and create a method.
So we go ahead and call this one as save user.
That's how we do that.
We go ahead and say, hey, request and response some kind of and again, this is abstraction code.
So I'm not working on whether it is absolutely working or not.
This is kind of algorithmic design or loosely algorithm.
So don't pinpoint me.
Please don't be like that comment section of YouTube and you are already taking a professional course,
so you are very nice.
Okay, so how do we do this one?
Let's just say this is my model that I called up and on this model I go ahead and run the method, which
is a create method which creates a new entry into the database.
I go ahead and come up here and I pass on this, my user here.
Now the whole idea behind that mongoose is that this entirety of the thing is actually tenable.
That means you can go ahead and say dot then and you can go ahead and say Dot catch.
So this is how you go ahead and do this.
Now what you want to further do after this one, this all depends on you.
So if the user was successfully created, it will actually return back a user to you and you can go
ahead and call back like this and you can go ahead and do more stuff.
So more stuff, whatever you want to do after that, maybe send a message to the user on the front end,
whatever you want to do.
This is one of the strategy that you might want to do.
Okay.
What is the another strategy that we can actually go ahead and do?
Let's go ahead and try that one more time.
So we're going to go ahead and say, hey, let's go ahead and define the same method.
Save user.
It is going to be simply request and response.
Okay, further down the road, I go ahead and work on with that.
So I go ahead and I want to obviously grab this user.
So whatever he's saving into the user, I want to store that into available.
So pretty simple.
I go ahead and say this is the variable and I go ahead and say, hey user, please go ahead and create
a user.
For me using this, my user pretty obvious, but we saw in one of the earlier section that this operation
doesn't happen like at the instant moment.
So that is why in the documentation they mention this that hey, go ahead and use it await there as
soon as you await there this entire method needs to be become a sync there.
So this is how the basic stuff but this is not all this is not all the reason why this is not all because
here you are actually handling the case where things go wrong.
Here you are not handling the things which goes wrong.
You are just handling the time sensitivity part of it.
So how this works in this case, you are going to see that the authorized way are kind of a recommended
way.
Not authorized is actually to wrap all of this into a trigger block.
So you need to go ahead and do some kind of try catch and just like that.
So this is how things goes in and into the tribe log you go ahead and pass on this information and anything
goes wrong.
You actually rub that up in the try catch.
But the thing is, on top of this, some time you still need to handle the promises.
So even although you have written this as saying, you have written the try and catch, and if further
down the road you actually go ahead and say, Hey, I still need you to handle the promises and you're
going to see some console.log warning that, hey, go ahead and do that.
So which one is good?
Again, there is no right or wrong here.
You can go ahead and go for this entirety of the flow.
But the problem is sometimes you drill down so much inside of the code that some variables are not easily
accessible.
You need to pass on a reference of them and a whole bunch of this actually comes in.
And then we have these arrow functions so we can.
So there's a lot of things up here.
So what you're going to see in most of the production grade code is happen something like this.
So people like to go ahead and create a simple helper function.
And what this helper function does is nothing.
It just wraps around your entirety of the code or function that you're passing up and just puts a kind
of a promise around it.
So that's basically it.
So let's just call this one as simply I usually call this one in all of my code as a big promise because
it's kind of a big brother.
So what you do is you obviously are going to receive a function on this because this entire request
and response, whatever is going on, you just pass on it to as a test.
So this is my function that I receive up here.
Now, as soon as I received this one, I take that as a kind of an argument and I pass it on further
down the road to this one.
So request response and next.
Next is very important in writing this help.
To function further down the road.
I go ahead and say, Hey, I will create my promise myself and promise.
So whatever the default promise you are going to use, it's going to come in.
And this promise has two property.
The first one is resolve and this is how the resolve works.
And the second one is dot catch.
So in the catch I go ahead and say next.
So that further whoever is actually running this can actually handle this one.
But in the case of resolve, obviously the function that you're passing me up, I need to execute that
inside this one.
So I go ahead and say, hey, function.
And obviously in this function you'll give me the parameters that are coming in, request, response
and next.
So that's it.
That is it.
I know this is a little bit kind of a mode for you to understand, but in case you have taken my classes
on the YouTube of JavaScript of higher order functions and all of that, this is how it works.
Now, once you have defined this higher order function, what is going to happen now?
After that, things actually becomes a lot more easier.
And you can just wrap this up around entirety of the thing into a simple big promise up here.
I know this is a lot for you to digest, so go ahead, watch this video, read a little bit of the documentation.
And this is kind of important thing for you to understand, either go with entirely of the dot dot catch
syntax if you want to use something like holding the reference data weight and all of that, then Big
Promise is definitely going to come in and we will be using that into the next section.
So don't you worry too much.
I'll explain that one more time.
So that is easier for you to understand.
I know this was this video was a little bit heavy, so go ahead, rewatch it or probably just wait for
the next section to come up.
Let's go ahead and catch up in the next video.