Add error codes to watering data errors
This commit is contained in:
@@ -4,6 +4,7 @@ import { GeoCoordinates, WeatherData, ZimmermanWateringData } from "../../types"
|
||||
import { httpJSONRequest } from "../weather";
|
||||
import { WeatherProvider } from "./WeatherProvider";
|
||||
import { approximateSolarRadiation, CloudCoverInfo, EToData } from "../adjustmentMethods/EToAdjustmentMethod";
|
||||
import { CodedError, ErrorCode } from "../../errors";
|
||||
|
||||
export default class DarkSkyWeatherProvider extends WeatherProvider {
|
||||
|
||||
@@ -28,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 "An error occurred while retrieving weather information from Dark Sky."
|
||||
throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from Dark Sky." );
|
||||
}
|
||||
|
||||
if ( !yesterdayData.hourly || !yesterdayData.hourly.data ) {
|
||||
throw "Necessary field(s) were missing from weather information returned by Dark Sky.";
|
||||
throw new CodedError( ErrorCode.MissingWeatherField, "Necessary field(s) were missing from weather information returned by Dark Sky." );
|
||||
}
|
||||
|
||||
const samples = [
|
||||
@@ -41,7 +42,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
|
||||
|
||||
// Fail if not enough data is available.
|
||||
if ( samples.length !== 24 ) {
|
||||
throw "Insufficient data was returned by Dark Sky.";
|
||||
throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data was returned by Dark Sky." );
|
||||
}
|
||||
|
||||
const totals = { temp: 0, humidity: 0, precip: 0 };
|
||||
@@ -122,7 +123,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider {
|
||||
try {
|
||||
historicData = await httpJSONRequest( historicUrl );
|
||||
} catch (err) {
|
||||
throw "An error occurred while retrieving weather information from Dark Sky."
|
||||
throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from Dark Sky." );
|
||||
}
|
||||
|
||||
const cloudCoverInfo: CloudCoverInfo[] = historicData.hourly.data.map( ( hour ): CloudCoverInfo => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { httpJSONRequest } from "../weather";
|
||||
import { WeatherProvider } from "./WeatherProvider";
|
||||
import { approximateSolarRadiation, CloudCoverInfo, EToData } from "../adjustmentMethods/EToAdjustmentMethod";
|
||||
import * as moment from "moment";
|
||||
import { CodedError, ErrorCode } from "../../errors";
|
||||
|
||||
export default class OWMWeatherProvider extends WeatherProvider {
|
||||
|
||||
@@ -25,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 "An error occurred while retrieving weather information from OWM."
|
||||
throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from OWM." );
|
||||
}
|
||||
|
||||
// Indicate watering data could not be retrieved if the forecast data is incomplete.
|
||||
if ( !forecast || !forecast.list ) {
|
||||
throw "Necessary field(s) were missing from weather information returned by OWM.";
|
||||
throw new CodedError( ErrorCode.MissingWeatherField, "Necessary field(s) were missing from weather information returned by OWM." );
|
||||
}
|
||||
|
||||
let totalTemp = 0,
|
||||
@@ -111,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 "An error occurred while retrieving ETo information from OWM."
|
||||
throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving ETo information from OWM." );
|
||||
}
|
||||
|
||||
// Indicate ETo data could not be retrieved if the forecast data is incomplete.
|
||||
if ( !forecast || !forecast.list || forecast.list.length < 8 ) {
|
||||
throw "Insufficient data available from OWM."
|
||||
throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data available from OWM." );
|
||||
}
|
||||
|
||||
// Take a sample over 24 hours.
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { GeoCoordinates, PWS, WeatherData, ZimmermanWateringData } from "../../types";
|
||||
import { WeatherProvider } from "./WeatherProvider";
|
||||
import { httpJSONRequest } from "../weather";
|
||||
import { CodedError, ErrorCode } from "../../errors";
|
||||
|
||||
export default class WUnderground extends WeatherProvider {
|
||||
|
||||
async getWateringData( coordinates: GeoCoordinates, pws?: PWS ): Promise< ZimmermanWateringData > {
|
||||
if ( !pws ) {
|
||||
throw "WUnderground WeatherProvider requires a PWS to be specified.";
|
||||
throw new CodedError( ErrorCode.NoPwsProvided, "WUnderground WeatherProvider requires a PWS to be specified." );
|
||||
}
|
||||
|
||||
const url = `https://api.weather.com/v2/pws/observations/hourly/7day?stationId=${ pws.id }&format=json&units=e&apiKey=${ pws.apiKey }`;
|
||||
@@ -15,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 "An error occurred while retrieving weather information from WUnderground."
|
||||
throw new CodedError( ErrorCode.WeatherApiError, "An error occurred while retrieving weather information from WUnderground." );
|
||||
}
|
||||
|
||||
// Take the 24 most recent observations.
|
||||
@@ -23,7 +24,7 @@ export default class WUnderground extends WeatherProvider {
|
||||
|
||||
// Fail if not enough data is available.
|
||||
if ( samples.length !== 24 ) {
|
||||
throw "Insufficient data was returned by WUnderground.";
|
||||
throw new CodedError( ErrorCode.InsufficientWeatherData, "Insufficient data was returned by WUnderground." );
|
||||
}
|
||||
|
||||
const totals = { temp: 0, humidity: 0, precip: 0 };
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { GeoCoordinates, PWS, WeatherData, ZimmermanWateringData } from "../../types";
|
||||
import { EToData } from "../adjustmentMethods/EToAdjustmentMethod";
|
||||
import { CodedError, ErrorCode } from "../../errors";
|
||||
|
||||
export class WeatherProvider {
|
||||
/**
|
||||
@@ -8,11 +9,11 @@ export class WeatherProvider {
|
||||
* @param pws The PWS to retrieve the weather from, or undefined if a PWS should not be used. If the implementation
|
||||
* of this method does not have PWS support, this parameter may be ignored and coordinates may be used instead.
|
||||
* @return A Promise that will be resolved with the ZimmermanWateringData if it is successfully retrieved,
|
||||
* or rejected with an error message if an error occurs while retrieving the ZimmermanWateringData or the WeatherProvider
|
||||
* does not support this method.
|
||||
* or rejected with a CodedError if an error occurs while retrieving the ZimmermanWateringData (or the WeatherProvider
|
||||
* does not support this method).
|
||||
*/
|
||||
getWateringData( coordinates: GeoCoordinates, pws?: PWS ): Promise< ZimmermanWateringData > {
|
||||
throw "Selected WeatherProvider does not support getWateringData";
|
||||
throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod, "Selected WeatherProvider does not support getWateringData" );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,12 +30,11 @@ export class WeatherProvider {
|
||||
/**
|
||||
* Retrieves the data necessary for calculating potential ETo.
|
||||
* @param coordinates The coordinates to retrieve the data for.
|
||||
* @return A Promise that will be resolved with the EToData if it is successfully retrieved,
|
||||
* or rejected with an error message if an error occurs while retrieving the EToData or the WeatherProvider does
|
||||
* not support this method.
|
||||
* @return A Promise that will be resolved with the EToData if it is successfully retrieved, or rejected with a
|
||||
* CodedError if an error occurs while retrieving the EToData (or the WeatherProvider does not support this method).
|
||||
*/
|
||||
getEToData( coordinates: GeoCoordinates ): Promise< EToData > {
|
||||
throw "Selected WeatherProvider does not support getEToData";
|
||||
throw new CodedError( ErrorCode.UnsupportedAdjustmentMethod, "Selected WeatherProvider does not support getEToData" );
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user