Merge pull request #26 from Derpthemeus/improve-resolveCoordinates

Improve location resolution error handling
This commit is contained in:
Samer Albahra
2019-05-13 21:04:33 -05:00
committed by GitHub

View File

@@ -26,8 +26,12 @@ const filters = {
*/ */
async function resolveCoordinates( location: string ): Promise< GeoCoordinates > { async function resolveCoordinates( location: string ): Promise< GeoCoordinates > {
if ( !location ) {
throw "No location specified";
}
if ( filters.pws.test( location ) ) { if ( filters.pws.test( location ) ) {
throw "Unable to resolve location"; throw "Weather Underground is discontinued";
} else if ( filters.gps.test( location ) ) { } else if ( filters.gps.test( location ) ) {
const split: string[] = location.split( "," ); const split: string[] = location.split( "," );
return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ]; return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
@@ -52,7 +56,7 @@ async function resolveCoordinates( location: string ): Promise< GeoCoordinates >
} else { } else {
// Otherwise, indicate no data was found // Otherwise, indicate no data was found
throw "Unable to resolve location"; throw "No match found for specified location";
} }
} }
} }
@@ -189,16 +193,11 @@ function checkWeatherRestriction( adjustmentValue: number, weather: WateringData
export const getWeatherData = async function( req: express.Request, res: express.Response ) { export const getWeatherData = async function( req: express.Request, res: express.Response ) {
const location: string = getParameter(req.query.loc); const location: string = getParameter(req.query.loc);
if ( !location ) {
res.send( "Error: Unable to resolve location" );
return;
}
let coordinates: GeoCoordinates; let coordinates: GeoCoordinates;
try { try {
coordinates = await resolveCoordinates( location ); coordinates = await resolveCoordinates( location );
} catch (err) { } catch (err) {
res.send( "Error: Unable to resolve location" ); res.send(`Error: Unable to resolve location (${err})`);
return; return;
} }
@@ -229,12 +228,6 @@ export const getWateringData = async function( req: express.Request, res: expres
adjustmentOptions: AdjustmentOptions; adjustmentOptions: AdjustmentOptions;
// Exit if no location is provided
if ( !location ) {
res.send( "Error: No location provided." );
return;
}
// X-Forwarded-For header may contain more than one IP address and therefore // X-Forwarded-For header may contain more than one IP address and therefore
// the string is split against a comma and the first value is selected // the string is split against a comma and the first value is selected
remoteAddress = remoteAddress.split( "," )[ 0 ]; remoteAddress = remoteAddress.split( "," )[ 0 ];
@@ -253,33 +246,15 @@ export const getWateringData = async function( req: express.Request, res: expres
adjustmentOptions = undefined; adjustmentOptions = undefined;
} }
// Attempt to resolve provided location to GPS coordinates.
let coordinates: GeoCoordinates; let coordinates: GeoCoordinates;
// Parse location string try {
if ( filters.pws.test( location ) ) { coordinates = await resolveCoordinates( location );
} catch (err) {
// Weather Underground is discontinued and PWS or ICAO cannot be resolved res.send(`Error: Unable to resolve location (${err})`);
res.send( "Error: Weather Underground is discontinued." );
return; return;
} else if ( filters.gps.test( location ) ) {
// Handle GPS coordinates by storing each coordinate in an array
const splitLocation: string[] = location.split( "," );
coordinates = [ parseFloat( splitLocation[ 0 ] ), parseFloat( splitLocation[ 1 ] ) ];
location = coordinates;
} else {
// Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete
try {
coordinates = await resolveCoordinates( location );
} catch (err) {
res.send("Error: Unable to resolve location");
return;
}
location = coordinates;
} }
location = coordinates;
// Continue with the weather request // Continue with the weather request
let timeData: TimeData = getTimeData( coordinates ); let timeData: TimeData = getTimeData( coordinates );