Tidy up unexpected error handling
This commit is contained in:
@@ -55,11 +55,11 @@ export enum ErrorCode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** An error with a numeric code that can be used to identify the type of error. */
|
/** An error with a numeric code that can be used to identify the type of error. */
|
||||||
export class CodedError {
|
export class CodedError extends Error {
|
||||||
public readonly errCode: ErrorCode;
|
public readonly errCode: ErrorCode;
|
||||||
public readonly message: string;
|
|
||||||
|
|
||||||
public constructor( errCode: ErrorCode ) {
|
public constructor( errCode: ErrorCode, message?: string ) {
|
||||||
|
super( message );
|
||||||
this.errCode = errCode;
|
this.errCode = errCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,12 +219,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
try {
|
try {
|
||||||
coordinates = await resolveCoordinates( location );
|
coordinates = await resolveCoordinates( location );
|
||||||
} catch ( err ) {
|
} catch ( err ) {
|
||||||
let codedError: CodedError = makeCodedError( err );
|
sendWateringError( res, makeCodedError( err ) );
|
||||||
if ( codedError.errCode === ErrorCode.UnexpectedError ) {
|
|
||||||
console.error( `An unexpected error occurred during location resolution for "${ req.url }": `, err );
|
|
||||||
}
|
|
||||||
|
|
||||||
sendWateringError( res, codedError );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,12 +278,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
adjustmentOptions, coordinates, weatherProvider, pws
|
adjustmentOptions, coordinates, weatherProvider, pws
|
||||||
);
|
);
|
||||||
} catch ( err ) {
|
} catch ( err ) {
|
||||||
let codedError: CodedError = makeCodedError( err );
|
sendWateringError( res, makeCodedError( err ) );
|
||||||
if ( codedError.errCode === ErrorCode.UnexpectedError ) {
|
|
||||||
console.error( `An unexpected error occurred during watering scale calculation for "${ req.url }": `, err );
|
|
||||||
}
|
|
||||||
|
|
||||||
sendWateringError( res, codedError );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,12 +293,7 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
try {
|
try {
|
||||||
wateringData = await weatherProvider.getWateringData( coordinates );
|
wateringData = await weatherProvider.getWateringData( coordinates );
|
||||||
} catch ( err ) {
|
} catch ( err ) {
|
||||||
let codedError: CodedError = makeCodedError( err );
|
sendWateringError( res, makeCodedError( err ) );
|
||||||
if ( codedError.errCode === ErrorCode.UnexpectedError ) {
|
|
||||||
console.error( `An unexpected error occurred during restriction checks for "${ req.url }": `, err );
|
|
||||||
}
|
|
||||||
|
|
||||||
sendWateringError( res, codedError );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,6 +324,10 @@ export const getWateringData = async function( req: express.Request, res: expres
|
|||||||
* @param useJson Indicates if the response body should use a JSON format instead of a format similar to URL query strings.
|
* @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 ) {
|
function sendWateringError( res: express.Response, error: CodedError, useJson: boolean = false ) {
|
||||||
|
if ( error.errCode === ErrorCode.UnexpectedError ) {
|
||||||
|
console.error( `An unexpected error occurred:`, error );
|
||||||
|
}
|
||||||
|
|
||||||
sendWateringData( res, { errCode: error.errCode, scale: 100 } );
|
sendWateringData( res, { errCode: error.errCode, scale: 100 } );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user