Skip to content

Instantly share code, notes, and snippets.

@tmurvv
Created January 20, 2018 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmurvv/3470f8a438e4cb6f3e9c70f783ae5fbc to your computer and use it in GitHub Desktop.
Save tmurvv/3470f8a438e4cb6f3e9c70f783ae5fbc to your computer and use it in GitHub Desktop.
This function works if city is found, but how do I 'error handle' a 404 response?
function getJSONOuter(cityName) {
if (!cityName) {
var cityName = "Calgary";
}
if (cityName == "" || cityName == null) {
cityName = "Calgary";
}
try {
$.getJSON('https://cors.5apps.com/?uri=http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&units=metric&APPID=c3e00c8860695fd6096fe32896042eda', function (data) {
//Was not able to trigger this 404 code so I need to investigate more how to error handle a JSON call.
if (data.cod == 404) {
alert("404 initial if statement");
throw "city not found";
}
var windSpeedkmh = Math.round(data.wind.speed * 3.6);
var Celsius = Math.round(data.main.temp)
var iconId = data.weather[0].icon;
var weatherURL = "http://openweathermap.org/img/w/" +
iconId + ".png";
var iconImg = "<img src='" + weatherURL + "'>";
$('#location').html('Location: ' + data.name + ', ' + data.sys.country);
$('#sky-image').html(iconImg);
$('#weather-id').html('Skies: ' + data.weather[0].description);
$('#temperature').html(Celsius);
$('#toFahrenheit').click(function () {
$('#temperature').html(Math.round((9 / 5) * Celsius + 32));
$('#wind-speed').html(Math.round(windSpeedkmh * 0.621) + ' mph')
})
$('#toCelsius').click(function () {
$('#temperature').html(Celsius);
$('#wind-speed').html(windSpeedkmh + ' km/hr')
})
$('#wind-speed').html(windSpeedkmh + ' km/h');
$('#humidity').html('Humidity: ' + data.main.humidity + ' %');
})
}
catch (e) {
alert("Error: " + e);
}
try {
getForecast(cityName);
}
catch (e) {
alert("Forecast fail " + e);
}
}
@tmurvv
Copy link
Author

tmurvv commented Jan 20, 2018

Line 20-42 not my code

@tlo-johnson
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment