JavaScript Node.js Basics Handling Errors in Node Handling the Error Event in Node

Marcus Schumacher
Marcus Schumacher
10,547 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 (
    try {
        const request = https.get(`${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,408 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 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 :)