Created
June 25, 2018 00:32
-
-
Save blaswan/6b11a5ac0dca81485b8587e978bc88a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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