使用dart的异步与stack_trace的链 [英] Using dart's async with stack_trace's Chain

查看:474
本文介绍了使用dart的异步与stack_trace的链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图利用stack_trace提供的Chain对象,如下:

I'm trying to take advantage of the Chain object provided by stack_trace like so:

import 'dart:async';
import 'package:stack_trace/stack_trace.dart';

main() async {
  print('Hello world: ${console_test.calculate()}!');
  Chain.capture(() async {
    try {
      await testFunction();
    } catch(e,st) {
      print(Trace.format(new Chain.forTrace(st)));
    }
  });
}


Future testFunction() async {
  throw new Exception("TEST");
}

我得到的输出是:

Hello world: 42!
main.dart 4:1  main.<async>.<fn>.<async>



我理解输出的堆栈跟踪应包括testFunction,但由于某种原因不是。如果我用futures代替:

I understand that the outputted stack trace should include testFunction, but for some reason it isn't. If I do it with futures instead:

import 'dart:async';
import 'package:stack_trace/stack_trace.dart';

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

Future testFunction() async {
  throw new Exception("TEST");
}

我得到更多的预期输出:

I get more expected output:

Hello world: 42!
main.dart 17:3       testFunction.<async>
dart:async           _Completer.completeError
main.dart 4:1        testFunction.<async>
dart:async           Future.Future
main.dart 4:1        testFunction
main.dart 11:26      main.<async>.<fn>
package:stack_trace  Chain.capture
main.dart 10:16      main.<async>

我做错了什么?

推荐答案

是什么版本的Dart和 stack_trace 你在使用吗?在Dart 1.9.0-edge.44028 stack_trace 1.2.3 ,删除 console_test 行后,我得到以下输出:

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>

这也值得注意,你不需要使用 Trace。格式。你可以使用 Chain.terse ,这也将保留异步间隙。

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.

这篇关于使用dart的异步与stack_trace的链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆