Bloc: Error thrown within onTransition breaks the Bloc.

Created on 4 Nov 2019  路  2Comments  路  Source: felangel/bloc

Describe the bug
If an error/exception is thrown within onTransition, the bloc stops working completely:
bloc.add stops adding events or mapEventToState doesn't receive them.

To Reproduce

@override
void onTransition(transition) {
  throw new Exception('Explooooosion');
}

The bloc remains broken even if you comment out the exception, and hot reload.

The reason seems to stem from:

transformStates(transformEvents(_eventSubject, (Event event) {
      currentEvent = event;
      return mapEventToState(currentEvent).handleError(_handleError);
    })).forEach(
      (State nextState) {
        if (state == nextState || _stateSubject.isClosed) return;
        final transition = Transition(
          currentState: state,
          event: currentEvent,
          nextState: nextState,
        );
        BlocSupervisor.delegate.onTransition(this, transition);
        onTransition(transition); // <-- Will cause processing to stop.
        _stateSubject.add(nextState);
      },
    );

.forEach states:
If this stream emits an error, or if the call to [action] throws, the returned future completes with that error, and processing stops.

The workaround for now is simply to try catch code running in onTransition.

bug

Most helpful comment

Fixed in #643 and included in bloc v1.0.1 馃帀

All 2 comments

Hi @larssn 馃憢
Thanks for opening an issue!

Good point, I'll have this fixed asap 馃憤

Fixed in #643 and included in bloc v1.0.1 馃帀

Was this page helpful?
0 / 5 - 0 ratings