Using dart's async with stack_trace's Chain

浪尽此生 提交于 2019-12-08 20:50:29

What versions of Dart and stack_trace are you using? On Dart 1.9.0-edge.44028 with stack_trace 1.2.3, after deleting the console_test line, I get the following output:

test.dart 16:3       testFunction.<async>
dart:async           _Completer.completeError
test.dart 17:2       testFunction.<async>
dart:async           Future.Future.microtask
test.dart 7:25       main.<async>.<fn>.<async>
package:stack_trace  Chain.capture
test.dart 5:16       main.<async>

It's also worth noting that you don't really need to use Trace.format with Chain. You can just use Chain.terse, which will also preserve the asynchronous gaps.

SanMadJack

@nex3 correctly pointed out that my original example in fact works in the 1.9.0 dev build of Dart, so this might just be an incompatibility at present. Regardless, taking the async off of the Chain.capture, changing the await on testFunction() to a return and using an await on Chain.Capture actually produce the correct output. This obviously isn't 100% optimal, but it gets the job done.

main() async {
  print('Hello world: ${console_test.calculate()}!');
  var test = await Chain.capture(() {
    return testFunction();
  },
  onError: (e, stackTrace) => print(Trace.format(new Chain.forTrace(stackTrace))));
  print(test);
}

Future testFunction() async {
  return "nice!";
  throw new Exception("TEST");
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!