Add more error handling

This commit is contained in:
Samer Albahra
2015-07-01 21:48:32 -05:00
parent 3091fecb43
commit 199399ff5e

View File

@@ -87,6 +87,10 @@
// If it is, reply with an array containing the GPS coordinates // If it is, reply with an array containing the GPS coordinates
callback( [ data.RESULTS[0].lat, data.RESULTS[0].lon ] ); 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 // Perform the HTTP request to retrieve the weather data
httpRequest( url, function( data ) { httpRequest( url, function( data ) {
// Return the data to the callback function try {
callback( JSON.parse( data ) );
// 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 ) eip: ipToInt( remoteAddress )
}; };
// Return the response to the client // Return the response to the client in the requested format
if ( outputFormat === "json" ) { if ( outputFormat === "json" ) {
res.json( data ); res.json( data );
} else { } else {
@@ -336,6 +348,11 @@
} }
getPWSCoordinates( location, weatherUndergroundKey, function( result ) { getPWSCoordinates( location, weatherUndergroundKey, function( result ) {
if ( result === false ) {
res.send( "Error: Unable to resolve location" );
return;
}
location = result; location = result;
getWeatherData( location, finishRequest ); getWeatherData( location, finishRequest );
} ); } );
@@ -344,6 +361,11 @@
// Attempt to resolve provided location to GPS coordinates when it does not match // Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete // a GPS coordinate or Weather Underground location using Weather Underground autocomplete
resolveCoordinates( location, function( result ) { resolveCoordinates( location, function( result ) {
if ( result === false ) {
res.send( "Error: Unable to resolve location" );
return;
}
location = result; location = result;
getWeatherData( location, finishRequest ); getWeatherData( location, finishRequest );
} ); } );