Skip to content

Commit 18a9c45

Browse files
authored
[go_router] Fix redirect zone async error handling (flutter#12836)
Fixes flutter#192066 ## Summary Replaces the guarded redirect zone with a value-only zone when running redirect callbacks. Redirect callbacks still have access to the current router through zone values, so APIs such as `GoRouter.of(context)` and `context.namedLocation(...)` continue to work during redirects. However, unrelated unawaited async errors started during a redirect are no longer caught by go_router and wrapped as `GoException`. ## Tests - `fvm dart run ../../script/tool/bin/flutter_plugin_tools.dart format --packages go_router` - `fvm dart run ../../script/tool/bin/flutter_plugin_tools.dart analyze --packages go_router` - `fvm dart run ../../script/tool/bin/flutter_plugin_tools.dart dart-test --packages go_router` - `fvm dart run ../../script/tool/bin/flutter_plugin_tools.dart validate --packages go_router` - `fvm flutter test test/go_router_test.dart` - `fvm flutter test test/redirect_chain_test.dart` For the checklist, check the ones that are true:
1 parent 9bc15a0 commit 18a9c45

3 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎packages/go_router/lib/src/configuration.dart‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -625,30 +625,9 @@ class RouteConfiguration {
625625
return callback();
626626
}
627627

628-
T? result;
629-
var errorOccurred = false;
630-
631-
runZonedGuarded<void>(
632-
() {
633-
result = callback();
634-
},
635-
(Object error, StackTrace stack) {
636-
errorOccurred = true;
637-
// Convert any exception during redirect to a GoException and rethrow
638-
final GoException goException = error is GoException
639-
? error
640-
: GoException('Exception during redirect: $error');
641-
throw goException;
642-
},
643-
zoneValues: <Object?, Object?>{currentRouterKey: router},
644-
);
645-
646-
if (errorOccurred) {
647-
// This should not be reached since we rethrow in the error handler
648-
throw GoException('Unexpected error in router zone');
649-
}
650-
651-
return result as T;
628+
return Zone.current
629+
.fork(zoneValues: <Object?, Object?>{currentRouterKey: router})
630+
.run<T>(callback);
652631
}
653632

654633
/// Get the location for the provided route.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes unrelated async errors started during redirects being wrapped as `GoException`.
3+
version: patch

‎packages/go_router/test/go_router_test.dart‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,43 @@ void main() {
20632063
expect(find.text('should not reach here'), findsNothing);
20642064
});
20652065

2066+
testWidgets('unrelated async errors started during redirect are not wrapped', (
2067+
WidgetTester tester,
2068+
) async {
2069+
final Object expectedTopError = StateError('top-level background failure');
2070+
final Object expectedRouteError = StateError('route-level background failure');
2071+
final caughtErrors = <Object>[];
2072+
2073+
await runZonedGuarded<Future<void>>(
2074+
() async {
2075+
await createRouter(
2076+
<RouteBase>[
2077+
GoRoute(
2078+
path: '/',
2079+
builder: (BuildContext context, GoRouterState state) => const HomeScreen(),
2080+
redirect: (BuildContext context, GoRouterState state) {
2081+
Future<void>.error(expectedRouteError);
2082+
return null;
2083+
},
2084+
),
2085+
],
2086+
tester,
2087+
redirect: (BuildContext context, GoRouterState state) {
2088+
Future<void>.error(expectedTopError);
2089+
return null;
2090+
},
2091+
);
2092+
2093+
await tester.pump();
2094+
},
2095+
(Object error, StackTrace stackTrace) {
2096+
caughtErrors.add(error);
2097+
},
2098+
);
2099+
2100+
expect(caughtErrors, <Object>[expectedTopError, expectedRouteError]);
2101+
});
2102+
20662103
testWidgets('context extension methods work in redirects', (WidgetTester tester) async {
20672104
String? capturedNamedLocation;
20682105
final routes = <GoRoute>[

0 commit comments

Comments
 (0)