Created
January 20, 2018 23:45
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 20-42 not my code