ShellLinesController.sink.addError should add errors to ShellLinesController.stream
Conrad33 opened this issue · comments
Attempting to addError to the stream of a ShellLinesController will always result in an unhandled exception.
For example:
Stream<double> shellTask() async* {
final output = ShellLinesController();
Shell(stdout: output.sink).run(
'''
some script that doesn't return 0
''',
).then((_) => output.close(), onError: output.sink.addError); // Add error to the stream
await for (final output in output.stream) { // This code should throw here, but it doesn't. Error isn't propagated to this stream.
// Parse line by line and yield some formatted data
yield formattedData;
}
}
My current work around:
import 'package:async/async.dart';
Stream<double> shellTask() async* {
final output = ShellLinesController();
final errors = StreamController<Never>();
Shell(stdout: output.sink).run(
'''
some script that doesn't return 0
''',
).then((_) {output.close(); errors.close();}, onError: errors.sink.addError); // Add error to the stream
final outputStream = StreamGroup.merge<String>([output.stream, errors.stream]);
await for (final output in outputStream ) { // Now this code throws here.
// Parse line by line and yield some formatted data
yield formattedData;
}
}
Thanks for the report, the sample code and the workaround you use as it helps investigation a solution on my side. I have to admin that I never thought about errors in this simple controller. I added a fix in just published 0.13.3+1 that should hopefully work in your case. Let me know!
Works good 👍