Skip to content

Commit

Permalink
closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Jun 4, 2020
1 parent ccd2736 commit b5cc1c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/state_notifier/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [0.5.0] - 4/06/2020

If no `onError` is specified, errors are now reported to the current `Zone`.

## [0.4.0] - 13/03/2020

Added a flag on `addListener` to disable the first immediate call of the listener
Expand Down
9 changes: 7 additions & 2 deletions packages/state_notifier/lib/state_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library state_notifier;

import 'dart:async';
import 'dart:collection';

import 'package:meta/meta.dart';
Expand Down Expand Up @@ -152,9 +153,13 @@ Consider checking `mounted`.
for (final listenerEntry in _listeners) {
try {
listenerEntry.listener(value);
} catch (err, stack) {
} catch (error, stackTrace) {
didThrow = true;
onError?.call(err, stack);
if (onError != null) {
onError(error, stackTrace);
} else {
Zone.current.handleUncaughtError(error, stackTrace);
}
}
}
if (didThrow) {
Expand Down
2 changes: 1 addition & 1 deletion packages/state_notifier/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: state_notifier
description: ValueNotifier, but outside Flutter and with some extra perks
version: 0.4.0
version: 0.5.0
author: Remi Rousselet <darky12s@gmail.com>
homepage: https://github.com/rrousselGit/state_notifier

Expand Down
20 changes: 20 additions & 0 deletions packages/state_notifier/test/state_notifier_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -222,6 +224,24 @@ void main() {
verify(onError(error, argThat(isNotNull))).called(1);
verifyNoMoreInteractions(onError);
});
test('no onError fallbacks to zone', () {
final notifier = TestNotifier(0)
..addListener((v) {
if (v > 0) throw StateError('first');
})
..addListener((v) {
if (v > 0) throw StateError('second');
});

final errors = <Object>[];
runZonedGuarded(notifier.increment, (err, stack) => errors.add(err));

expect(errors, [
isStateError.having((s) => s.message, 'message', 'first'),
isStateError.having((s) => s.message, 'message', 'second'),
isA<Error>(),
]);
});
test('onError (change)', () {
final onError = ErrorListener();
final notifier = TestNotifier(0)
Expand Down

0 comments on commit b5cc1c2

Please sign in to comment.