From 800215b4b93110cfc3c49cba1c0bd2251ced8dbd Mon Sep 17 00:00:00 2001 From: Ketul Makwana Date: Fri, 11 Sep 2026 12:28:58 +0530 Subject: [PATCH] Fix redirect zone async error handling --- packages/go_router/lib/src/configuration.dart | 27 ++-------------- ...ix_redirect_zone_async_error_handling.yaml | 3 ++ packages/go_router/test/go_router_test.dart | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 packages/go_router/pending_changelogs/fix_redirect_zone_async_error_handling.yaml diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index 932144426b85..932fbb8d4f38 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -622,30 +622,9 @@ class RouteConfiguration { return callback(); } - T? result; - var errorOccurred = false; - - runZonedGuarded( - () { - result = callback(); - }, - (Object error, StackTrace stack) { - errorOccurred = true; - // Convert any exception during redirect to a GoException and rethrow - final GoException goException = error is GoException - ? error - : GoException('Exception during redirect: $error'); - throw goException; - }, - zoneValues: {currentRouterKey: router}, - ); - - if (errorOccurred) { - // This should not be reached since we rethrow in the error handler - throw GoException('Unexpected error in router zone'); - } - - return result as T; + return Zone.current + .fork(zoneValues: {currentRouterKey: router}) + .run(callback); } /// Get the location for the provided route. diff --git a/packages/go_router/pending_changelogs/fix_redirect_zone_async_error_handling.yaml b/packages/go_router/pending_changelogs/fix_redirect_zone_async_error_handling.yaml new file mode 100644 index 000000000000..ad64c2345efb --- /dev/null +++ b/packages/go_router/pending_changelogs/fix_redirect_zone_async_error_handling.yaml @@ -0,0 +1,3 @@ +changelog: | + - Fixes unrelated async errors started during redirects being wrapped as `GoException`. +version: patch diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index fc38957ae039..7a3a3131716b 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -2063,6 +2063,38 @@ void main() { expect(find.text('should not reach here'), findsNothing); }); + testWidgets('unrelated async errors started during redirect are not wrapped', ( + WidgetTester tester, + ) async { + final Object expectedError = StateError('background failure'); + Object? caughtError; + + await runZonedGuarded>( + () async { + await createRouter( + [ + GoRoute( + path: '/', + builder: (BuildContext context, GoRouterState state) => const HomeScreen(), + ), + ], + tester, + redirect: (BuildContext context, GoRouterState state) { + Future.error(expectedError); + return null; + }, + ); + + await tester.pump(); + }, + (Object error, StackTrace stackTrace) { + caughtError = error; + }, + ); + + expect(caughtError, same(expectedError)); + }); + testWidgets('context extension methods work in redirects', (WidgetTester tester) async { String? capturedNamedLocation; final routes = [