Skip to content

Instantly share code, notes, and snippets.

@blaswan
Created June 25, 2018 00:32
Show Gist options
  • Save blaswan/6b11a5ac0dca81485b8587e978bc88a4 to your computer and use it in GitHub Desktop.
Save blaswan/6b11a5ac0dca81485b8587e978bc88a4 to your computer and use it in GitHub Desktop.
export const assert = (condition: boolean, messageOrError?: string | Error) => {
if (!condition) {
if (typeof messageOrError === "string" || typeof messageOrError === "undefined") {
const message = messageOrError || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
} else {
throw messageOrError;
}
}
};
// pass specific error
// assert(url, new ValidationError("Required value missing: url"));
// or pass only message
// assert(url, "Required value missing: url");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment