Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library stack_trace.chain; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection'; | |
| 9 | |
| 10 import 'stack_zone_specification.dart'; | |
| 11 import 'trace.dart'; | |
| 12 import 'utils.dart'; | |
| 13 | |
| 14 /// A function that handles errors in the zone wrapped by [Chain.capture]. | |
| 15 typedef void ChainHandler(error, Chain chain); | |
| 16 | |
| 17 /// A chain of stack traces. | |
| 18 /// | |
| 19 /// A stack chain is a collection of one or more stack traces that collectively | |
| 20 /// represent the path from [main] through nested function calls to a particular | |
| 21 /// code location, usually where an error was thrown. Multiple stack traces are | |
| 22 /// necessary when using asynchronous functions, since the program's stack is | |
| 23 /// reset as each asynchronous callback is run. | |
|
Bob Nystrom
2013/11/20 20:19:53
"reset" -> "unwound", "run" -> "registered".
nweiz
2013/11/20 21:30:12
I think my wording is correct here. Registering a
| |
| 24 /// | |
| 25 /// Stack chains can be automatically tracked using [Chain.capture]. This sets | |
| 26 /// up a new [Zone] in which the current stack chain is tracked and can be | |
| 27 /// accessed using [new Chain.current]. Any errors that would be top-leveled in | |
| 28 /// the zone can be handled, along with their associated chains, with the | |
| 29 /// `onError` callback. | |
| 30 /// | |
| 31 /// For the most part [Chain.capture] will notice when an error is thrown and | |
| 32 /// associate the correct stack chain with it; the chain can be accessed using | |
| 33 /// [new Chain.forTrace]. However, there are some cases where exceptions won't | |
| 34 /// be automatically detected: any [Future] constructor, | |
| 35 /// [Completer.completeError], [Stream.addError], and libraries that use these. | |
| 36 /// For these, all you need to do is wrap the Future or Stream in a call to | |
| 37 /// [Chain.track] and the errors will be tracked correctly. | |
| 38 class Chain implements StackTrace { | |
| 39 /// The line used in the string representation of stack chains to represent | |
| 40 /// the gap between traces. | |
| 41 static const _GAP = '===== asynchronous gap ===========================\n'; | |
| 42 | |
| 43 /// The stack traces that make up this chain. | |
| 44 /// | |
| 45 /// Like the frames in a stack trace, the traces are ordered from most local | |
| 46 /// to least local. The first one is the trace where the actual exception was | |
| 47 /// raised, the second one is where that callback was scheduled, and so on. | |
| 48 final List<Trace> traces; | |
| 49 | |
| 50 /// The [StackZoneSpecification] for the current zone. | |
| 51 static StackZoneSpecification get _currentSpec => | |
| 52 Zone.current[#stack_trace.stack_zone.spec]; | |
| 53 | |
| 54 /// Runs [callback] in a [Zone] in which the current stack chain is tracked | |
| 55 /// and automatically associated with (most) errors. | |
| 56 /// | |
| 57 /// If [onError] is passed, any error in the zone that would otherwise go | |
| 58 /// unhandled is passed to it, along with the [Chain] associated with that | |
| 59 /// error. Note that if [callback] produces multiple unhandled errors, | |
| 60 /// [onError] may be called more than once. If [onError] isn't passed, the | |
| 61 /// parent Zone's `unhandledErrorHandler` will be called with the error and | |
| 62 /// its chain. | |
| 63 /// | |
| 64 /// For the most part an error thrown in the zone will have the correct stack | |
| 65 /// chain associated with it. However, there are some cases where exceptions | |
| 66 /// won't be automatically detected: any [Future] constructor, | |
| 67 /// [Completer.completeError], [Stream.addError], and libraries that use | |
| 68 /// these. For these, all you need to do is wrap the Future or Stream in a | |
| 69 /// call to [Chain.track] and the errors will be tracked correctly. | |
| 70 /// | |
| 71 /// If [callback] returns a value, it will be returned by [capture] as well. | |
| 72 /// | |
| 73 /// Currently, capturing stack chains doesn't work when using dart2js due to | |
| 74 /// issues 15171 and 15105. Stack chains reported on dart2js will contain only | |
|
Bob Nystrom
2013/11/20 20:19:53
Since this is in a doc comment, link to these bugs
nweiz
2013/11/20 21:30:12
Done.
| |
| 75 /// one trace. | |
| 76 static capture(callback(), {ChainHandler onError}) { | |
| 77 var spec = new StackZoneSpecification(onError); | |
| 78 return runZoned(callback, zoneSpecification: spec.toSpec(), zoneValues: { | |
| 79 #stack_trace.stack_zone.spec: spec | |
| 80 }); | |
| 81 } | |
| 82 | |
| 83 /// Ensures that any errors emitted by [futureOrStream] have the correct stack | |
| 84 /// chain information associated with them. | |
| 85 /// | |
| 86 /// For the most part an error thrown within a [capture] zone will have the | |
| 87 /// correct stack chain automatically associated with it. However, there are | |
| 88 /// some cases where exceptions won't be automatically detected: any [Future] | |
| 89 /// constructor, [Completer.completeError], [Stream.addError], and libraries | |
| 90 /// that use these. | |
| 91 /// | |
| 92 /// This returns a [Future] or [Stream] that will emit the same values and | |
| 93 /// errors as [futureOrStream]. The only exception is that if [futureOrStream] | |
| 94 /// emits an error without a stack trace, one will be added in the return | |
| 95 /// value. | |
| 96 /// | |
| 97 /// If this is called outside of a [capture] zone, it just returns | |
| 98 /// [futureOrStream] as-is. | |
| 99 /// | |
| 100 /// As the name suggests, [futureOrStream] may be either a [Future] or a | |
| 101 /// [Stream]. | |
| 102 static track(futureOrStream) { | |
| 103 if (_currentSpec == null) return futureOrStream; | |
| 104 if (futureOrStream is Future) { | |
| 105 return _currentSpec.trackFuture(futureOrStream, 1); | |
| 106 } else { | |
| 107 return _currentSpec.trackStream(futureOrStream, 1); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 /// Returns the current stack chain. | |
| 112 /// | |
| 113 /// By default, the first frame of the first trace will be the line where | |
| 114 /// [Chain.current] is called. If [level] is passed, the first trace will | |
| 115 /// start that many frames up instead. | |
| 116 /// | |
| 117 /// If this is called outside of a [capture] zone, it just returns a | |
| 118 /// single-trace chain. | |
| 119 factory Chain.current([int level=0]) { | |
| 120 if (_currentSpec != null) return _currentSpec.currentChain(level + 1); | |
| 121 return new Chain([new Trace.current(level + 1)]); | |
| 122 } | |
| 123 | |
| 124 /// Returns the stack chain associated with [trace]. | |
| 125 /// | |
| 126 /// The first stack trace in the returned chain will always be [trace] | |
| 127 /// (converted to a [Trace] if necessary). If there is no chain associated | |
| 128 /// with [trace] or if this is called outside of a [capture] zone, this just | |
| 129 /// returns a single-trace chain containing [trace]. | |
| 130 factory Chain.forTrace(StackTrace trace) { | |
| 131 if (_currentSpec == null) return new Chain([new Trace.from(trace)]); | |
| 132 return _currentSpec.chainFor(trace); | |
| 133 } | |
| 134 | |
| 135 /// Parses a string representation of a stack chain. | |
| 136 /// | |
| 137 /// Specifically, this parses the output of [Chain.toString]. | |
| 138 factory Chain.parse(String chain) => | |
| 139 new Chain(chain.split(_GAP).map((trace) => new Trace.parseFriendly(trace))); | |
| 140 | |
| 141 /// Returns a new [Chain] comprised of [traces]. | |
| 142 Chain(Iterable<Trace> traces) | |
| 143 : traces = new UnmodifiableListView<Trace>(traces.toList()); | |
| 144 | |
| 145 /// Returns a terser version of [this]. | |
| 146 /// | |
| 147 /// This calls [Trace.terse] on every trace in [traces], and discards any | |
| 148 /// trace that contain only internal frames. | |
| 149 Chain get terse { | |
| 150 return new Chain(traces.map((trace) => trace.terse).where((trace) { | |
| 151 // Ignore traces that contain only internal processing. | |
| 152 return trace.frames.length > 1; | |
| 153 })); | |
| 154 } | |
| 155 | |
| 156 /// Converts [this] to a [Trace]. | |
| 157 /// | |
| 158 /// The trace version of a chain is just the concatenation of all the traces | |
| 159 /// in the chain. | |
| 160 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames))); | |
| 161 | |
| 162 String toString() => traces.join(_GAP); | |
| 163 } | |
| OLD | NEW |