@@ -30,6 +30,15 @@ typedef void _ChainHandler(error, Chain chain);
3030/// Since [ZoneSpecification] can't be extended or even implemented, in order to
3131/// get a real [ZoneSpecification] instance it's necessary to call [toSpec] .
3232class StackZoneSpecification {
33+ /// An opaque object used as a zone value to disable chain tracking in a given
34+ /// zone.
35+ ///
36+ /// If `Zone.current[disableKey]` is `true` , no stack chains will be tracked.
37+ static final disableKey = new Object ();
38+
39+ /// Whether chain-tracking is disabled in the current zone.
40+ bool get _disabled => Zone .current[disableKey] == true ;
41+
3342 /// The expando that associates stack chains with [StackTrace] s.
3443 ///
3544 /// The chains are associated with stack traces rather than errors themselves
@@ -54,11 +63,11 @@ class StackZoneSpecification {
5463 /// Converts [this] to a real [ZoneSpecification] .
5564 ZoneSpecification toSpec () {
5665 return new ZoneSpecification (
57- handleUncaughtError: handleUncaughtError ,
58- registerCallback: registerCallback ,
59- registerUnaryCallback: registerUnaryCallback ,
60- registerBinaryCallback: registerBinaryCallback ,
61- errorCallback: errorCallback );
66+ handleUncaughtError: _handleUncaughtError ,
67+ registerCallback: _registerCallback ,
68+ registerUnaryCallback: _registerUnaryCallback ,
69+ registerBinaryCallback: _registerBinaryCallback ,
70+ errorCallback: _errorCallback );
6271 }
6372
6473 /// Returns the current stack chain.
@@ -79,57 +88,20 @@ class StackZoneSpecification {
7988 return new _Node (trace, previous).toChain ();
8089 }
8190
82- /// Ensures that an error emitted by [future] has the correct stack
83- /// information associated with it.
84- ///
85- /// By default, the first frame of the first trace will be the line where
86- /// [trackFuture] is called. If [level] is passed, the first trace will start
87- /// that many frames up instead.
88- Future trackFuture (Future future, [int level= 0 ]) {
89- var completer = new Completer .sync ();
90- var node = _createNode (level + 1 );
91- future.then (completer.complete).catchError ((e, stackTrace) {
92- if (stackTrace == null ) stackTrace = new Trace .current ();
93- if (stackTrace is ! Chain && _chains[stackTrace] == null ) {
94- _chains[stackTrace] = node;
95- }
96- completer.completeError (e, stackTrace);
97- });
98- return completer.future;
99- }
100-
101- /// Ensures that any errors emitted by [stream] have the correct stack
102- /// information associated with them.
103- ///
104- /// By default, the first frame of the first trace will be the line where
105- /// [trackStream] is called. If [level] is passed, the first trace will start
106- /// that many frames up instead.
107- Stream trackStream (Stream stream, [int level= 0 ]) {
108- var node = _createNode (level + 1 );
109- return stream.transform (new StreamTransformer .fromHandlers (
110- handleError: (error, stackTrace, sink) {
111- if (stackTrace == null ) stackTrace = new Trace .current ();
112- if (stackTrace is ! Chain && _chains[stackTrace] == null ) {
113- _chains[stackTrace] = node;
114- }
115- sink.addError (error, stackTrace);
116- }));
117- }
118-
11991 /// Tracks the current stack chain so it can be set to [_currentChain] when
12092 /// [f] is run.
121- ZoneCallback registerCallback (Zone self, ZoneDelegate parent, Zone zone,
93+ ZoneCallback _registerCallback (Zone self, ZoneDelegate parent, Zone zone,
12294 Function f) {
123- if (f == null ) return parent.registerCallback (zone, null );
95+ if (f == null || _disabled ) return parent.registerCallback (zone, f );
12496 var node = _createNode (1 );
12597 return parent.registerCallback (zone, () => _run (f, node));
12698 }
12799
128100 /// Tracks the current stack chain so it can be set to [_currentChain] when
129101 /// [f] is run.
130- ZoneUnaryCallback registerUnaryCallback (Zone self, ZoneDelegate parent,
102+ ZoneUnaryCallback _registerUnaryCallback (Zone self, ZoneDelegate parent,
131103 Zone zone, Function f) {
132- if (f == null ) return parent.registerUnaryCallback (zone, null );
104+ if (f == null || _disabled ) return parent.registerUnaryCallback (zone, f );
133105 var node = _createNode (1 );
134106 return parent.registerUnaryCallback (zone, (arg) {
135107 return _run (() => f (arg), node);
@@ -138,9 +110,10 @@ class StackZoneSpecification {
138110
139111 /// Tracks the current stack chain so it can be set to [_currentChain] when
140112 /// [f] is run.
141- ZoneBinaryCallback registerBinaryCallback (Zone self, ZoneDelegate parent,
113+ ZoneBinaryCallback _registerBinaryCallback (Zone self, ZoneDelegate parent,
142114 Zone zone, Function f) {
143- if (f == null ) return parent.registerBinaryCallback (zone, null );
115+ if (f == null || _disabled) return parent.registerBinaryCallback (zone, f);
116+
144117 var node = _createNode (1 );
145118 return parent.registerBinaryCallback (zone, (arg1, arg2) {
146119 return _run (() => f (arg1, arg2), node);
@@ -149,8 +122,12 @@ class StackZoneSpecification {
149122
150123 /// Looks up the chain associated with [stackTrace] and passes it either to
151124 /// [_onError] or [parent] 's error handler.
152- handleUncaughtError (Zone self, ZoneDelegate parent, Zone zone, error,
125+ _handleUncaughtError (Zone self, ZoneDelegate parent, Zone zone, error,
153126 StackTrace stackTrace) {
127+ if (_disabled) {
128+ return parent.handleUncaughtError (zone, error, stackTrace);
129+ }
130+
154131 var stackChain = chainFor (stackTrace);
155132 if (_onError == null ) {
156133 return parent.handleUncaughtError (zone, error, stackChain);
@@ -171,8 +148,10 @@ class StackZoneSpecification {
171148
172149 /// Attaches the current stack chain to [stackTrace] , replacing it if
173150 /// necessary.
174- AsyncError errorCallback (Zone self, ZoneDelegate parent, Zone zone,
151+ AsyncError _errorCallback (Zone self, ZoneDelegate parent, Zone zone,
175152 Object error, StackTrace stackTrace) {
153+ if (_disabled) return parent.errorCallback (zone, error, stackTrace);
154+
176155 // Go up two levels to get through [_CustomZone.errorCallback].
177156 if (stackTrace == null ) {
178157 stackTrace = _createNode (2 ).toChain ();
0 commit comments