简体   繁体   中英

Using dart's async with stack_trace's 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");
}

The output I get is this:

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

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>

Am I doing something wrong? Is Chain incompatible with the whole async/await thing?

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.

@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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM