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

Issue(solved): Still show stack trace when I apply try-catch

function getProfile(username) {
  const URL = `teamtreehouse.com/${username}.json`
  try {
    const request = https.get(URL, res => {
      let body = ''
      res.on('data', data => {
        // read the data from buffer type
        body += data.toString()
      })

      res.on('end', () => {
        // parse the data
        const profileObj = JSON.parse(body) 
        // print the data
        printMessage(username, profileObj.badges.length, profileObj.points.JavaScript)
      })
    })

    request.on('error', error => {
      console.error('Problems is', error.message);
    })
  } catch (error) {
    console.log('Problem is:', error)
  }
}

cmd below

Problem is: TypeError [ERR_INVALID_DOMAIN_NAME]: Unable to determine the domain name
    at request (https.js:266:13)
    at Object.get (https.js:285:15)
    at getProfile (C:\Users\kao\Desktop\self\Programming\treeHouse_course_practice\nodejs_practice\building-a-command-line-app\app.js:19:27)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (C:\Users\kao\Desktop\self\Programming\treeHouse_course_practice\nodejs_practice\building-a-command-line-app\app.js:45:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,076 Points

I'm failing to understand your question? The try-catch is doing exactly what it's supposed to do, it's logging out an error that occurred within the try branch.

As for the error itself you'll have to look up information about the https.get function, because on of your parameters is either in the incorrect format, or you are providing it with a bad domain.

in the line I should use

catch (error) {
    console.log('Problem is:', error.message)
}

stupid syntax error..