foldFrames method

Chain foldFrames(
  1. bool predicate(
    1. Frame
    ), {
  2. bool terse = false,
})

Returns a new Chain based on this chain where multiple stack frames matching predicate are folded together.

This means that whenever there are multiple frames in a row that match predicate, only the last one is kept. In addition, traces that are composed entirely of frames matching predicate are omitted.

This is useful for limiting the amount of library code that appears in a stack trace by only showing user code and code that's called by user code.

If terse is true, this will also fold together frames from the core library or from this package, and simplify core library frames as in Trace.terse.

Implementation

Chain foldFrames(bool Function(Frame) predicate, {bool terse = false}) {
  var foldedTraces =
      traces.map((trace) => trace.foldFrames(predicate, terse: terse));
  var nonEmptyTraces = foldedTraces.where((trace) {
    // Ignore traces that contain only folded frames.
    if (trace.frames.length > 1) return true;
    if (trace.frames.isEmpty) return false;

    // In terse mode, the trace may have removed an outer folded frame,
    // leaving a single non-folded frame. We can detect a folded frame because
    // it has no line information.
    if (!terse) return false;
    return trace.frames.single.line != null;
  });

  // If all the traces contain only internal processing, preserve the last
  // (top-most) one so that the chain isn't empty.
  if (nonEmptyTraces.isEmpty && foldedTraces.isNotEmpty) {
    return Chain([foldedTraces.last]);
  }

  return Chain(nonEmptyTraces);
}