Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: pkg/stack_trace/lib/src/chain.dart

Issue 75863004: Add a stack chain class to the stack trace package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/pkg.status ('k') | pkg/stack_trace/lib/src/stack_zone_specification.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 before each asynchronous callback is run.
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 /// Note that even if [onError] isn't passed, this zone will still be an error
72 /// zone. This means that any errors that would cross the zone boundary are
73 /// considered unhandled.
74 ///
75 /// If [callback] returns a value, it will be returned by [capture] as well.
76 ///
77 /// Currently, capturing stack chains doesn't work when using dart2js due to
78 /// issues [15171] and [15105]. Stack chains reported on dart2js will contain
79 /// only one trace.
80 ///
81 /// [15171]: https://code.google.com/p/dart/issues/detail?id=15171
82 /// [15105]: https://code.google.com/p/dart/issues/detail?id=15105
83 static capture(callback(), {ChainHandler onError}) {
84 var spec = new StackZoneSpecification(onError);
85 return runZoned(callback, zoneSpecification: spec.toSpec(), zoneValues: {
86 #stack_trace.stack_zone.spec: spec
87 });
88 }
89
90 /// Ensures that any errors emitted by [futureOrStream] have the correct stack
91 /// chain information associated with them.
92 ///
93 /// For the most part an error thrown within a [capture] zone will have the
94 /// correct stack chain automatically associated with it. However, there are
95 /// some cases where exceptions won't be automatically detected: any [Future]
96 /// constructor, [Completer.completeError], [Stream.addError], and libraries
97 /// that use these.
98 ///
99 /// This returns a [Future] or [Stream] that will emit the same values and
100 /// errors as [futureOrStream]. The only exception is that if [futureOrStream]
101 /// emits an error without a stack trace, one will be added in the return
102 /// value.
103 ///
104 /// If this is called outside of a [capture] zone, it just returns
105 /// [futureOrStream] as-is.
106 ///
107 /// As the name suggests, [futureOrStream] may be either a [Future] or a
108 /// [Stream].
109 static track(futureOrStream) {
110 if (_currentSpec == null) return futureOrStream;
111 if (futureOrStream is Future) {
112 return _currentSpec.trackFuture(futureOrStream, 1);
113 } else {
114 return _currentSpec.trackStream(futureOrStream, 1);
115 }
116 }
117
118 /// Returns the current stack chain.
119 ///
120 /// By default, the first frame of the first trace will be the line where
121 /// [Chain.current] is called. If [level] is passed, the first trace will
122 /// start that many frames up instead.
123 ///
124 /// If this is called outside of a [capture] zone, it just returns a
125 /// single-trace chain.
126 factory Chain.current([int level=0]) {
127 if (_currentSpec != null) return _currentSpec.currentChain(level + 1);
128 return new Chain([new Trace.current(level + 1)]);
129 }
130
131 /// Returns the stack chain associated with [trace].
132 ///
133 /// The first stack trace in the returned chain will always be [trace]
134 /// (converted to a [Trace] if necessary). If there is no chain associated
135 /// with [trace] or if this is called outside of a [capture] zone, this just
136 /// returns a single-trace chain containing [trace].
137 ///
138 /// If [trace] is already a [Chain], it will be returned as-is.
139 factory Chain.forTrace(StackTrace trace) {
140 if (trace is Chain) return trace;
141 if (_currentSpec == null) return new Chain([new Trace.from(trace)]);
142 return _currentSpec.chainFor(trace);
143 }
144
145 /// Parses a string representation of a stack chain.
146 ///
147 /// Specifically, this parses the output of [Chain.toString].
148 factory Chain.parse(String chain) =>
149 new Chain(chain.split(_GAP).map((trace) => new Trace.parseFriendly(trace)));
150
151 /// Returns a new [Chain] comprised of [traces].
152 Chain(Iterable<Trace> traces)
153 : traces = new UnmodifiableListView<Trace>(traces.toList());
154
155 /// Returns a terser version of [this].
156 ///
157 /// This calls [Trace.terse] on every trace in [traces], and discards any
158 /// trace that contain only internal frames.
159 Chain get terse {
160 return new Chain(traces.map((trace) => trace.terse).where((trace) {
161 // Ignore traces that contain only internal processing.
162 return trace.frames.length > 1;
163 }));
164 }
165
166 /// Converts [this] to a [Trace].
167 ///
168 /// The trace version of a chain is just the concatenation of all the traces
169 /// in the chain.
170 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames)));
171
172 String toString() => traces.join(_GAP);
173 }
OLDNEW
« no previous file with comments | « pkg/pkg.status ('k') | pkg/stack_trace/lib/src/stack_zone_specification.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698