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

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: code review 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
« no previous file with comments | « pkg/barback/lib/src/asset_cascade.dart ('k') | pkg/barback/lib/src/phase_input.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(_onLog);
85 if (_logController.hasListener) { 86 _handleResults(cascade);
86 _logController.add(entry); 87 }
87 } else if (entry.level != LogLevel.FINE) {
88 // No listeners, so just print entry.
89 var buffer = new StringBuffer();
90 buffer.write("[${entry.level} ${entry.transform}] ");
91 88
92 if (entry.span != null) { 89 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors),
93 buffer.write(entry.span.getLocationMessage(entry.message)); 90 broadcast: true);
94 } else { 91 });
95 buffer.write(entry.message);
96 }
97
98 print(buffer);
99 }
100 });
101
102 cascade.results.listen((result) {
103 _cascadeResults[cascade.package] = result;
104 // If any cascade hasn't yet finished, the overall build isn't finished
105 // either.
106 if (_cascadeResults.values.any((result) => result == null)) return;
107
108 // Include all build errors for all cascades. If no cascades have
109 // errors, the result will automatically be considered a success.
110 _resultsController.add(
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 } 92 }
122 93
123 /// Gets the asset node identified by [id]. 94 /// Gets the asset node identified by [id].
124 /// 95 ///
125 /// If [id] is for a generated or transformed asset, this will wait until it 96 /// 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 97 /// has been created and return it. This means that the returned asset will
127 /// always be [AssetState.AVAILABLE]. 98 /// always be [AssetState.AVAILABLE].
128 /// 99 ///
129 /// If the asset cannot be found, returns null. 100 /// If the asset cannot be found, returns null.
130 Future<AssetNode> getAssetNode(AssetId id) { 101 Future<AssetNode> getAssetNode(AssetId id) {
131 var cascade = _cascades[id.package]; 102 return _inErrorZone(() {
132 if (cascade != null) return cascade.getAssetNode(id); 103 var cascade = _cascades[id.package];
133 return new Future.value(null); 104 if (cascade != null) return cascade.getAssetNode(id);
105 return new Future.value(null);
106 });
134 } 107 }
135 108
136 /// Gets all output assets. 109 /// Gets all output assets.
137 /// 110 ///
138 /// If a build is currently in progress, waits until it completes. The 111 /// 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 112 /// returned future will complete with an error if the build is not
140 /// successful. 113 /// successful.
141 /// 114 ///
142 /// Any transforms using [LazyTransformer]s will be forced to generate 115 /// Any transforms using [LazyTransformer]s will be forced to generate
143 /// concrete outputs, and those outputs will be returned. 116 /// concrete outputs, and those outputs will be returned.
144 Future<AssetSet> getAllAssets() { 117 Future<AssetSet> getAllAssets() {
145 for (var cascade in _cascades.values) { 118 for (var cascade in _cascades.values) {
146 cascade.forceAllTransforms(); 119 _inErrorZone(() => cascade.forceAllTransforms());
147 } 120 }
148 121
149 if (_cascadeResults.values.contains(null)) { 122 if (_cascadeResults.values.contains(null)) {
150 // A build is still ongoing, so wait for it to complete and try again. 123 // A build is still ongoing, so wait for it to complete and try again.
151 return results.first.then((_) => getAllAssets()); 124 return results.first.then((_) => getAllAssets());
152 } 125 }
153 126
154 // If an unexpected error occurred, complete with that. 127 // If an unexpected error occurred, complete with that.
155 if (_lastUnexpectedError != null) { 128 if (_lastUnexpectedError != null) {
156 var error = _lastUnexpectedError; 129 var error = _lastUnexpectedError;
(...skipping 16 matching lines...) Expand all
173 146
174 /// Adds [sources] to the graph's known set of source assets. 147 /// Adds [sources] to the graph's known set of source assets.
175 /// 148 ///
176 /// Begins applying any transforms that can consume any of the sources. If a 149 /// 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 150 /// given source is already known, it is considered modified and all
178 /// transforms that use it will be re-applied. 151 /// transforms that use it will be re-applied.
179 void updateSources(Iterable<AssetId> sources) { 152 void updateSources(Iterable<AssetId> sources) {
180 groupBy(sources, (id) => id.package).forEach((package, ids) { 153 groupBy(sources, (id) => id.package).forEach((package, ids) {
181 var cascade = _cascades[package]; 154 var cascade = _cascades[package];
182 if (cascade == null) throw new ArgumentError("Unknown package $package."); 155 if (cascade == null) throw new ArgumentError("Unknown package $package.");
183 cascade.updateSources(ids); 156 _inErrorZone(() => cascade.updateSources(ids));
184 }); 157 });
185 } 158 }
186 159
187 /// Removes [removed] from the graph's known set of source assets. 160 /// Removes [removed] from the graph's known set of source assets.
188 void removeSources(Iterable<AssetId> sources) { 161 void removeSources(Iterable<AssetId> sources) {
189 groupBy(sources, (id) => id.package).forEach((package, ids) { 162 groupBy(sources, (id) => id.package).forEach((package, ids) {
190 var cascade = _cascades[package]; 163 var cascade = _cascades[package];
191 if (cascade == null) throw new ArgumentError("Unknown package $package."); 164 if (cascade == null) throw new ArgumentError("Unknown package $package.");
192 cascade.removeSources(ids); 165 _inErrorZone(() => cascade.removeSources(ids));
193 }); 166 });
194 } 167 }
195 168
196 void updateTransformers(String package, 169 void updateTransformers(String package,
197 Iterable<Iterable<Transformer>> transformers) { 170 Iterable<Iterable<Transformer>> transformers) {
198 _cascades[package].updateTransformers(transformers); 171 _inErrorZone(() => _cascades[package].updateTransformers(transformers));
172 }
173
174 /// A handler for a log entry from an [AssetCascade].
175 void _onLog(LogEntry entry) {
176 if (_logController.hasListener) {
177 _logController.add(entry);
178 } else if (entry.level != LogLevel.FINE) {
179 // No listeners, so just print entry.
180 var buffer = new StringBuffer();
181 buffer.write("[${entry.level} ${entry.transform}] ");
182
183 if (entry.span != null) {
184 buffer.write(entry.span.getLocationMessage(entry.message));
185 } else {
186 buffer.write(entry.message);
187 }
188
189 print(buffer);
190 }
191 }
192
193 /// Listens to and handles the build results from [cascade].
194 void _handleResults(AssetCascade cascade) {
195 cascade.results.listen((result) {
196 _cascadeResults[cascade.package] = result;
197 // If any cascade hasn't yet finished, the overall build isn't finished
198 // either.
199 if (_cascadeResults.values.any((result) => result == null)) return;
200
201 // Include all build errors for all cascades. If no cascades have
202 // errors, the result will automatically be considered a success.
203 _resultsController.add(new BuildResult.aggregate(_cascadeResults.values));
204 });
205 }
206
207 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to
208 /// the error channel of [results].
209 ///
210 /// [body] can return a value or a [Future] that will be piped to the returned
211 /// [Future]. If it throws a [BarbackException], that exception will be piped
212 /// to the returned [Future] as well. Any other exceptions will be piped to
213 /// [results].
214 Future _inErrorZone(body()) {
215 var completer = new Completer.sync();
216 runZoned(() {
217 syncFuture(body).then(completer.complete).catchError((error, stackTrace) {
218 if (error is! BarbackException) throw error;
219 completer.completeError(error, stackTrace);
220 });
221 }, onError: (error, stackTrace) {
222 _lastUnexpectedError = error;
223 _lastUnexpectedErrorTrace = stackTrace;
224 _resultsController.addError(error, stackTrace);
225 });
226 return completer.future;
199 } 227 }
200 } 228 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/asset_cascade.dart ('k') | pkg/barback/lib/src/phase_input.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698