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!

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 the Error Event in Node

Marcus Schumacher
Marcus Schumacher
16,616 Points

Can't get try catch block to work properly

At the end of the video, he gets a nice clean error message from his try catch block. I can not get my try catch block to display the error.message

// require https module
const https = require('https');

function printMessage(username, badgeCount, points) {
    const message = `${username} had ${badgeCount} total badge(s) and ${points} points in Javascript`;

function getProfile(username) {
    // conect to the API URL (https://teamtreehouse.com/username.json)
    try {
        const request = https.get(`https://wwwteamtreehouse.com/${username}.json`, response => {

            let body = "";
            // read the data
            response.on('data', data =>{
                body += data.toString();

            response.on('end', ()=>{
                // parse the data
                const profile = JSON.parse(body);
                // print out the information
                printMessage(username, profile.badges.length, profile.points.JavaScript);

        request.on('error', error => console.error(`Problem with request: ${error.message}`));
    } catch(error) { 
        console.error(error.message); // Why doesn't this work?
}// end getProfile()

const users = process.argv.slice(2);
users.forEach(username => {

1 Answer

Aakash Srivastava
Aakash Srivastava
5,415 Points

Hey friend Marcus Schumacher , you have done everything correctly , just a little mistake .
Notice ,in the video , when Chalkers is showing you the example of throwing error , then in first example , he wrote www but in the second video :

  1. he has removed the whole www
  2. he also removed the whole https:// so URL is only teamtreehouse.com But what you have done is , you have not removed www , it's still there . That's why , it's showing two error .
    One for 1 case and another for 2 case.
    Hope it will help :)