Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Node.js Basics!
You have completed Node.js Basics!
Preview
Learn more about errors that occur in Node.js.
Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
As you may have already noticed, our apps
are bound to have some errors, whether
0:04
it's code, we've written or changes in
the resources and dependencies we use.
0:08
Thanks to the Error object.
0:13
We can get feedback from errors occur and
this helps us correct them.
0:14
Think about a math test for example, if
you handed in a math test, and the teacher
0:19
only returns the test back to students who
got a perfect score, that's not helpful.
0:24
If you hand in the test, and the teacher
marked which question you got wrong,
0:30
that's somewhat helpful.
0:34
If you handed in a test in the teacher
indicating not only which question you got
0:37
wrong but
indicating what type of mistake you made.
0:40
Maybe you forgot to carry the three.
0:44
That's something you can learn from,
done right.
0:45
A JavaScript error handling can
function like a great math teacher.
0:48
They can tell you not only where
your code stopped working, but
0:52
also what type of issue
caused the problem.
0:55
There are different types of errors that
can occur in node, and these include
0:58
standard JavaScript errors, system errors,
user specified errors and assertions.
1:03
Were able to find out
more about a given error,
1:09
including its type by
examining its properties.
1:12
These include the error stack,
1:15
which describes when
the error occurred in the code.
1:18
The error code, which tells us what
kind of error, there's a great list in
1:22
the documentation and the error message,
which is a string describing the error.
1:26
One method of error handling you might
be familiar with is the try-catch block.
1:32
try-catch block is an error handling
technique used to execute a block
1:37
of code and
1:41
throw an error message if something goes
wrong within the block to be executed.
1:41
These are useful in JavaScript, but
with many of Node's asynchronous methods,
1:47
the try-catch block is executed completely
before Node.js methods have a chance to
1:51
finish.
1:56
Because Node.js is asynchronous in nature,
1:57
we will handle most errors in
Node using asynchronous methods.
1:59
Let's intentionally create
an error in our project,
2:05
we remove the period from URL and
then save, and try to run our code.
2:09
We get a long error message that may be
difficult for app users to interpret.
2:14
The error messages includes
the code ENOTFOUND.
2:20
We can look this up in the node
error documentation and
2:25
find out if this is a result of
the address not being found.
2:28
Which makes sense for
an address that doesn't exist.
2:31
We can use the tools that node gives us to
handle this error in a more user friendly,
2:36
organized way.
2:41
An incorrect but valid URL in the
asynchronous block of code won't be caught
2:43
by something like a try catch block.
2:47
It's not until runtime or
the code is executed and
2:50
the async call starts, that this
error will be recognized.
2:52
Just like we watch for data in events
earlier, we can watch for errors as well.
2:57
On the request object we will
watch for events with on.
3:06
Watch out for the error event.
3:14
Give this a callback function
that takes the error.
3:18
And will use the console.error
method to print out the error.
3:24
I will save and will try it again and
this has removed some of the extra text,
3:34
but we can go even further and
narrow down specific properties
3:40
of the Error object. Let's go with
the error code for example.
3:46
There we go.
4:01
Now many users may not know
what these codes mean.
4:02
We can write a custom error
message in this handler as well.
4:06
There is our custom message.
4:24
Now, a bad argument,
4:25
like removing the HTTPS protocol
will cause an error right away.
4:27
When we remove the protocol from our URL,
4:32
Add the period back,
save, try running again.
4:40
We get an invalid URL error.
4:49
This is caused by a missing protocol or
invalid URL.
4:52
The asynchronous call cannot be made
without the correct protocol so
4:55
this error is synchronous.
4:58
In this instance, the program will
be unable to continue running.
5:01
Because of this, it is a rare case where
we can use a try-catch block in Node.
5:04
We'll wrap the request in a try block.
5:10
We'll catch the error, and
5:25
use console.error again to
log out the error message,
5:30
And there we have it only
the error message appears.
5:43
We can again customize this even
further to make the message our own.
5:47
Great work, in the next video we'll
look more at the try-catch block for
5:52
parsing errors.
5:56
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up