Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Handling Errors in Node Handling Status Code Errors

Gerry K
Gerry K
5,877 Points

Question with else statement for error message in this video

Here's the code from the video...

else { const message = there was an error getting the profile for ${username} (${response.statusCode}); const statusCodeError = new Error(message);
printError(message); }


I don't understand why we are creating the statusCodeError variable, could someone please explain why this is required? And if it is required?

Thanks!

2 Answers

Steven Parker
Steven Parker
231,261 Points

It's only "required" to show the reason for the error to the user.

But first, you have an error in your code. The last line is to print the error string, and should be:

     printError(statusCodeError);

So now with that fixed, when an error occurs in getting the profile, the statusCodeError which was filled in with a message explaining what went wrong will appear on the screen. Without all this, if a problem occurred, you would just not see anything at all.

There is another error in your code. Don't forget to surround your string with `` or you will get an error. The reason we make an error variable is to utilize the printError function. Remember the printError function needs the parameter passed in to be an error since it calls error.message. We could simply print the string to display the error but using the printError function brings neatness and consistency to our code since we already used it to print all of our other errors.