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

Michael Lauridsen
Michael Lauridsen
10,321 Points

Why am I getting the whole emitted error here?

I made the new Error object with the message "There was an error getting the profile for michaellauridsen1 (404)", but besides that, the whole stack gets printed. I tried comparing Andrews code to mine, but I can't seem to find why the whole error stack follows.

Here's my code:

// Problem: We need a simple way to look at a user's badge count and JavaScript points
// Solution: Use Node.js to connect to Treehouse's API to get profile information to print out

//Require https module
const https = require("https");

//Print Error Messages
function printError(error) {
  console.error(error);
}

function getProfile(username) {
  try {
    //Function to print message to console
    function printMessage(username, badgeCount, points) {
      const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript`;
      console.log(message);
    }

    // Connect to the API URL (https://teamtreehouse.com/username.json)
    const request = https.get(
      `https://teamtreehouse.com/${username}.json`,
      response => {
        if (response.statusCode === 200) {
          // Read the  data
          let body = "";
          response.on("data", data => {
            body += data.toString();
          });
          response.on("end", () => {
            try {
              // Parse the data
              const profile = JSON.parse(body);
              // Print the data
              printMessage(
                username,
                profile.badges.length,
                profile.points.JavaScript
              );
            } catch (error) {
              printError(error.message);
            }
          });
        } else {
            const message = `There was an error getting the profile for ${username} (${response.statusCode})`;
            const statusCodeError = new Error(message);
            printError(statusCodeError);
        }
      }
    );
    request.on("error", error =>
      printError(`Problem with request: ${error.message}`)
    );
  } catch (error) {
    printError(error.message);
  }
}

const users = process.argv.slice(2);
users.forEach(getProfile);

Here's the error:

node app.js michaellauridsen1
Error: There was an error getting the profile for michaellauridsen1 (404)
    at ClientRequest.https.get.response (C:\Users\Michael\NodeJS Basics\Command line app\app.js:46:37)
    at ClientRequest.g (events.js:292:16)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at TLSSocket.socketOnData (_http_client.js:362:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)

Here's what I expected:

There was an error getting the profile for michaellauridsen1 (404)

2 Answers

Michael Lauridsen
Michael Lauridsen
10,321 Points

Ugh, found the error myself. the rewritten printError() function was printing the whole error instead of error.message.

Thank you for posting this with the solution! I had the same problem, and it was driving me nuts.