Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video I'll show you how I added error handling all throughout my application.
Documentation
OpenWeatherMap API Solution Code
Here's an overview of the differences from the code shown in the video:
-
weather.js
- There's no need to create a readable query as the query value that's being passed into the method is already in a readable format
- There's no need to check if the location was found as the API won't return a "200" OK status code if the query wasn't successful
Here's the code:
weather.js
const http = require('http');
const https = require('https');
const querystring = require('querystring');
const api = require('./api.json');
// Print out temp details
function printWeather(weather) {
const message = `Current temperature in ${weather.name} is ${weather.main.temp}F`;
console.log(message);
}
// Print out error message
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);
}
}
module.exports.get = get;
-
0:01
First I implemented a printError function
-
0:04
that will display the message from the error object.
-
0:08
I used a try catch statement in case if the URL is malformed.
-
0:13
Remember, if a malformed URL is entered,
-
0:17
the error will be thrown immediately and not emitted.
-
0:21
In the catch block, I called the print error function for the malformed error.
-
0:28
Then I checked the status code was okay or 200.
-
0:33
Here, I created an error message with a human readable status message.
-
0:39
Remember, STATUS_CODES object is only on HTTP, not HTTPS.
-
0:48
I've also included a readable query, adding spaces back in to the query.
-
0:55
The code for
-
0:56
the readable query is up here, because I may need it for other errors later on.
-
1:01
Next I catch any thrown errors by the JSON parser.
-
1:06
Finally if the query doesn't return a location because it's spelt wrong, or
-
1:11
the place doesn't exist, we want to return a sensible error message.
-
1:19
Let's see this in action.
-
1:21
Type node app.js 90210.
-
1:28
And then a location.
-
1:35
And then somewhere fictional.
-
1:48
And we get that sensible error message.
-
1:50
Cool.
-
1:52
Great work, you've come a long way.
-
1:54
You should be really proud of yourself.
-
1:56
You've created two command line applications that go out on to
-
1:59
the Internet and retrieve information.
-
2:01
Now that you've worked with two API endpoints, you can use any.
-
2:06
The possibilities are endless.
-
2:08
Why not make a command line utility that goes out to Twitter and
-
2:12
brings back the latest tweets from your favorite JavaScript developer?
-
2:16
Or get the latest stock prices for your favorite tech company?
-
2:19
Or a command line application that converts currencies?
-
2:23
I encourage you to practice more to really solidify the concepts.
-
2:26
Looking at the documentation and other solutions isn't cheating.
-
2:30
They're aids for your learning.
-
2:32
Until next time, keep practicing.
You need to sign up for Treehouse in order to download course files.
Sign up