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
Request errors can come with status codes that tell us more about the type of error that occurred.
Documentation
Snippets
function printError(error) {
console.error(error.message);
}
const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
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
To keep our code DRY, let's add
a function that prints error messages.
0:00
I've added a snippet in the teacher's
notes, so you can just copy and paste.
0:05
We can call this function
in all of our handlers and
0:12
replace the statements
that are currently there.
0:14
So in our try block.
0:19
PrintError.
0:24
Save, Same here in our on method.
0:29
And we'll test this out by
running the code again.
0:57
We just get our error message, great.
1:00
HTTP provides us with status codes
that indicate the status of a request.
1:03
When a request is completed without issue,
we'll get a status of 200.
1:08
Maybe there was a server error,
which will give us a 500 error.
1:12
Maybe the endpoint moved,
and we'll get 301.
1:18
If the endpoint is incorrect,
we'll get a status code of 404 Not Found.
1:22
So far, we've assumed that each
API response will be okay.
1:28
Let's handle all the status
codes that aren't 200.
1:32
If the status code is 200,
we want to continue.
1:35
If not,
we want to print out an error message.
1:39
The message will say, there was an error
getting the profile for the username,
1:42
with the status code at the end.
1:46
Let's create a status code error,
by creating a new error object.
1:49
Inside the response callback,
we'll check if the status code equals 200.
1:53
If the status code isn't 200,
we'll create a new error to handle this.
2:05
We'll first create an error message where
we'll interpolate the HTTP status code.
2:11
And we'll need to include HTTP in our
file, using require to do this.
2:19
Actually copy this,
remove the S in both places.
2:27
Now on a new line,
create a new error object.
2:36
And now, we use printError,
print out that statusCodeError.
2:54
Save.
3:06
There we have it.
3:15
Our error message with the correct
status code appended to the end.
3:17
Well done, you've gotten acquainted
with some of the errors and
3:24
error types in Node.
3:27
Take your learning further by getting
familiar with the different possible
3:29
errors in the documentation.
3:32
When you get an error in your code,
do your best to track where and
3:34
why it is occurring.
3:37
This will help you improve
your debugging skills and
3:39
getting a better understanding
of the language.
3:41
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