Skip to content

Instantly share code, notes, and snippets.

@blaswan
Created June 25, 2018 00:32
Show Gist options
  • Select an option

  • Save blaswan/6b11a5ac0dca81485b8587e978bc88a4 to your computer and use it in GitHub Desktop.

Select an option

Save blaswan/6b11a5ac0dca81485b8587e978bc88a4 to your computer and use it in GitHub Desktop.

Revisions

  1. blaswan created this gist Jun 25, 2018.
    18 changes: 18 additions & 0 deletions assert.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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");