Index: pkg/http/lib/src/utils.dart |
diff --git a/pkg/http/lib/src/utils.dart b/pkg/http/lib/src/utils.dart |
index 28e2bfbec3141a0f8a7e42fef898ff92b49d8738..bca314d95cf1828fe0f51b6b7b612b8d379d469c 100644 |
--- a/pkg/http/lib/src/utils.dart |
+++ b/pkg/http/lib/src/utils.dart |
@@ -172,8 +172,7 @@ StreamConsumer<List<int>, dynamic> wrapOutputStream(OutputStream stream) => |
class _OutputStreamConsumer implements StreamConsumer<List<int>, dynamic> { |
final OutputStream _outputStream; |
- _OutputStreamConsumer(this._outputStream) |
- : super(); |
+ _OutputStreamConsumer(this._outputStream); |
Future consume(Stream<List<int>> stream) { |
// TODO(nweiz): we have to manually keep track of whether or not the |
@@ -182,19 +181,29 @@ class _OutputStreamConsumer implements StreamConsumer<List<int>, dynamic> { |
// the following TODO. |
var completed = false; |
var completer = new Completer(); |
- stream.listen((data) => _outputStream.write(data), onDone: () { |
- _outputStream.close(); |
- // TODO(nweiz): wait until _outputStream.onClosed is called once issue |
- // 7761 is fixed. |
- if (!completed) completer.complete(); |
- completed = true; |
- }); |
+ stream.listen((data) { |
+ // Writing empty data to a closed stream can cause errors. |
+ if (data.isEmpty) return; |
+ |
+ // TODO(nweiz): remove this try/catch when issue 7836 is fixed. |
+ try { |
+ _outputStream.write(data); |
+ } catch (e, stack) { |
+ if (!completed) completer.completeError(e, stack); |
+ completed = true; |
+ } |
+ }, onDone: () => _outputStream.close()); |
_outputStream.onError = (e) { |
if (!completed) completer.completeError(e); |
completed = true; |
}; |
+ _outputStream.onClosed = () { |
+ if (!completed) completer.complete(); |
+ completed = true; |
+ }; |
+ |
return completer.future; |
} |
} |