Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/listen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0-beta.4

- `ErrorContext` is now a required positional, non-nullable parameter.

## 1.0.0-beta.3

- Updates `ErrorCallback` to pass the raw error object and original `StackTrace`.
Expand Down
15 changes: 10 additions & 5 deletions packages/listen/lib/src/listen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ enum ErrorContext {
}

/// Signature of callbacks that are called when an error is reported by a listener or assertion.
typedef ErrorCallback =
void Function(Object error, StackTrace? stackTrace, {ErrorContext? context});
///
/// The cause of the error can be read from `context`.
///
/// See also:
///
/// * [ErrorContext], which identifies the context in which an error was reported.
typedef ErrorCallback = void Function(Object error, StackTrace? stackTrace, ErrorContext context);

/// Signature of callbacks that are called when an object is created.
///
Expand Down Expand Up @@ -71,7 +76,7 @@ abstract class Listenable {
/// Error callback that is called when an error is thrown by a listener or assertion.
///
/// By default, errors are rethrown preserving their original [StackTrace].
static ErrorCallback onError = (Object error, StackTrace? stackTrace, {ErrorContext? context}) {
static ErrorCallback onError = (Object error, StackTrace? stackTrace, ErrorContext context) {
Error.throwWithStackTrace(error, stackTrace ?? StackTrace.current);
};

Expand Down Expand Up @@ -187,7 +192,7 @@ mixin class ChangeNotifier implements Listenable {
'can no longer be used.',
),
StackTrace.current,
context: ErrorContext.assertion,
ErrorContext.assertion,
);
}
return true;
Expand Down Expand Up @@ -437,7 +442,7 @@ mixin class ChangeNotifier implements Listenable {
try {
_listeners[i]?.call();
} catch (exception, stack) {
Listenable.onError(exception, stack, context: ErrorContext.listener);
Listenable.onError(exception, stack, ErrorContext.listener);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/listen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: listen
description: A package to notify state changes to interested listeners in pure Dart.
repository: https://github.com/flutter/core-packages/tree/main/packages/listen
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+listen%22
version: 1.0.0-beta.3
version: 1.0.0-beta.4

environment:
sdk: ^3.10.0
Expand Down
10 changes: 5 additions & 5 deletions packages/listen/test/listen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void main() {
setUp(() {
lastError = null;
lastContext = null;
Listenable.onError = (Object error, StackTrace? stackTrace, {ErrorContext? context}) {
Listenable.onError = (Object error, StackTrace? stackTrace, ErrorContext context) {
lastError = error;
lastContext = context;
};
Expand Down Expand Up @@ -116,7 +116,7 @@ void main() {

final ErrorCallback original = Listenable.onError;
final List<Object> errors = [];
Listenable.onError = (Object error, StackTrace? stackTrace, {ErrorContext? context}) {
Listenable.onError = (Object error, StackTrace? stackTrace, ErrorContext context) {
errors.add(error);
};
addTearDown(() {
Expand Down Expand Up @@ -190,7 +190,7 @@ void main() {
test('Listenable.onError preserves runtime exception types', () {
final List<Object> errors = [];
final ErrorCallback original = Listenable.onError;
Listenable.onError = (Object error, StackTrace? stackTrace, {ErrorContext? context}) {
Listenable.onError = (Object error, StackTrace? stackTrace, ErrorContext context) {
errors.add(error);
};
addTearDown(() {
Expand All @@ -210,7 +210,7 @@ void main() {
test('Listenable.onError default implementation rethrows and preserves StackTrace', () {
final stack = StackTrace.fromString('test_stack_trace_marker');
try {
originalOnError(const FormatException('bad format'), stack);
originalOnError(const FormatException('bad format'), stack, ErrorContext.listener);
fail('Expected originalOnError to throw');
} catch (e, s) {
expect(e, isA<FormatException>());
Expand All @@ -221,7 +221,7 @@ void main() {
test('Listenable.onError reports correct ErrorContext across all failure types', () {
final List<ErrorContext?> contexts = [];
final ErrorCallback original = Listenable.onError;
Listenable.onError = (Object error, StackTrace? stackTrace, {ErrorContext? context}) {
Listenable.onError = (Object error, StackTrace? stackTrace, ErrorContext context) {
contexts.add(context);
};
addTearDown(() {
Expand Down
Loading