onError method

  1. @override
void onError(
  1. BlocBase bloc,
  2. Object error,
  3. StackTrace stackTrace
)

Called whenever an error is thrown in any Bloc or Cubit. The stackTrace argument may be StackTrace.empty if an error was received without a stack trace.

Implementation

@override
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
  super.onError(bloc, error, stackTrace);
  assert(() {
    final context = CausalityZone.currentContext();
    TrinityEventBus.instance.emit(CausalEvent(
      parentId: context?.eventId,
      type: CausalEventType.crashEvent,
      description: '${bloc.runtimeType}: error',
      metadata: {
        'bloc_type': bloc.runtimeType.toString(),
        'error_type': error.runtimeType.toString(),
        'error_message': error.toString().substring(
              0,
              error.toString().length > 500 ? 500 : error.toString().length,
            ),
        'stack_top_3': stackTrace.toString().split('\n').take(3).join('\n'),
      },
    ));
    return true;
  }());
}