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

Harry Ramli
Harry Ramli
4,831 Points

Http request errors vs status code errors

What is the difference between handling errors using status codes and request.on(‘error’) ? I thought both are related to http get requests and error event runs when status code is anything but 200.

1 Answer

Kevin Gates
Kevin Gates
15,052 Points

Hi there.

Not every non-200 response is an error (list of response codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) . For instance, a 201 is a created response. A 301 means something was permanently moved.

Certain errors can occur when you are initially attempting to connect to the server. You may get a 500 error. Other times the server may not respond at all, and the connection attempt will timeout, resulting in an error.

So you need a catch for the error that occurs if the server doesn't respond.

Next, you will need to check to see the type of response that the server does give you. Depending on what you're asking and depending on what the response is, you will then decide if this is merely information (Something was created, 201) or is an error (404, not found).

Hope this helps. :)