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
    
      
  We can use a try catch block to watch for synchronous errors 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
                      Before we get started, make sure that the
HTTPS module's back in place in your URL.
                      0:00
                    
                    
                      The node error documentation,
                      0:06
                    
                    
                      it says that there is another node error
where we can use a try catch block.
                      0:08
                    
                    
                      This error is a parsing error.
                      0:12
                    
                    
                      Before we were given JSON we
could work with by the API.
                      0:14
                    
                    
                      Requesting a profile that doesn't
exist will return an invalid response,
                      0:18
                    
                    
                      which will cause an error
when we try to parse it.
                      0:22
                    
                    
                      Let's try running the program
with a fake username.
                      0:31
                    
                    
                      Node app.js veggiemikel.
                      0:36
                    
                    
                      We get an unexpected token error.
                      0:44
                    
                    
                      When we look for a profile that doesn't
exist, the message Not Found is returned.
                      0:47
                    
                    
                      This is why there's an N.
                      0:53
                    
                    
                      JSON should start with a square bracket or
curly braces.
                      0:55
                    
                    
                      This is why the letter N is unexpected.
                      1:00
                    
                    
                      We can wrap the code dealing
with parsing in a try block,
                      1:05
                    
                    
                      Catch the error,
                      1:14
                    
                    
                      And use console.error to
print out the error message.
                      1:23
                    
                    
                      Now, when we run it,
we simply see our error message.
                      1:33
                    
                    
                      Great work.
                      1:37
                    
                    
                      In the next video, we'll clean up our code
a little bit and learn more about errors.
                      1:39
                    
              
        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