From 2918527bcba6b6b25b226ab4b2284e82147a2c83 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Sun, 1 Sep 2019 15:05:41 -0400 Subject: [PATCH] Remove errMessage --- errors.ts | 6 ++--- routes/adjustmentMethods/AdjustmentMethod.ts | 8 ------ .../adjustmentMethods/EToAdjustmentMethod.ts | 4 +-- .../ZimmermanAdjustmentMethod.ts | 1 - routes/weather.ts | 26 +++++++++---------- routes/weatherProviders/DarkSky.ts | 8 +++--- routes/weatherProviders/OWM.ts | 8 +++--- routes/weatherProviders/WUnderground.ts | 6 ++--- routes/weatherProviders/WeatherProvider.ts | 4 +-- 9 files changed, 29 insertions(+), 42 deletions(-) diff --git a/errors.ts b/errors.ts index c6e6c25..1974697 100644 --- a/errors.ts +++ b/errors.ts @@ -59,9 +59,8 @@ export class CodedError { public readonly errCode: ErrorCode; public readonly message: string; - public constructor( errCode: ErrorCode, message: string ) { + public constructor( errCode: ErrorCode ) { this.errCode = errCode; - this.message = message; } } @@ -79,7 +78,6 @@ export function makeCodedError( err: any ): CodedError { if ( err instanceof CodedError ) { return err; } else { - // Include the current timestamp in the error message so the full error can easily be found in the logs. - return new CodedError( ErrorCode.UnexpectedError, "Unexpected error occurred at timestamp " + Date.now() ); + return new CodedError( ErrorCode.UnexpectedError ); } } diff --git a/routes/adjustmentMethods/AdjustmentMethod.ts b/routes/adjustmentMethods/AdjustmentMethod.ts index 2a6aaba..f80a3b1 100644 --- a/routes/adjustmentMethods/AdjustmentMethod.ts +++ b/routes/adjustmentMethods/AdjustmentMethod.ts @@ -44,14 +44,6 @@ export interface AdjustmentMethodResponse { * watering. */ rainDelay?: number; - /** - * An message to send to the OS firmware to indicate that an error occurred while calculating the watering - * scale and the returned scale either defaulted to some reasonable value or was calculated with incomplete data. - * Older firmware versions will ignore this field (they will silently swallow the error and use the returned scale), - * but newer firmware versions may be able to alert the user that an error occurred and/or default to a - * user-configured watering scale instead of using the one returned by the AdjustmentMethod. - */ - errMessage?: string; /** A code describing the type of error that occurred (if one occurred). */ errCode?: ErrorCode; /** The data that was used to calculate the watering scale, or undefined if no data was used. */ diff --git a/routes/adjustmentMethods/EToAdjustmentMethod.ts b/routes/adjustmentMethods/EToAdjustmentMethod.ts index 29afb96..abfba8a 100644 --- a/routes/adjustmentMethods/EToAdjustmentMethod.ts +++ b/routes/adjustmentMethods/EToAdjustmentMethod.ts @@ -18,7 +18,7 @@ async function calculateEToWateringScale( ): Promise< AdjustmentMethodResponse > { if ( pws ) { - throw new CodedError( ErrorCode.PwsNotSupported, "ETo adjustment method does not support personal weather stations through WUnderground." ); + throw new CodedError( ErrorCode.PwsNotSupported ); } // Temporarily disabled since OWM forecast data is checking if rain is forecasted for 3 hours in the future. @@ -41,7 +41,7 @@ async function calculateEToWateringScale( if ( adjustmentOptions && "baseETo" in adjustmentOptions ) { baseETo = adjustmentOptions.baseETo } else { - throw new CodedError( ErrorCode.MissingAdjustmentOption, "A baseline potential ETo must be provided." ); + throw new CodedError( ErrorCode.MissingAdjustmentOption ); } if ( adjustmentOptions && "elevation" in adjustmentOptions ) { diff --git a/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts b/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts index d4ba15b..bbbc7e3 100644 --- a/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts +++ b/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts @@ -43,7 +43,6 @@ async function calculateZimmermanWateringScale( scale: 100, rawData: rawData, errCode: ErrorCode.MissingWeatherField, - errMessage: "Necessary field(s) were missing from ZimmermanWateringData.", wateringData: wateringData }; } diff --git a/routes/weather.ts b/routes/weather.ts index e3411d6..047f88d 100644 --- a/routes/weather.ts +++ b/routes/weather.ts @@ -46,11 +46,11 @@ const cache = new WateringScaleCache(); export async function resolveCoordinates( location: string ): Promise< GeoCoordinates > { if ( !location ) { - throw new CodedError( ErrorCode.InvalidLocationFormat, "No location specified" ); + throw new CodedError( ErrorCode.InvalidLocationFormat ); } if ( filters.pws.test( location ) ) { - throw new CodedError( ErrorCode.InvalidLocationFormat, "PWS ID must be specified in the pws adjustment option." ); + throw new CodedError( ErrorCode.InvalidLocationFormat ); } else if ( filters.gps.test( location ) ) { const split: string[] = location.split( "," ); return [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ]; @@ -64,7 +64,7 @@ export async function resolveCoordinates( location: string ): Promise< GeoCoordi data = await httpJSONRequest( url ); } catch (err) { // If the request fails, indicate no data was found. - throw new CodedError( ErrorCode.LocationServiceApiError, "An API error occurred while attempting to resolve location" ); + throw new CodedError( ErrorCode.LocationServiceApiError ); } // Check if the data is valid @@ -75,7 +75,7 @@ export async function resolveCoordinates( location: string ): Promise< GeoCoordi } else { // Otherwise, indicate no data was found - throw new CodedError( ErrorCode.NoLocationFound, "No match found for specified location" ); + throw new CodedError( ErrorCode.NoLocationFound ); } } } @@ -196,7 +196,7 @@ export const getWateringData = async function( req: express.Request, res: expres remoteAddress = remoteAddress.split( "," )[ 0 ]; if ( !adjustmentMethod ) { - sendWateringData( res, { errCode: ErrorCode.InvalidAdjustmentMethod, errMessage: "Invalid AdjustmentMethod ID" } ); + sendWateringData( res, { errCode: ErrorCode.InvalidAdjustmentMethod } ); return; } @@ -210,7 +210,7 @@ export const getWateringData = async function( req: express.Request, res: expres adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" ); } catch ( err ) { // If the JSON is not valid then abort the calculation - sendWateringData( res, { errCode: ErrorCode.MalformedAdjustmentOptions, errMessage: `Unable to parse options (${ err })` } ); + sendWateringData( res, { errCode: ErrorCode.MalformedAdjustmentOptions } ); return; } @@ -224,7 +224,7 @@ export const getWateringData = async function( req: express.Request, res: expres console.error( `An unexpected error occurred during location resolution for "${ req.url }": `, err ); } - sendWateringData( res, { errCode: codedError.errCode, errMessage: `Unable to resolve location "${ location }" (${ codedError.message })` } ); + sendWateringData( res, { errCode: codedError.errCode } ); return; } @@ -241,11 +241,11 @@ export const getWateringData = async function( req: express.Request, res: expres // Make sure that the PWS ID and API key look valid. if ( !pwsId ) { - sendWateringData( res, { errCode: ErrorCode.InvalidPwsId, errMessage: "PWS ID does not appear to be valid" } ); + sendWateringData( res, { errCode: ErrorCode.InvalidPwsId } ); return; } if ( !apiKey ) { - sendWateringData( res, { errCode: ErrorCode.InvalidPwsApiKey, errMessage: "PWS API key does not appear to be valid" } ); + sendWateringData( res, { errCode: ErrorCode.InvalidPwsApiKey } ); return; } @@ -262,7 +262,6 @@ export const getWateringData = async function( req: express.Request, res: expres sunset: timeData.sunset, eip: ipToInt( remoteAddress ), rawData: undefined, - errMessage: undefined, errCode: 0 }; @@ -289,13 +288,12 @@ export const getWateringData = async function( req: express.Request, res: expres console.error( `An unexpected error occurred during watering scale calculation for "${ req.url }": `, err ); } - sendWateringData( res, { errCode: codedError.errCode, errMessage: codedError.message } ); + sendWateringData( res, { errCode: codedError.errCode } ); return; } data.scale = adjustmentMethodResponse.scale; data.errCode = adjustmentMethodResponse.errCode || 0; - data.errMessage = adjustmentMethodResponse.errMessage; data.rd = adjustmentMethodResponse.rainDelay; data.rawData = adjustmentMethodResponse.rawData; @@ -311,7 +309,7 @@ export const getWateringData = async function( req: express.Request, res: expres console.error( `An unexpected error occurred during restriction checks for "${ req.url }": `, err ); } - sendWateringData( res, { errCode: codedError.errCode, errMessage: codedError.message } ); + sendWateringData( res, { errCode: codedError.errCode } ); return; } } @@ -323,7 +321,7 @@ export const getWateringData = async function( req: express.Request, res: expres } // Cache the watering scale if caching is enabled and no error occurred. - if ( weatherProvider.shouldCacheWateringScale() && !data.errMessage ) { + if ( weatherProvider.shouldCacheWateringScale() ) { cache.storeWateringScale( req.params[ 0 ], coordinates, pws, adjustmentOptions, { scale: data.scale, rawData: data.rawData, diff --git a/routes/weatherProviders/DarkSky.ts b/routes/weatherProviders/DarkSky.ts index 5c6f336..f8133b7 100644 --- a/routes/weatherProviders/DarkSky.ts +++ b/routes/weatherProviders/DarkSky.ts @@ -29,11 +29,11 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { yesterdayData = await httpJSONRequest( yesterdayUrl ); } catch ( err ) { console.error( "Error retrieving weather information from Dark Sky:", err ); - throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from Dark Sky." ); + throw new CodedError( ErrorCode.WeatherApiError ); } if ( !yesterdayData.hourly || !yesterdayData.hourly.data ) { - throw new CodedError( ErrorCode.MissingWeatherField, "Necessary field(s) were missing from weather information returned by Dark Sky." ); + throw new CodedError( ErrorCode.MissingWeatherField ); } const samples = [ @@ -42,7 +42,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { // Fail if not enough data is available. if ( samples.length !== 24 ) { - throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data was returned by Dark Sky." ); + throw new CodedError( ErrorCode.InsufficientWeatherData ); } const totals = { temp: 0, humidity: 0, precip: 0 }; @@ -123,7 +123,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { try { historicData = await httpJSONRequest( historicUrl ); } catch (err) { - throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from Dark Sky." ); + throw new CodedError( ErrorCode.WeatherApiError ); } const cloudCoverInfo: CloudCoverInfo[] = historicData.hourly.data.map( ( hour ): CloudCoverInfo => { diff --git a/routes/weatherProviders/OWM.ts b/routes/weatherProviders/OWM.ts index 493929a..9aa169c 100644 --- a/routes/weatherProviders/OWM.ts +++ b/routes/weatherProviders/OWM.ts @@ -26,12 +26,12 @@ export default class OWMWeatherProvider extends WeatherProvider { forecast = await httpJSONRequest( forecastUrl ); } catch ( err ) { console.error( "Error retrieving weather information from OWM:", err ); - throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from OWM." ); + throw new CodedError( ErrorCode.WeatherApiError ); } // Indicate watering data could not be retrieved if the forecast data is incomplete. if ( !forecast || !forecast.list ) { - throw new CodedError( ErrorCode.MissingWeatherField, "Necessary field(s) were missing from weather information returned by OWM." ); + throw new CodedError( ErrorCode.MissingWeatherField ); } let totalTemp = 0, @@ -112,12 +112,12 @@ export default class OWMWeatherProvider extends WeatherProvider { forecast = await httpJSONRequest( forecastUrl ); } catch (err) { console.error( "Error retrieving ETo information from OWM:", err ); - throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving ETo information from OWM." ); + throw new CodedError( ErrorCode.WeatherApiError ); } // Indicate ETo data could not be retrieved if the forecast data is incomplete. if ( !forecast || !forecast.list || forecast.list.length < 8 ) { - throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data available from OWM." ); + throw new CodedError( ErrorCode.InsufficientWeatherData ); } // Take a sample over 24 hours. diff --git a/routes/weatherProviders/WUnderground.ts b/routes/weatherProviders/WUnderground.ts index 71114be..e83d41e 100644 --- a/routes/weatherProviders/WUnderground.ts +++ b/routes/weatherProviders/WUnderground.ts @@ -7,7 +7,7 @@ export default class WUnderground extends WeatherProvider { async getWateringData( coordinates: GeoCoordinates, pws?: PWS ): Promise< ZimmermanWateringData > { if ( !pws ) { - throw new CodedError( ErrorCode.NoPwsProvided, "WUnderground WeatherProvider requires a PWS to be specified." ); + throw new CodedError( ErrorCode.NoPwsProvided ); } const url = `https://api.weather.com/v2/pws/observations/hourly/7day?stationId=${ pws.id }&format=json&units=e&apiKey=${ pws.apiKey }`; @@ -16,7 +16,7 @@ export default class WUnderground extends WeatherProvider { data = await httpJSONRequest( url ); } catch ( err ) { console.error( "Error retrieving weather information from WUnderground:", err ); - throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from WUnderground." ); + throw new CodedError( ErrorCode.WeatherApiError ); } // Take the 24 most recent observations. @@ -24,7 +24,7 @@ export default class WUnderground extends WeatherProvider { // Fail if not enough data is available. if ( samples.length !== 24 ) { - throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data was returned by WUnderground." ); + throw new CodedError( ErrorCode.InsufficientWeatherData ); } const totals = { temp: 0, humidity: 0, precip: 0 }; diff --git a/routes/weatherProviders/WeatherProvider.ts b/routes/weatherProviders/WeatherProvider.ts index 0074fbe..078d347 100644 --- a/routes/weatherProviders/WeatherProvider.ts +++ b/routes/weatherProviders/WeatherProvider.ts @@ -13,7 +13,7 @@ export class WeatherProvider { * does not support this method). */ getWateringData( coordinates: GeoCoordinates, pws?: PWS ): Promise< ZimmermanWateringData > { - throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod, "Selected WeatherProvider does not support getWateringData" ); + throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod ); } /** @@ -34,7 +34,7 @@ export class WeatherProvider { * CodedError if an error occurs while retrieving the EToData (or the WeatherProvider does not support this method). */ getEToData( coordinates: GeoCoordinates ): Promise< EToData > { - throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod, "Selected WeatherProvider does not support getEToData" ); + throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod ); }; /**