Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active January 11, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeBelt/277e59c6a760138f57eeb7be04018815 to your computer and use it in GitHub Desktop.
Save codeBelt/277e59c6a760138f57eeb7be04018815 to your computer and use it in GitHub Desktop.
Error Checker
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;
};
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