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 trialJared Greenberg
6,887 PointsHandling Errors - Solution (Node.js)
I am getting very confused about error handling. I thought that try...catch statements bubble up and handle any error thrown within the try block. However the try...catch statement that surrounds the code in the get function below does NOT catch the error of having a mal-formed URL like its supposed to according to the instructor's video. When I change the url to something invalid, it warns me in the console that the error thrown is unhandled. I can ONLY get the proper error message handled if I do.... request.on('error', printError);
So my question is what then is the point of the try... catch block? What errors will it actually catch?
function printError(error) {
console.error(error.message);
}
function get(query) {
try{
const parameters = {
APPID: api.key,
units: 'imperial'
};
const zipCode = parseInt(query);
if (!isNaN(zipCode)) {
parameters.zip = zipCode + ',us';
} else {
parameters.q = query + ',us';
}
const url = `https://api.openweathermap.org/data/2.5/weather?${querystring.stringify(parameters)}`;
console.log(url);
const request = https.get(url, response => {
if (response.statusCode === 200) {
let body = '';
// Read the data
response.on('data', chunk => {
body += chunk;
});
response.on('end', () => {
try {
//Parse data
const weather = JSON.parse(body);
//Print the data
printWeather(weather);
} catch (error) {
//Parser error
printError(error);
}
});
} else {
// Status error code
const statusErrorCode = new Error(`There was an error getting the message for "${query}". (${http.STATUS_CODES[response.statusCode]})`);
printError(statusErrorCode);
}
});
} catch (error) {
printError(error);
}
}```
1 Answer
Joseph Lander
Full Stack JavaScript Techdegree Graduate 27,765 PointsYes, I also struggled here. Even direct cut and paste showed that the incorrect URL gave out unhandled error messages. I'm not sure how much is due to the API being deprecated or any other issue.
In fairness, I had used an online course from Udemy on this topic and they had to update the course due to a deprecating weather API as well. It must make it difficult to keep up with.
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 PointsJames Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 PointsI also do not get handling errors in general, and this objective gave me quite the headache. I've even worked on a few outside projects that just seem to not have errors, so I have no clue. Hopefully someone comes along and answers this.