Throw errors instead of returning them
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { BaseWateringData, GeoCoordinates, PWS } from "../../types";
|
import { BaseWateringData, GeoCoordinates, PWS } from "../../types";
|
||||||
import { WeatherProvider } from "../weatherProviders/WeatherProvider";
|
import { WeatherProvider } from "../weatherProviders/WeatherProvider";
|
||||||
import { ErrorCode } from "../../errors";
|
|
||||||
|
|
||||||
|
|
||||||
export interface AdjustmentMethod {
|
export interface AdjustmentMethod {
|
||||||
@@ -44,8 +43,6 @@ export interface AdjustmentMethodResponse {
|
|||||||
* watering.
|
* watering.
|
||||||
*/
|
*/
|
||||||
rainDelay?: number;
|
rainDelay?: number;
|
||||||
/** 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. */
|
/** The data that was used to calculate the watering scale, or undefined if no data was used. */
|
||||||
wateringData: BaseWateringData;
|
wateringData: BaseWateringData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { AdjustmentMethod, AdjustmentMethodResponse, AdjustmentOptions } from ".
|
|||||||
import { GeoCoordinates, PWS, ZimmermanWateringData } from "../../types";
|
import { GeoCoordinates, PWS, ZimmermanWateringData } from "../../types";
|
||||||
import { validateValues } from "../weather";
|
import { validateValues } from "../weather";
|
||||||
import { WeatherProvider } from "../weatherProviders/WeatherProvider";
|
import { WeatherProvider } from "../weatherProviders/WeatherProvider";
|
||||||
import { ErrorCode } from "../../errors";
|
import { CodedError, ErrorCode } from "../../errors";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,12 +39,7 @@ async function calculateZimmermanWateringScale(
|
|||||||
// Check to make sure valid data exists for all factors
|
// Check to make sure valid data exists for all factors
|
||||||
if ( !validateValues( [ "temp", "humidity", "precip" ], wateringData ) ) {
|
if ( !validateValues( [ "temp", "humidity", "precip" ], wateringData ) ) {
|
||||||
// Default to a scale of 100% if fields are missing.
|
// Default to a scale of 100% if fields are missing.
|
||||||
return {
|
throw new CodedError( ErrorCode.MissingWeatherField );
|
||||||
scale: 100,
|
|
||||||
rawData: rawData,
|
|
||||||
errCode: ErrorCode.MissingWeatherField,
|
|
||||||
wateringData: wateringData
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let humidityBase = 30, tempBase = 70, precipBase = 0;
|
let humidityBase = 30, tempBase = 70, precipBase = 0;
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
remoteAddress = remoteAddress.split( "," )[ 0 ];
|
remoteAddress = remoteAddress.split( "," )[ 0 ];
|
||||||
|
|
||||||
if ( !adjustmentMethod ) {
|
if ( !adjustmentMethod ) {
|
||||||
sendWateringData( res, { errCode: ErrorCode.InvalidAdjustmentMethod } );
|
sendWateringError( res, new CodedError( ErrorCode.InvalidAdjustmentMethod ));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +210,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" );
|
adjustmentOptions = JSON.parse( "{" + adjustmentOptionsString + "}" );
|
||||||
} catch ( err ) {
|
} catch ( err ) {
|
||||||
// If the JSON is not valid then abort the calculation
|
// If the JSON is not valid then abort the calculation
|
||||||
sendWateringData( res, { errCode: ErrorCode.MalformedAdjustmentOptions } );
|
sendWateringError( res, new CodedError( ErrorCode.MalformedAdjustmentOptions ) );
|
||||||
return;
|
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 );
|
console.error( `An unexpected error occurred during location resolution for "${ req.url }": `, err );
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWateringData( res, { errCode: codedError.errCode } );
|
sendWateringError( res, codedError );
|
||||||
return;
|
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.
|
// Make sure that the PWS ID and API key look valid.
|
||||||
if ( !pwsId ) {
|
if ( !pwsId ) {
|
||||||
sendWateringData( res, { errCode: ErrorCode.InvalidPwsId } );
|
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ) );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( !apiKey ) {
|
if ( !apiKey ) {
|
||||||
sendWateringData( res, { errCode: ErrorCode.InvalidPwsApiKey } );
|
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ) );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,12 +288,11 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
console.error( `An unexpected error occurred during watering scale calculation for "${ req.url }": `, err );
|
console.error( `An unexpected error occurred during watering scale calculation for "${ req.url }": `, err );
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWateringData( res, { errCode: codedError.errCode } );
|
sendWateringError( res, codedError );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
data.scale = adjustmentMethodResponse.scale;
|
data.scale = adjustmentMethodResponse.scale;
|
||||||
data.errCode = adjustmentMethodResponse.errCode || 0;
|
|
||||||
data.rd = adjustmentMethodResponse.rainDelay;
|
data.rd = adjustmentMethodResponse.rainDelay;
|
||||||
data.rawData = adjustmentMethodResponse.rawData;
|
data.rawData = adjustmentMethodResponse.rawData;
|
||||||
|
|
||||||
@@ -309,7 +308,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
console.error( `An unexpected error occurred during restriction checks for "${ req.url }": `, err );
|
console.error( `An unexpected error occurred during restriction checks for "${ req.url }": `, err );
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWateringData( res, { errCode: codedError.errCode } );
|
sendWateringError( res, codedError );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -333,6 +332,16 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
sendWateringData( res, data, outputFormat === "json" );
|
sendWateringData( res, data, outputFormat === "json" );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a response to a watering scale request with an error code and a default watering scale of 100%.
|
||||||
|
* @param res The Express Response object to send the response through.
|
||||||
|
* @param error The error code to send in the response body.
|
||||||
|
* @param useJson Indicates if the response body should use a JSON format instead of a format similar to URL query strings.
|
||||||
|
*/
|
||||||
|
function sendWateringError( res: express.Response, error: CodedError, useJson: boolean = false ) {
|
||||||
|
sendWateringData( res, { errCode: error.errCode, scale: 100 } );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a response to an HTTP request with a 200 status code.
|
* Sends a response to an HTTP request with a 200 status code.
|
||||||
* @param res The Express Response object to send the response through.
|
* @param res The Express Response object to send the response through.
|
||||||
|
|||||||
Reference in New Issue
Block a user