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

Side by Side Diff: pkg/barback/lib/src/package_graph.dart

Issue 183813006: Use zones to capture unexpected errors in barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 barback.package_graph; 5 library barback.package_graph;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'asset_cascade.dart'; 9 import 'asset_cascade.dart';
10 import 'asset_id.dart'; 10 import 'asset_id.dart';
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 /// This is used to pipe an unexpected error from a build to the resulting 64 /// This is used to pipe an unexpected error from a build to the resulting
65 /// [Future] returned by [getAllAssets]. 65 /// [Future] returned by [getAllAssets].
66 var _lastUnexpectedError; 66 var _lastUnexpectedError;
67 67
68 /// The stack trace for [_lastUnexpectedError]. 68 /// The stack trace for [_lastUnexpectedError].
69 StackTrace _lastUnexpectedErrorTrace; 69 StackTrace _lastUnexpectedErrorTrace;
70 70
71 /// Creates a new [PackageGraph] that will transform assets in all packages 71 /// Creates a new [PackageGraph] that will transform assets in all packages
72 /// made available by [provider]. 72 /// made available by [provider].
73 PackageGraph(this.provider) { 73 PackageGraph(this.provider) {
74 for (var package in provider.packages) { 74 _inErrorZone(() {
75 var cascade = new AssetCascade(this, package); 75 for (var package in provider.packages) {
76 // The initial result for each cascade is "success" since the cascade 76 var cascade = new AssetCascade(this, package);
77 // doesn't start building until some source in that graph is updated. 77 // The initial result for each cascade is "success" since the cascade
78 _cascadeResults[package] = new BuildResult.success(); 78 // doesn't start building until some source in that graph is updated.
79 _cascades[package] = cascade; 79 _cascadeResults[package] = new BuildResult.success();
80 cascade.onDirty.listen((_) { 80 _cascades[package] = cascade;
81 _cascadeResults[package] = null; 81 cascade.onDirty.listen((_) {
82 }); 82 _cascadeResults[package] = null;
83 });
83 84
84 cascade.onLog.listen((entry) { 85 cascade.onLog.listen((entry) {
85 if (_logController.hasListener) { 86 if (_logController.hasListener) {
86 _logController.add(entry); 87 _logController.add(entry);
87 } else if (entry.level != LogLevel.FINE) { 88 } else if (entry.level != LogLevel.FINE) {
88 // No listeners, so just print entry. 89 // No listeners, so just print entry.
89 var buffer = new StringBuffer(); 90 var buffer = new StringBuffer();
90 buffer.write("[${entry.level} ${entry.transform}] "); 91 buffer.write("[${entry.level} ${entry.transform}] ");
91 92
92 if (entry.span != null) { 93 if (entry.span != null) {
93 buffer.write(entry.span.getLocationMessage(entry.message)); 94 buffer.write(entry.span.getLocationMessage(entry.message));
94 } else { 95 } else {
95 buffer.write(entry.message); 96 buffer.write(entry.message);
97 }
98
99 print(buffer);
96 } 100 }
101 });
97 102
98 print(buffer); 103 cascade.results.listen((result) {
99 } 104 _cascadeResults[cascade.package] = result;
100 }); 105 // If any cascade hasn't yet finished, the overall build isn't finishe d
Bob Nystrom 2014/03/04 23:52:40 Long line. This method is pretty big. How about b
nweiz 2014/03/05 00:11:58 Done.
106 // either.
107 if (_cascadeResults.values.any((result) => result == null)) return;
101 108
102 cascade.results.listen((result) { 109 // Include all build errors for all cascades. If no cascades have
103 _cascadeResults[cascade.package] = result; 110 // errors, the result will automatically be considered a success.
104 // If any cascade hasn't yet finished, the overall build isn't finished 111 _resultsController.add(
105 // either. 112 new BuildResult.aggregate(_cascadeResults.values));
106 if (_cascadeResults.values.any((result) => result == null)) return; 113 }, onError: (error, stackTrace) {
114 _lastUnexpectedError = error;
115 _lastUnexpectedErrorTrace = stackTrace;
116 _resultsController.addError(error, stackTrace);
117 });
118 }
107 119
108 // Include all build errors for all cascades. If no cascades have 120 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors),
109 // errors, the result will automatically be considered a success. 121 broadcast: true);
110 _resultsController.add( 122 });
111 new BuildResult.aggregate(_cascadeResults.values));
112 }, onError: (error, stackTrace) {
113 _lastUnexpectedError = error;
114 _lastUnexpectedErrorTrace = stackTrace;
115 _resultsController.addError(error, stackTrace);
116 });
117 }
118
119 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors),
120 broadcast: true);
121 } 123 }
122 124
123 /// Gets the asset node identified by [id]. 125 /// Gets the asset node identified by [id].
124 /// 126 ///
125 /// If [id] is for a generated or transformed asset, this will wait until it 127 /// If [id] is for a generated or transformed asset, this will wait until it
126 /// has been created and return it. This means that the returned asset will 128 /// has been created and return it. This means that the returned asset will
127 /// always be [AssetState.AVAILABLE]. 129 /// always be [AssetState.AVAILABLE].
128 /// 130 ///
129 /// If the asset cannot be found, returns null. 131 /// If the asset cannot be found, returns null.
130 Future<AssetNode> getAssetNode(AssetId id) { 132 Future<AssetNode> getAssetNode(AssetId id) {
131 var cascade = _cascades[id.package]; 133 return _inErrorZone(() {
132 if (cascade != null) return cascade.getAssetNode(id); 134 var cascade = _cascades[id.package];
133 return new Future.value(null); 135 if (cascade != null) return cascade.getAssetNode(id);
136 return new Future.value(null);
137 });
134 } 138 }
135 139
136 /// Gets all output assets. 140 /// Gets all output assets.
137 /// 141 ///
138 /// If a build is currently in progress, waits until it completes. The 142 /// If a build is currently in progress, waits until it completes. The
139 /// returned future will complete with an error if the build is not 143 /// returned future will complete with an error if the build is not
140 /// successful. 144 /// successful.
141 /// 145 ///
142 /// Any transforms using [LazyTransformer]s will be forced to generate 146 /// Any transforms using [LazyTransformer]s will be forced to generate
143 /// concrete outputs, and those outputs will be returned. 147 /// concrete outputs, and those outputs will be returned.
144 Future<AssetSet> getAllAssets() { 148 Future<AssetSet> getAllAssets() {
145 for (var cascade in _cascades.values) { 149 for (var cascade in _cascades.values) {
146 cascade.forceAllTransforms(); 150 _inErrorZone(() => cascade.forceAllTransforms());
147 } 151 }
148 152
149 if (_cascadeResults.values.contains(null)) { 153 if (_cascadeResults.values.contains(null)) {
150 // A build is still ongoing, so wait for it to complete and try again. 154 // A build is still ongoing, so wait for it to complete and try again.
151 return results.first.then((_) => getAllAssets()); 155 return results.first.then((_) => getAllAssets()) ;
Bob Nystrom 2014/03/04 23:52:40 Extra space.
nweiz 2014/03/05 00:11:58 Done.
152 } 156 }
153 157
154 // If an unexpected error occurred, complete with that. 158 // If an unexpected error occurred, complete with that.
155 if (_lastUnexpectedError != null) { 159 if (_lastUnexpectedError != null) {
156 var error = _lastUnexpectedError; 160 var error = _lastUnexpectedError;
157 _lastUnexpectedError = null; 161 _lastUnexpectedError = null;
158 return new Future.error(error, _lastUnexpectedErrorTrace); 162 return new Future.error(error, _lastUnexpectedErrorTrace);
159 } 163 }
160 164
161 // If the build completed with an error, complete the future with it. 165 // If the build completed with an error, complete the future with it.
(...skipping 11 matching lines...) Expand all
173 177
174 /// Adds [sources] to the graph's known set of source assets. 178 /// Adds [sources] to the graph's known set of source assets.
175 /// 179 ///
176 /// Begins applying any transforms that can consume any of the sources. If a 180 /// Begins applying any transforms that can consume any of the sources. If a
177 /// given source is already known, it is considered modified and all 181 /// given source is already known, it is considered modified and all
178 /// transforms that use it will be re-applied. 182 /// transforms that use it will be re-applied.
179 void updateSources(Iterable<AssetId> sources) { 183 void updateSources(Iterable<AssetId> sources) {
180 groupBy(sources, (id) => id.package).forEach((package, ids) { 184 groupBy(sources, (id) => id.package).forEach((package, ids) {
181 var cascade = _cascades[package]; 185 var cascade = _cascades[package];
182 if (cascade == null) throw new ArgumentError("Unknown package $package."); 186 if (cascade == null) throw new ArgumentError("Unknown package $package.");
183 cascade.updateSources(ids); 187 _inErrorZone(() => cascade.updateSources(ids));
184 }); 188 });
185 } 189 }
186 190
187 /// Removes [removed] from the graph's known set of source assets. 191 /// Removes [removed] from the graph's known set of source assets.
188 void removeSources(Iterable<AssetId> sources) { 192 void removeSources(Iterable<AssetId> sources) {
189 groupBy(sources, (id) => id.package).forEach((package, ids) { 193 groupBy(sources, (id) => id.package).forEach((package, ids) {
190 var cascade = _cascades[package]; 194 var cascade = _cascades[package];
191 if (cascade == null) throw new ArgumentError("Unknown package $package."); 195 if (cascade == null) throw new ArgumentError("Unknown package $package.");
192 cascade.removeSources(ids); 196 _inErrorZone(() => cascade.removeSources(ids));
193 }); 197 });
194 } 198 }
195 199
196 void updateTransformers(String package, 200 void updateTransformers(String package,
197 Iterable<Iterable<Transformer>> transformers) { 201 Iterable<Iterable<Transformer>> transformers) {
198 _cascades[package].updateTransformers(transformers); 202 _inErrorZone(() => _cascades[package].updateTransformers(transformers));
203 }
204
205 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to
206 /// the error channel of [results].
207 ///
208 /// [body] can return a value or a [Future] that will be piped to the returned
209 /// [Future]. If it throws a [BarbackException], that exception will be piped
210 /// to the returned [Future] as well. ANy other exceptions will be piped to
Bob Nystrom 2014/03/04 23:52:40 "ANy" -> "Any"
nweiz 2014/03/05 00:11:58 Done.
211 /// [results].
212 Future _inErrorZone(body()) {
213 var completer = new Completer.sync();
214 runZoned(() {
215 syncFuture(body).then(completer.complete).catchError((error, stackTrace) {
216 if (error is! BarbackException) throw error;
217 completer.addError(error, stackTrace);
218 });
219 }, onError: (error, stackTrace) {
220 _lastUnexpectedError = error;
221 _lastUnexpectedErrorTrace = stackTrace;
222 _resultsController.addError(error, stackTrace);
223 });
224 return completer.future;
199 } 225 }
200 } 226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698