liangxianzhe/creator

Emitter.stream: Is there a way to catch exceptions ?

Closed this issue · 2 comments

Consider the following stream with exception:

Stream<int> someStream() async* {
  while (true) {
    await Future.delayed(const Duration(seconds: 1));
    throw Exception("Test exception");
    yield 123;
  }
}

final someStreamEmitter = Emitter.stream((ref) => someStream());

This will produce "[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception"

Is there a way to handle exceptions in stream ? Seems the current implementation is not passing the onError param to listen()

You are right. We need a better approach on this. I just merged a fix e7de5b2 and published a new version 0.3.2. Tested on the below code, but let me know if you still see issue on have a better approach on the fix.

import 'package:creator_core/creator_core.dart';

Stream<int> someStream() async* {
  while (true) {
    await Future.delayed(const Duration(seconds: 1));
    throw Exception("Test exception");
    yield 123;
  }
}

Future<void> main(List<String> args) async {
  final ref = Ref();
  final someStreamEmitter = Emitter.stream((ref) => someStream());
  try {
    print(await ref.watch(someStreamEmitter));
  } catch (e) {
    print("caught $e");
  }
  print("done");
}

Great. Thanks for the quick fix!