toString method - FormatException class - dart:core library (original) (raw)

String toString()

override

Returns a description of the format exception.

The description always contains the message.

If source is present and is a string, the description will contain (at least a part of) the source. If offset is also provided, the part of the source included will contain that offset, and the offset will be marked.

If the source is a string and it contains a line break before offset, only the line containing offset will be included, and its line number will also be part of the description. Line and character offsets are 1-based.

Implementation

String toString() {
  String report = "FormatException";
  Object? message = this.message;
  if (message != null && "" != message) {
    report = "$report: $message";
  }
  int? offset = this.offset;
  Object? source = this.source;
  if (source is String) {
    if (offset != null && (offset < 0 || offset > source.length)) {
      offset = null;
    }
    // Source is string and offset is null or valid.
    if (offset == null) {
      if (source.length > 78) {
        source = source.substring(0, 75) + "...";
      }
      return "$report\n$source";
    }
    int lineNum = 1;
    int lineStart = 0;
    bool previousCharWasCR = false;
    for (int i = 0; i < offset; i++) {
      int char = source.codeUnitAt(i);
      if (char == 0x0a) {
        if (lineStart != i || !previousCharWasCR) {
          lineNum++;
        }
        lineStart = i + 1;
        previousCharWasCR = false;
      } else if (char == 0x0d) {
        lineNum++;
        lineStart = i + 1;
        previousCharWasCR = true;
      }
    }
    if (lineNum > 1) {
      report += " (at line <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>i</mi><mi>n</mi><mi>e</mi><mi>N</mi><mi>u</mi><mi>m</mi><mo separator="true">,</mo><mi>c</mi><mi>h</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>c</mi><mi>t</mi><mi>e</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">lineNum, character </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">in</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.10903em;">N</span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">a</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span></span></span></span>{offset - lineStart + 1})\n";
    } else {
      report += " (at character ${offset + 1})\n";
    }
    int lineEnd = source.length;
    for (int i = offset; i < source.length; i++) {
      int char = source.codeUnitAt(i);
      if (char == 0x0a || char == 0x0d) {
        lineEnd = i;
        break;
      }
    }
    int length = lineEnd - lineStart;
    int start = lineStart;
    int end = lineEnd;
    String prefix = "";
    String postfix = "";
    if (length > 78) {
      // Can't show entire line. Try to anchor at the nearest end, if
      // one is within reach.
      int index = offset - lineStart;
      if (index < 75) {
        end = start + 75;
        postfix = "...";
      } else if (end - offset < 75) {
        start = end - 75;
        prefix = "...";
      } else {
        // Neither end is near, just pick an area around the offset.
        start = offset - 36;
        end = offset + 36;
        prefix = postfix = "...";
      }
    }
    String slice = source.substring(start, end);
    int markOffset = offset - start + prefix.length;
    return "$report$prefix$slice$postfix\n${" " * markOffset}^\n";
  } else {
    // The source is not a string.
    if (offset != null) {
      report += " (at offset $offset)";
    }
    return report;
  }
}