waitForEx<T> function

T waitForEx<T>(
  1. Future<T> wrapped
)

Changes a async call into a synchronous call.

waitForEx(someAsyncFunction());

Wraps the standard cli waitFor but rethrows any exceptions with a repaired stacktrace.

Exceptions would normally have a microtask stack which is useless. The repaired stack replaces the exceptions stack with a full stack.

Implementation

// T waitForEx<T>(Future<T> future) =>
//     Chain.capture(() => cli.waitFor<T>(future), onError: (e, st) {
//       print('hi');
//       Error.throwWithStackTrace(e, st);
//     });

T waitForEx<T>(Future<T> wrapped) {
  final stackTrace = Trace.current();
  late T value;
  try {
    value = cli.waitFor<T>(wrapped);
  }
  // ignore: avoid_catching_errors
  on AsyncError catch (e) {
    Error.throwWithStackTrace(e.error, _merge(stackTrace, e.stackTrace));
    // ignore: avoid_catches_without_on_clauses
  } catch (e, st) {
    Error.throwWithStackTrace(e, _merge(stackTrace, st));
  }

  return value;
}