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

    Function getErrorSpan

    • Normalizes the three error position shapes into one span.

      LexerError and ParseError expose a single position; EvaluatorError exposes start/end. Returns undefined for errors that are not roll-parser errors, or that carry no usable offset (an EvaluatorError raised on a hand-built AST, for instance).

      Parameters

      • error: unknown

        The caught value, of unknown type

      Returns ErrorSpan | undefined

      The span, or undefined when none is available

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

      function explain(notation: string): string | undefined {
      try {
      roll(notation);
      return undefined;
      } catch (error) {
      if (!isRollParserError(error)) throw error;
      const span = getErrorSpan(error);
      if (span == null) return error.message;
      const width = (span.end ?? span.start + 1) - span.start;
      return [
      error.message,
      notation,
      ' '.repeat(span.start) + '^'.repeat(width),
      ].join('\n');
      }
      }

      explain('2d6+&');
      // Unexpected character: '&'
      // 2d6+&
      // ^

      explain('2d6+1d0+3');
      // Invalid dice sides: 0
      // 2d6+1d0+3
      // ^^^