sanitizeStacktrace function

String sanitizeStacktrace(
  1. dynamic st, {
  2. bool shorten = true,
})

Sanitize a stacktrace. This will shorten file paths in order to remove any PII that may be contained in the full file path. For example, this will shorten file:///Users/foobar/tmp/error.dart to error.dart.

If shorten is true, this method will also attempt to compress the text of the stacktrace. GA has a 100 char limit on the text that can be sent for an exception. This will try and make those first 100 chars contain information useful to debugging the issue.

Implementation

String sanitizeStacktrace(dynamic st, {bool shorten = true}) {
  var str = '$st';

  Iterable<Match> iter = _pathRegex.allMatches(str);
  iter = iter.toList().reversed;

  for (var match in iter) {
    var replacement = match.group(1)!;
    str =
        str.substring(0, match.start) + replacement + str.substring(match.end);
  }

  if (shorten) {
    // Shorten the stacktrace up a bit.
    str = str.replaceAll(_tabOrSpaceRegex, ' ');
  }

  return str;
}