feat: ignoring addError in reported stacktrace
- Dominant language
- Dart
- Stars
- 12.5k
- Forks
- 3.4k
- PR merge metrics
- No merged PRs in 30d
Description
**Description**
When reading through the stack trace reported when calling `addError(Exception('Hello there'));` the first frame is always `BlocBase.addError`. This is because `addError()` implementation passes `StackTrace.current` if no stack trace is provided:
```dart
void addError(Object error, [StackTrace? stackTrace]) {
onError(error, stackTrace ?? StackTrace.current);
}
```
Here's a sample report from Crashlytics:

**Desired Solution**
The `BlocBase.addError` should be omitted in the stack trace passed to `onError` if no stack trace is passed to `addError`.
The solution I used in my BlocObserver is to create new Trace (using `stack_trace` package) and skip frames related to `BlocBase.addError`:
```dart
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
var trace = Trace.from(stackTrace);
// addError obscures the crashlytics reports
if (trace.frames.isNotEmpty &&
trace.frames.first.member == 'BlocBase.addError') { // I only care if this is the first frame, otherwise let's not modify the frames
trace = trace.skipFrames((f) => f.member == 'BlocBase.addError');
}
if (kDebugMode) {
///
} else {
FirebaseCrashlytics.instance.recordError(error, trace);
}
super.onError(bloc, error, trace);
}
//...
extension on Trace {
Trace skipFrames(bool Function(Frame f) predicate) {
final newFrames = frames.where((element) => !predicate(element));
return Trace(newFrames, original: original.toString());
}
}
```
**Alternatives Considered**
- Omitting the stack trace manually in my BlocObserver
- Always passing `StackTrace.current` to `addError`
Contributor guide
Assessment
This issue has not been assessed yet.