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

Unified Diff: lib/src/chain.dart

Issue 1583483003: Add a "when" parameter to Chain.capture(). (Closed) Base URL: git@github.com:dart-lang/stack_trace@master
Patch Set: Created 4 years, 11 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/chain.dart
diff --git a/lib/src/chain.dart b/lib/src/chain.dart
index 4baec216c8757a5de7f74c4ec3be105636aad78d..3f35c16019672ed02d086cc5cf527ea543d837a3 100644
--- a/lib/src/chain.dart
+++ b/lib/src/chain.dart
@@ -49,8 +49,14 @@ class Chain implements StackTrace {
static StackZoneSpecification get _currentSpec =>
Zone.current[#stack_trace.stack_zone.spec];
- /// Runs [callback] in a [Zone] in which the current stack chain is tracked
- /// and automatically associated with (most) errors.
+ /// If [when] is `true`, runs [callback] in a [Zone] in which the current
+ /// stack chain is tracked and automatically associated with (most) errors.
+ ///
+ /// If [when] is `false`, this does not track stack chains. Instead, it's
+ /// identical to [runZoned], except that it wraps any errors in [new
+ /// Chain.forTrace]—which will only wrap the trace unless there's a different
+ /// [Chain.capture] active. This makes it easy for the caller to only capture
+ /// stack chains in debug mode or during development.
///
/// If [onError] is passed, any error in the zone that would otherwise go
/// unhandled is passed to it, along with the [Chain] associated with that
@@ -64,7 +70,19 @@ class Chain implements StackTrace {
/// considered unhandled.
///
/// If [callback] returns a value, it will be returned by [capture] as well.
- static capture(callback(), {void onError(error, Chain chain)}) {
+ static capture(callback(), {void onError(error, Chain chain),
+ bool when: true}) {
+ if (!when) {
+ var newOnError;
+ if (onError != null) {
+ newOnError = (error, stackTrace) {
+ onError(error, new Chain.forTrace(stackTrace));
+ };
+ }
+
+ return runZoned(callback, onError: newOnError);
+ }
+
var spec = new StackZoneSpecification(onError);
return runZoned(() {
try {
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698