From 199399ff5eed4575865f876544c85883947850ef Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Wed, 1 Jul 2015 21:48:32 -0500 Subject: [PATCH] Add more error handling --- routes/weather.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index 519ad48..4f64e07 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -87,6 +87,10 @@ // If it is, reply with an array containing the GPS coordinates callback( [ data.RESULTS[0].lat, data.RESULTS[0].lon ] ); + } else { + + // Otherwise, indicate no data was found + callback( false ); } } ); } @@ -121,8 +125,16 @@ // Perform the HTTP request to retrieve the weather data httpRequest( url, function( data ) { - // Return the data to the callback function - callback( JSON.parse( data ) ); + try { + + // Return the data to the callback function if successful + callback( JSON.parse( data ) ); + } catch (err) { + + // Otherwise indicate the request failed + callback( false ); + } + } ); } @@ -280,7 +292,7 @@ eip: ipToInt( remoteAddress ) }; - // Return the response to the client + // Return the response to the client in the requested format if ( outputFormat === "json" ) { res.json( data ); } else { @@ -336,6 +348,11 @@ } getPWSCoordinates( location, weatherUndergroundKey, function( result ) { + if ( result === false ) { + res.send( "Error: Unable to resolve location" ); + return; + } + location = result; getWeatherData( location, finishRequest ); } ); @@ -344,6 +361,11 @@ // Attempt to resolve provided location to GPS coordinates when it does not match // a GPS coordinate or Weather Underground location using Weather Underground autocomplete resolveCoordinates( location, function( result ) { + if ( result === false ) { + res.send( "Error: Unable to resolve location" ); + return; + } + location = result; getWeatherData( location, finishRequest ); } );