doOnError method

ReactiveSubject<T> doOnError(
  1. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )
)

Performs a side-effect action for each error event emitted by the source ReactiveSubject.

The onError callback receives the error and stack trace but does not modify them.

Usage:

final subject = ReactiveSubject<int>();
final sideEffect = subject.doOnError((error, stackTrace) => print('Error: $error'));
sideEffect.stream.listen(print, onError: (e) => print('Stream error: $e'));
subject.addError('An error occurred'); // Prints: Error: An error occurred, then Stream error: An error occurred

Implementation

ReactiveSubject<T> doOnError(
    void Function(Object error, StackTrace stackTrace) onError) {
  final newSubject = ReactiveSubject<T>();
  stream
      .doOnError(onError)
      .listen(newSubject.add, onError: newSubject.addError);
  return newSubject;
}