Throw errors instead of returning them

This commit is contained in:
Matthew Oslan
2019-09-02 11:32:38 -04:00
parent 2918527bcb
commit 1a1ecafcb0
3 changed files with 19 additions and 18 deletions

View File

@@ -1,6 +1,5 @@
import { BaseWateringData, GeoCoordinates, PWS } from "../../types";
import { WeatherProvider } from "../weatherProviders/WeatherProvider";
import { ErrorCode } from "../../errors";
export interface AdjustmentMethod {
@@ -44,8 +43,6 @@ export interface AdjustmentMethodResponse {
* watering.
*/
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. */
wateringData: BaseWateringData;
}

View File

@@ -2,7 +2,7 @@ import { AdjustmentMethod, AdjustmentMethodResponse, AdjustmentOptions } from ".
import { GeoCoordinates, PWS, ZimmermanWateringData } from "../../types";
import { validateValues } from "../weather";
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
if ( !validateValues( [ "temp", "humidity", "precip" ], wateringData ) ) {
// Default to a scale of 100% if fields are missing.
return {
scale: 100,
rawData: rawData,
errCode: ErrorCode.MissingWeatherField,
wateringData: wateringData
};
throw new CodedError( ErrorCode.MissingWeatherField );
}
let humidityBase = 30, tempBase = 70, precipBase = 0;

View File

@@ -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 } );
sendWateringError( res, new CodedError( 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 } );
sendWateringError( res, new CodedError( 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 } );
sendWateringError( res, codedError );
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 } );
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsId ) );
return;
}
if ( !apiKey ) {
sendWateringData( res, { errCode: ErrorCode.InvalidPwsApiKey } );
sendWateringError( res, new CodedError( ErrorCode.InvalidPwsApiKey ) );
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 );
}
sendWateringData( res, { errCode: codedError.errCode } );
sendWateringError( res, codedError );
return;
}
data.scale = adjustmentMethodResponse.scale;
data.errCode = adjustmentMethodResponse.errCode || 0;
data.rd = adjustmentMethodResponse.rainDelay;
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 );
}
sendWateringData( res, { errCode: codedError.errCode } );
sendWateringError( res, codedError );
return;
}
}
@@ -333,6 +332,16 @@ export const getWateringData = async function( req: express.Request, res: expres
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.
* @param res The Express Response object to send the response through.