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

Unified Diff: sdk/lib/_internal/pub/lib/src/utils.dart

Issue 22878002: Handle missing files more gracefully in pub serve. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 4 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
Index: sdk/lib/_internal/pub/lib/src/utils.dart
diff --git a/sdk/lib/_internal/pub/lib/src/utils.dart b/sdk/lib/_internal/pub/lib/src/utils.dart
index 6a048cf1612a5184c6c0b0d309dc4cec16c561c9..da8f640e85dbed58d61939d658fcb017c0310d53 100644
--- a/sdk/lib/_internal/pub/lib/src/utils.dart
+++ b/sdk/lib/_internal/pub/lib/src/utils.dart
@@ -168,6 +168,46 @@ void chainToCompleter(Future future, Completer completer) {
onError: (e) => completer.completeError(e));
}
+/// Ensures that [stream] can emit at least one value successfully (or close
+/// without any values).
+///
+/// For example, reading asynchronously from a non-existent file will return a
+/// stream that fails on the first chunk. In order to handle that more
+/// gracefully, you may want to check that the stream looks like it's working
+/// before you pipe the stream to something else.
+///
+/// This lets you do that. It returns a [Future] that completes to a [Stream]
+/// emitting the same values and errors as [stream], but only if at least one
+/// value can be read successfully. If an error occurs before any values are
+/// emitted, the returned Future completes to that error.
+Future<Stream> validateStream(Stream stream) {
+ var completer = new Completer<Stream>();
+ var controller = new StreamController(sync: true);
+
+ StreamSubscription subscription;
+ subscription = stream.listen((value) {
+ // We got a value, so the stream is valid.
+ if (!completer.isCompleted) completer.complete(controller.stream);
+ controller.add(value);
+ }, onError: (error) {
+ // If the error came after values, it's OK.
+ if (completer.isCompleted) controller.addError(error);
nweiz 2013/08/12 21:35:08 I think you want to return here. Right now you'll
Bob Nystrom 2013/08/12 21:45:00 Oops, accidentally deleted that when I reordered t
+
+ // Otherwise, the error came first and the stream is invalid.
+ completer.completeError(error);
+
+ // We don't be returning the stream at all in this case, so unsubscribe
+ // and swallow the error.
+ subscription.cancel();
+ }, onDone: () {
+ // It closed with no errors, so the stream is valid.
+ if (!completer.isCompleted) completer.complete(controller.stream);
+ controller.close();
+ });
+
+ return completer.future;
+}
+
// TODO(nweiz): remove this when issue 7964 is fixed.
/// Returns a [Future] that will complete to the first element of [stream].
/// Unlike [Stream.first], this is safe to use with single-subscription streams.
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/command/serve.dart ('k') | sdk/lib/_internal/pub/test/serve/missing_asset_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698