Make streams emit errors when they can't fetch data (#233)

This commit is contained in:
Simon Binder 2019-11-09 16:34:01 +01:00
parent b0a9255a07
commit 067212e66c
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 20 additions and 4 deletions

View File

@ -194,11 +194,18 @@ class QueryStream<T> {
// Fetch data if it's needed, publish that data if it's possible.
if (!_controller.hasListener) return;
final data = await _fetcher.fetchData();
_lastData = data;
T data;
if (!_controller.isClosed) {
_controller.add(data);
try {
data = await _fetcher.fetchData();
_lastData = data;
if (!_controller.isClosed) {
_controller.add(data);
}
} catch (e, s) {
if (!_controller.isClosed) {
_controller.addError(e, s);
}
}
}
}

View File

@ -141,6 +141,15 @@ void main() {
verify(executor.runSelect(any, any)).called(2);
});
test('stream emits error when loading the query throws', () {
final exception = Exception('stub');
when(executor.runSelect(any, any))
.thenAnswer((_) => Future.error(exception));
final result = db.customSelectQuery('select 1').watch().first;
expectLater(result, throwsA(exception));
});
group('stream keys', () {
final keyA = StreamKey('SELECT * FROM users;', [], User);
final keyB = StreamKey('SELECT * FROM users;', [], User);