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 trialMarcus Schumacher
16,616 PointsCan'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`;
console.log(message);
}
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 => {
getProfile(username);
});
1 Answer
Aakash Srivastava
5,415 PointsHey 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 :
- he has removed the whole
www
- he also removed the whole
https://
so URL is onlyteamtreehouse.com
But what you have done is , you have not removedwww
, 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 :)