Skip to content
Open
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/bloc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.3.0

- fix: omit synthetic `BlocBase.addError` frame from stack traces auto-captured when no `stackTrace` is passed to `addError` ([#3585](https://github.com/felangel/bloc/issues/3585))

# 9.2.0

- feat: add `MultiBlocObserver` ([#4714](https://github.com/felangel/bloc/pull/4714))
Expand Down
13 changes: 12 additions & 1 deletion packages/bloc/lib/src/bloc_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,15 @@ abstract class BlocBase<State>
}

/// Reports an [error] which triggers [onError] with an optional [StackTrace].
///
/// If no [stackTrace] is provided, the current stack trace is captured with
/// the synthetic `BlocBase.addError` frame stripped, so the resulting trace
/// begins at the caller's call site.
@protected
@mustCallSuper
@override
void addError(Object error, [StackTrace? stackTrace]) {
onError(error, stackTrace ?? StackTrace.current);
onError(error, stackTrace ?? _withoutTopFrame(StackTrace.current));
}

/// Called whenever an [error] occurs and notifies [BlocObserver.onError].
Expand Down Expand Up @@ -175,4 +179,11 @@ abstract class BlocBase<State>
_blocObserver.onClose(this);
await _stateController.close();
}

static StackTrace _withoutTopFrame(StackTrace trace) {
final raw = trace.toString();
final newline = raw.indexOf('\n');
if (newline < 0) return trace;
return StackTrace.fromString(raw.substring(newline + 1));
}
}
2 changes: 1 addition & 1 deletion packages/bloc/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bloc
description: A predictable state management library that helps implement the BLoC (Business Logic Component) design pattern.
version: 9.2.0
version: 9.3.0
repository: https://github.com/felangel/bloc/tree/master/packages/bloc
issue_tracker: https://github.com/felangel/bloc/issues
homepage: https://github.com/felangel/bloc
Expand Down
37 changes: 37 additions & 0 deletions packages/bloc/test/cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ void main() {
() => observer.onError(cubit, expectedError, expectedStackTrace),
).called(1);
});

test('omits BlocBase.addError frame when no stackTrace is passed',
() async {
StackTrace? captured;
CounterCubit(
onErrorCallback: (_, stackTrace) => captured = stackTrace,
// ignore: invalid_use_of_protected_member
).addError(Exception('fatal exception'));

expect(captured, isNotNull);
final firstLine = captured!.toString().split('\n').first;
expect(firstLine, isNot(contains('BlocBase.addError')));
expect(firstLine, isNot(contains('bloc_base.dart')));
});

test('passes through an explicit stackTrace unchanged', () async {
final explicit = StackTrace.fromString('#0 user_call (test.dart:1)');
StackTrace? captured;
CounterCubit(
onErrorCallback: (_, stackTrace) => captured = stackTrace,
// ignore: invalid_use_of_protected_member
).addError(Exception('fatal exception'), explicit);

expect(captured, same(explicit));
});

test('captured stackTrace is non-empty when no stackTrace is passed',
() async {
StackTrace? captured;
CounterCubit(
onErrorCallback: (_, stackTrace) => captured = stackTrace,
// ignore: invalid_use_of_protected_member
).addError(Exception('fatal exception'));

expect(captured, isNotNull);
expect(captured!.toString(), isNotEmpty);
});
});

group('onChange', () {
Expand Down
Loading