3

Whilst trying to complete a Node.js course on Treehouse.com I could not get the .on("error") handling of https.get() to work. Below is my code:

var https = require("https");

// Print out message
function printMessage(username, badgeCount, points) {
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript.";
  console.log(message);
}

// Print out error messages
function printError(error) {
  console.error(error.message);
}

function get(username) {
  // Connect to the API URL (https://teamtreehouse/username.json)
  var request = https.get("teamtreehouse.com/" + username + ".json", function(response){
    var body = "";
    // Read the data
    response.on("data", function (chunk) {
      body += chunk;
    });
    response.on("end", function(){
      if (response.statusCode === 200) {
        try {
          // Parse the data
          var profile = JSON.parse(body);
          // Print the data
          printMessage(username, profile.badges.length, profile.points.JavaScript);
        } catch (error) {
          // Parse Error
          printError(error);
        }
      } else {
        // Status Error
        printError({message: "The user: " + username + " can not be found. HTTP Status Code: " + response.statusCode});

      }
    });
  });
  // Connection error
  request.on("error", printError);
}

module.exports.get = get;

The protocol from the URL has been removed on purpose to cause a connection error. This should be caught by request.on("error", printError); however it is not. Instead the feedback in the console is:

https.js:191
      throw new Error('Unable to determine the domain name');
      ^

Error: Unable to determine the domain name
    at Object.exports.request (https.js:191:13)
    at Object.exports.get (https.js:201:21)
    at get (/Users/dangranger/Sites/sandbox/JavaScript/Node/profile.js:16:23)
    at Array.forEach (native)
    at Object.<anonymous> (/Users/dangranger/Sites/sandbox/JavaScript/Node/treehouseApp.js:3:7)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)

I am using Node.js 5.6.0

2
  • 1
    The .on('error') handler only fires when something goes wrong with the request itself, not being able to resolve the domain name isn't a request error it seems Commented Feb 20, 2016 at 12:56
  • This is the same code they use in the tutorial and it appears to work for them when they remove the protocol from the URL as I have to trigger the error event, albeit with an older version of Node.js? Commented Feb 20, 2016 at 13:00

1 Answer 1

0

Thanks @adeneo, it appears that you are right. It appears to catch this kind of error you need a try catch block as explained in this answer Why is this basic Node.js error handling not working?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.