| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library stack_trace.chain; | 5 library stack_trace.chain; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'frame.dart'; | 10 import 'frame.dart'; |
| 11 import 'stack_zone_specification.dart'; | 11 import 'stack_zone_specification.dart'; |
| 12 import 'trace.dart'; | 12 import 'trace.dart'; |
| 13 import 'utils.dart'; | 13 import 'utils.dart'; |
| 14 | 14 |
| 15 /// A function that handles errors in the zone wrapped by [Chain.capture]. | 15 /// A function that handles errors in the zone wrapped by [Chain.capture]. |
| 16 typedef void ChainHandler(error, Chain chain); | 16 typedef void ChainHandler(error, Chain chain); |
| 17 | 17 |
| 18 /// A chain of stack traces. | 18 /// A chain of stack traces. |
| 19 /// | 19 /// |
| 20 /// A stack chain is a collection of one or more stack traces that collectively | 20 /// A stack chain is a collection of one or more stack traces that collectively |
| 21 /// represent the path from [main] through nested function calls to a particular | 21 /// represent the path from [main] through nested function calls to a particular |
| 22 /// code location, usually where an error was thrown. Multiple stack traces are | 22 /// code location, usually where an error was thrown. Multiple stack traces are |
| 23 /// necessary when using asynchronous functions, since the program's stack is | 23 /// necessary when using asynchronous functions, since the program's stack is |
| 24 /// reset before each asynchronous callback is run. | 24 /// reset before each asynchronous callback is run. |
| 25 /// | 25 /// |
| 26 /// Stack chains can be automatically tracked using [Chain.capture]. This sets | 26 /// Stack chains can be automatically tracked using [Chain.capture]. This sets |
| 27 /// up a new [Zone] in which the current stack chain is tracked and can be | 27 /// up a new [Zone] in which the current stack chain is tracked and can be |
| 28 /// accessed using [new Chain.current]. Any errors that would be top-leveled in | 28 /// accessed using [new Chain.current]. Any errors that would be top-leveled in |
| 29 /// the zone can be handled, along with their associated chains, with the | 29 /// the zone can be handled, along with their associated chains, with the |
| 30 /// `onError` callback. | 30 /// `onError` callback. For example: |
| 31 /// |
| 32 /// Chain.capture(() { |
| 33 /// // ... |
| 34 /// }, onError: (error, stackChain) { |
| 35 /// print("Caught error $error\n" |
| 36 /// "$stackChain"); |
| 37 /// }); |
| 31 /// | 38 /// |
| 32 /// For the most part [Chain.capture] will notice when an error is thrown and | 39 /// For the most part [Chain.capture] will notice when an error is thrown and |
| 33 /// associate the correct stack chain with it; the chain can be accessed using | 40 /// associate the correct stack chain with it; the chain can be accessed using |
| 34 /// [new Chain.forTrace]. However, there are some cases where exceptions won't | 41 /// [new Chain.forTrace]. However, there are some cases where exceptions won't |
| 35 /// be automatically detected: any [Future] constructor, | 42 /// be automatically detected: any [Future] constructor, |
| 36 /// [Completer.completeError], [Stream.addError], and libraries that use these. | 43 /// [Completer.completeError], [Stream.addError], and libraries that use these. |
| 37 /// For these, all you need to do is wrap the Future or Stream in a call to | 44 /// For these, all you need to do is wrap the Future or Stream in a call to |
| 38 /// [Chain.track] and the errors will be tracked correctly. | 45 /// [Chain.track] and the errors will be tracked correctly. |
| 39 class Chain implements StackTrace { | 46 class Chain implements StackTrace { |
| 40 /// The line used in the string representation of stack chains to represent | 47 /// The line used in the string representation of stack chains to represent |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 207 } |
| 201 | 208 |
| 202 /// Converts [this] to a [Trace]. | 209 /// Converts [this] to a [Trace]. |
| 203 /// | 210 /// |
| 204 /// The trace version of a chain is just the concatenation of all the traces | 211 /// The trace version of a chain is just the concatenation of all the traces |
| 205 /// in the chain. | 212 /// in the chain. |
| 206 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames))); | 213 Trace toTrace() => new Trace(flatten(traces.map((trace) => trace.frames))); |
| 207 | 214 |
| 208 String toString() => traces.join(_GAP); | 215 String toString() => traces.join(_GAP); |
| 209 } | 216 } |
| OLD | NEW |