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 Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Aakash Srivastava
Aakash Srivastava
5,415 Points

Error handling in Node.js

I have a question which took my whole day but still scratching my head .
I am wondering , why Errors are being handled differently in this course ?
When i saw profile.js file , errors have been handled differently here. Why?

I remember , Chalkley told us about the types of Errors in Node.basics course and that were :

  1. Asynchronous errors
  2. Error from developers perspective
  3. Parsing errors
  4. status code errors
    Now , all the errors have been handled in this course too . The only difference is Asynchronous Error is handled this time on response object inspite of request object which happens in last course . Why is this so? I have really given a lot of time understanding this , any help would be much appreciated .
    Please explain in detail . Jennifer Nordell

1 Answer

Bob Swaney
Bob Swaney
13,010 Points

Hello, I'm not 100% sure, as I am in the process of taking this course as well...but after doing some digging on the nodejs.org website and looking through the code that we are working on up to this point, I have deduced the following... At one point in this section, we copy a snippet of code from 'example_profile.js' into 'router.js' ... require('./profile.js') ... we haven't really gone through this massive file, we just include it...It handles a lot of things that we didn't really touch on..and if you notice, through this very lesson exercise, we are getting back badge counts from the Treehouse API without having to use the ('https') or ('http') modules and using the .get() method on either like we did the Node.js basics...so how are we connecting with it? Through requiring the profile.js file... So on to the error handling...if you notice when we put in the wrong username information, we get back an error without a stack trace, that seems very similar to the 'brushed up' code that we did in Node.js Basics course...it is simplified and also shows the 'http status code' not by number (404), but by (Not Found)...this snippet of code is also found within the 'profile.js' file we require in 'router.js'..

from profile.js --

if (response.statusCode !== 200) {
            //Status Code Error
            profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));

So, it looks like we are tackling harder issues for the moment, with being able to run and connect to an http server in general, and are less worried about the various different way to handle errors for the moment. I hope this helps and makes sense.