roll-parser - v3.4.0
    Preparing search index...

    Function isRollParserError

    • Type guard for roll-parser errors. Checks instanceof first, then a brand carried on the error's prototype — so it still matches when instanceof cannot, namely an error from an iframe or vm context, or from a second copy of the library in node_modules at this version or newer.

      Use it as the outer filter in every catch: anything it rejects came from somewhere else and should be rethrown. The brand is what makes that sound — a foreign error is never accepted just for carrying a code that happens to collide with one of ours.

      What it answers is "did this come from us", not "whose fault was it". A true covers bad notation, a bad options object, and a broken invariant in here alike, so it is the wrong test to hang a user-facing message on. Reach for isNotationError for that.

      Only this library's own error prototype carries the brand, so holding it is proof of origin, and the code is trusted rather than re-validated: an error from a newer minor passes with a code this build has never heard of, which is what RollParserErrorCode being an open union already implies. A value that forges the brand is out of scope, as it is for any brand check.

      The one boundary it cannot cross is a worker. postMessage and structuredClone rebuild an Error from message and stack alone, discarding code, name, and the prototype with it, so the value that arrives is no longer recognizable as anything. Send error.code yourself as part of the message payload if the other side needs it.

      Parameters

      • value: unknown

        The caught value, of unknown type

      Returns value is RollParserError

      true when value is a roll-parser error

      import { isRollParserError, roll } from 'roll-parser';

      try {
      roll('2d6+&');
      } catch (error) {
      if (!isRollParserError(error)) throw error;
      error.code; // 'UNEXPECTED_CHARACTER'
      error.message; // "Unexpected character: '&'"
      }