diff --git a/packages/flutter_tools/lib/src/base/async_guard.dart b/packages/flutter_tools/lib/src/base/async_guard.dart index 76fe120f95..1c7d636dde 100644 --- a/packages/flutter_tools/lib/src/base/async_guard.dart +++ b/packages/flutter_tools/lib/src/base/async_guard.dart @@ -26,8 +26,7 @@ import 'dart:async'; /// completed with its result when passed the error object and stack trace. /// /// After the returned [Future] is completed, whether it be with a value or an -/// error, all further errors resulting from the execution of [fn] both -/// synchronous and asynchronous are ignored. +/// error, all further errors resulting from the execution of [fn] are ignored. /// /// Rationale: /// @@ -100,10 +99,10 @@ Future asyncGuard( completer.completeError(e, s); return; } - if (onError is _UnaryOnError) { - completer.complete(onError(e)); - } else if (onError is _BinaryOnError) { + if (onError is _BinaryOnError) { completer.complete(onError(e, s)); + } else if (onError is _UnaryOnError) { + completer.complete(onError(e)); } } diff --git a/packages/flutter_tools/test/general.shard/base/async_guard_test.dart b/packages/flutter_tools/test/general.shard/base/async_guard_test.dart index 1546cd5839..5b442eeed3 100644 --- a/packages/flutter_tools/test/general.shard/base/async_guard_test.dart +++ b/packages/flutter_tools/test/general.shard/base/async_guard_test.dart @@ -181,7 +181,7 @@ void main() { expect(caughtByCatchError, true); }); - test('asyncError is propagated correctly with onError callback', () async { + test('asyncError is propagated with binary onError', () async { bool caughtByZone = false; bool caughtByHandler = false; bool caughtByOnError = false; @@ -218,4 +218,83 @@ void main() { expect(caughtByHandler, false); expect(caughtByOnError, true); }); + + test('asyncError is propagated with unary onError', () async { + bool caughtByZone = false; + bool caughtByHandler = false; + bool caughtByOnError = false; + + final Completer completer = Completer(); + await FakeAsync().run((FakeAsync time) { + unawaited(runZoned(() async { + final Future f = asyncGuard( + () => delayedThrow(time), + onError: (Object e) { + caughtByOnError = true; + }, + ); + try { + await f; + } catch (e) { + caughtByHandler = true; + } + if (!completer.isCompleted) { + completer.complete(null); + } + }, onError: (Object e, StackTrace s) { + caughtByZone = true; + if (!completer.isCompleted) { + completer.complete(null); + } + })); + time.elapse(const Duration(seconds: 1)); + time.flushMicrotasks(); + return completer.future; + }); + + expect(caughtByZone, false); + expect(caughtByHandler, false); + expect(caughtByOnError, true); + }); + + test('asyncError is propagated with optional stack trace', () async { + bool caughtByZone = false; + bool caughtByHandler = false; + bool caughtByOnError = false; + bool nonNullStackTrace = false; + + final Completer completer = Completer(); + await FakeAsync().run((FakeAsync time) { + unawaited(runZoned(() async { + final Future f = asyncGuard( + () => delayedThrow(time), + onError: (Object e, [StackTrace s]) { + caughtByOnError = true; + nonNullStackTrace = s != null; + }, + ); + try { + await f; + } catch (e) { + caughtByHandler = true; + } + if (!completer.isCompleted) { + completer.complete(null); + } + }, onError: (Object e, StackTrace s) { + caughtByZone = true; + if (!completer.isCompleted) { + completer.complete(null); + } + })); + time.elapse(const Duration(seconds: 1)); + time.flushMicrotasks(); + return completer.future; + }); + + expect(caughtByZone, false); + expect(caughtByHandler, false); + expect(caughtByOnError, true); + expect(nonNullStackTrace, true); + }); }