Last active
January 11, 2019 17:00
-
-
Save codeBelt/277e59c6a760138f57eeb7be04018815 to your computer and use it in GitHub Desktop.
Error Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeError = (obj: any) => | |
new Error(`check()'s first argument must be a boolean but argument was of type ${typeof obj}`); | |
const isNotTypeBoolean = (arg: any) => typeof arg !== 'boolean'; | |
/** | |
* Helper function for throwing errors if a given expression evaluates to false. | |
* This function is strict and will throw an error the the type of the first | |
* argument is not "boolean". | |
*/ | |
export const check = (predicate: boolean, error: Error) => { | |
if (isNotTypeBoolean(predicate)) { | |
throw typeError(predicate); | |
} else if (predicate) { | |
return; | |
} | |
throw error; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const errorMissingId: Error = new Error('A non-empty \'id\' must be provided in the request body'); | |
check(id != null, errorMissingId); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment