flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.01k stars 27.19k forks source link

[go_router] exceptions thrown during build does not trigger the errorPageBuilder #126753

Open DamienMrtl opened 1 year ago

DamienMrtl commented 1 year ago

Using Flutter 3.10.0 and go_router 7.0.1

Steps to reproduce

  1. In a goRoute build, throw a custom exception MyCustomRoutingException
  2. In the errorPageBuilder, try to check if exception is of type MyCustomRoutingException

Expected results

Expected to call ErrorBuilder when an error occurs.

This was working with go_router v3.1.1

The change was made here in this commit.

Actual results

Error gets passed up in main.dart

Code sample

Code sample ```dart import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { MyApp({super.key}); final GoRouter router = GoRouter( routes: [ GoRoute( path: '/', pageBuilder: (context, state) { throw const CustomRoutingError(); }, ), ], errorPageBuilder: (context, state) { if (state.error is CustomRoutingError) { return const MaterialPage( key: ValueKey('error_page'), child: Center( child: Text('Custom Routing Error'), ), ); } return const MaterialPage( key: ValueKey('error_page'), child: Center( child: Text('Unknown Error'), ), ); }, ); @override Widget build(BuildContext context) { return MaterialApp.router( routeInformationProvider: router.routeInformationProvider, routeInformationParser: router.routeInformationParser, routerDelegate: router.routerDelegate, backButtonDispatcher: router.backButtonDispatcher, ); } } class CustomRoutingError implements Exception { const CustomRoutingError(); } ```

Flutter Doctor output

Doctor output ```console [√] Flutter (Channel stable, 3.10.0, on Microsoft Windows [Version 10.0.22621.1702], locale en-CH) • Flutter version 3.10.0 on channel stable at C:\src\flutter_2 • Upstream repository https://github.com/flutter/flutter.git • Framework revision 84a1e904f4 (4 days ago), 2023-05-09 07:41:44 -0700 • Engine revision d44b5a94c9 • Dart version 3.0.0 • DevTools version 2.23.1 [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 32.0.0) • Android SDK at C:\Users\morat\AppData\Local\Android\sdk • Platform android-33, build-tools 32.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [!] Visual Studio - develop for Windows (Visual Studio Build Tools 2017 15.9.38) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools • Visual Studio Build Tools 2017 version 15.9.28307.1622 • Windows 10 SDK version 10.0.17763.0 X Visual Studio 2019 or later is required. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [√] Android Studio (version 2020.3) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [√] VS Code, 64-bit edition (version 1.78.2) • VS Code at C:\Program Files\Microsoft VS Code • Flutter extension version 3.64.0 [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.1702] • Chrome (web) • chrome • web-javascript • Google Chrome 113.0.5672.93 • Edge (web) • edge • web-javascript • Microsoft Edge 112.0.1722.68 [√] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ```
anaseng779824508 commented 1 year ago

Windows Version (the doctor check crashed) X Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this issue at https://github.com/flutter/flutter/issues. X ProcessException: Failed to find "systeminfo" in the search path. Command: systeminfo

DamienMrtl commented 1 year ago

Windows Version (the doctor check crashed) X Due to an error, the doctor check did not complete. If the error message below is not helpful, please let us know about this issue at https://github.com/flutter/flutter/issues. X ProcessException: Failed to find "systeminfo" in the search path. Command: systeminfo

The doctor issue is not relevant in this case. I'm using flutter web.

darshankawar commented 1 year ago

Thanks for the detailed report. Seeing the same exception upon running the provided code sample. It may not be specific to web, as I noticed the exception while running on mobile.

console error log ``` ======== Exception caught by widgets library ======================================================= The following CustomRoutingError was thrown building Builder(dirty): Instance of 'CustomRoutingError' The relevant error-causing widget was: Builder Builder:file:///Users/dhs/.pub-cache/hosted/pub.dev/go_router-7.0.1/lib/src/builder.dart:83:7 When the exception was thrown, this was the stack: #0 new MyApp. (package:dummy_test/main.dart:16:11) #1 RouteBuilder._buildPageForRoute (package:go_router/src/builder.dart:296:27) #2 RouteBuilder._buildRecursive (package:go_router/src/builder.dart:182:34) #3 RouteBuilder.buildPages (package:go_router/src/builder.dart:141:7) #4 RouteBuilder.tryBuild (package:go_router/src/builder.dart:119:9) #5 RouteBuilder.build. (package:go_router/src/builder.dart:88:35) #6 Builder.build (package:flutter/src/widgets/basic.dart:7632:48) #7 StatelessElement.build (package:flutter/src/widgets/framework.dart:5156:49) #8 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5086:15) #9 Element.rebuild (package:flutter/src/widgets/framework.dart:4805:7) #10 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5068:5) #11 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5062:5) ... Normal element mounting (412 frames) #423 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3971:16) #424 Element.updateChild (package:flutter/src/widgets/framework.dart:3708:18) #425 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1253:16) #426 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1222:5) #427 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:1169:18) #428 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2720:19) #429 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1168:13) #430 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:1001:7) #431 WidgetsBinding.scheduleAttachRootWidget. (package:flutter/src/widgets/binding.dart:981:7) #435 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12) (elided 3 frames from class _Timer and dart:async-patch) ==================================================================================================== W/Parcel (12047): Expecting binder but got null! ```

Latest breaking changes: https://pub.dev/packages/go_router/changelog#700

stable, master flutter doctor -v ``` [!] Flutter (Channel stable, 3.10.0, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.10.0 on channel stable at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 84a1e904f4 (2 days ago), 2023-05-09 07:41:44 -0700 • Engine revision d44b5a94c9 • Dart version 3.0.0 • DevTools version 2.23.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. [!] Flutter (Channel master, 3.11.0-6.0.pre.63, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.11.0-6.0.pre.63 on channel master at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 7b8f7a2a64 (7 hours ago), 2023-05-14 17:42:29 -0400 • Engine revision 326de1d849 • Dart version 3.1.0 (build 3.1.0-110.0.dev) • DevTools version 2.23.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/dhs/Library/Android/sdk ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 13C100 • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2) • IntelliJ at /Applications/IntelliJ IDEA.app • Flutter plugin version 65.1.4 • Dart plugin version 213.7228 [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.29.0 [✓] Connected device (3 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 15.3.1 19D52 • macOS (desktop) • macos • darwin-x64 • macOS 12.2.1 21D62 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
danielmessi13 commented 7 months ago

same issue here, any updates ?

Taha-Firoz commented 6 months ago

@danielmessi13 Looks like this isn't happening anytime soon since the testing framework go_router is using can't test the RouteBuilderException, some part of either the framework or flutter catches the error before the router, I think.

Anyway the workaround mentioned by chuntai is

•••

I'm throwing custom exceptions depending on specific events that can occur during the build of the route

I think the best bet is to do a context.go when the condition is met. probably in a postframecallback. or just build a different widget base on that.