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 5b3bcb1916
commit 4a2b02782e
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 21 additions and 4 deletions

View File

@ -9,6 +9,7 @@
- New `batch` method on generated databases to execute multiple queries in a single batch
- Experimental support to run moor on a background isolate
- Reduce use of parentheses in SQL code generated at runtime
- Query streams now emit errors that happened while running the query
## 2.0.1

View File

@ -200,11 +200,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);