In this video we're going to take just a couple of minutes to talk about errors in node j s so we're
gonna take our working program and we're gonna break it generating an error message.
Then we'll look at its structure and figure out how we can use the information it provides us to easily
figure out where in our program things are going wrong.
And my hope is that this very short video can save you much more time in the long run by making it easier
for you to find the issues in your application.
So down below we're going to take our working app and break it by making a change that's going to cause
an error to show up to do this.
I'm going to change the code and save notes right here.
We create a data Jason variable and then we reference it writing its content to a file.
All I'm going to do is change the upper case as to a lower case one that's going to cause the script
to crash as we're trying to reference a variable that's never been defined.
Now let's go ahead and actually run the program to see the error message.
We're gonna get down below.
We have to use a command that would actually have save notes get called.
That could be remove or add.
I'll just use add.
So node app dot J.S. add I'm going to add a new note with the following title.
I'll use something like error message.
Then I'll set up my body as well.
And right here I can just use.
Run it.
Perfect.
Now if I run this it is going to have the effect of running save notes when it tries to save our newly
added note to the data store and the program is indeed going to crash.
So let's go ahead and hit enter to generate the error message.
Now if we scroll up we can break this down and the most important piece is right here after a new line
maybe about four or five lines down.
This is the actual error message.
This is the explanation from v 8 as to why your program cut it run.
In this case it is a reference error which is a whole category of errors and it has an explicit message
data.
Jason is not defined so this is letting us know exactly why things failed.
But it's not really letting us know why.
So it's still going to be up to us to figure out what's going wrong is data.
Jason not defined because I've misspelled it or is it not defined because maybe Jason dot string ify
didn't give us back what we thought it was going to give us back.
So when we referenced it later it broke.
So it'll still be up to us to really work through the problem that caused the error but the error can
get us to the right part of the code.
Now below the message what we just explored.
We have a stack trace this contains a trace of all of the functions that are running to get to this
point in the code and you'll notice the first line right after our message contains the call to save
notes.
So it's saying that we are at Save notes and it points to the correct file that is in Notes app.
It is called Notes Doc J S and it even points to the line and the column so line 56 column 36.
That is exactly right here where the problem occurred.
So that first line after it your message contains the most useful information in the stack trace.
As we go down the trace we typically tend to get less actionable information from here we can see that
save notes was called by add note which was true that was called by handler which is true handler being
the method we defined an app dot J S on line at twenty five.
And if I go over there that's exactly what we're going to see.
We have our handler method and on line twenty five we do indeed call add note.
So it save notes to add note to handler and then after that we start to get into the internals in this
case a lot of these are coming from yards we can see things related to run command pass args pass and
others and the further we get down the list the closer we get to the node internals which in this case
are these last three.
So when we're looking at the stack trace we want to start from top to bottom as the top contains the
most explicit pertinent information related to the error.
That's where I'd like to stop for this video.
You now know a bit about how to work with error messages and how to use the no debugger to debug your
node applications using the Chrome developer tools.
These are both techniques you can now use throughout the rest of the class to fix your issues faster.
Now you're still going to make issues that is expected and completely normal.
But it does get better over time as you get more comfortable with node you'll make less and less issues
for yourself to solve and the ones you do create you'll be able to solve faster.
But like everything it is a skill set that needs to be built up over time.
We're gonna start to talk about asynchronous development in the next section.
So let's go ahead and jump right into that.