Add global error handling in main function

This commit is contained in:
Moustapha Kodjo Amadou 2026-03-02 11:55:19 +01:00
parent 1256e608c7
commit a5fd40fdfb

View file

@ -54,32 +54,62 @@ String? customDns;
void main(List<String> args) async { void main(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
if (Platform.isLinux && runWebViewTitleBarWidget(args)) return; if (Platform.isLinux && runWebViewTitleBarWidget(args)) return;
MediaKit.ensureInitialized();
await RustLib.init(); // Widget-layer errors (build / layout / paint)
await imgCropIsolate.start(); FlutterError.onError = (FlutterErrorDetails details) {
await getIsolateService.start(); FlutterError.presentError(details); // keep default red-screen in debug
if (!(Platform.isAndroid || Platform.isIOS)) { AppLogger.log(
await windowManager.ensureInitialized(); 'FlutterError: ${details.exceptionAsString()}\n${details.stack}',
} logLevel: LogLevel.error,
if (Platform.isWindows) { );
registerProtocolHandler("mangayomi"); };
}
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) { // Async errors that escape the Flutter framework (PlatformDispatcher)
final availableVersion = await WebViewEnvironment.getAvailableVersion(); PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
if (availableVersion != null) { AppLogger.log(
final document = await getApplicationDocumentsDirectory(); 'PlatformDispatcher error: $error\n$stack',
webViewEnvironment = await WebViewEnvironment.create( logLevel: LogLevel.error,
settings: WebViewEnvironmentSettings( );
userDataFolder: p.join(document.path, 'flutter_inappwebview'), return true; // handled prevent app termination
), };
// Zone-level catch-all for anything that slips through both layers
runZonedGuarded(
() async {
MediaKit.ensureInitialized();
await RustLib.init();
await imgCropIsolate.start();
await getIsolateService.start();
if (!(Platform.isAndroid || Platform.isIOS)) {
await windowManager.ensureInitialized();
}
if (Platform.isWindows) {
registerProtocolHandler("mangayomi");
}
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
final availableVersion = await WebViewEnvironment.getAvailableVersion();
if (availableVersion != null) {
final document = await getApplicationDocumentsDirectory();
webViewEnvironment = await WebViewEnvironment.create(
settings: WebViewEnvironmentSettings(
userDataFolder: p.join(document.path, 'flutter_inappwebview'),
),
);
}
}
final storage = StorageProvider();
await storage.requestPermission();
isar = await storage.initDB(null, inspector: kDebugMode);
runApp(ProviderScope(child: MyApp(), retry: (retryCount, error) => null));
unawaited(_postLaunchInit(storage));
},
(Object error, StackTrace stack) {
AppLogger.log(
'runZonedGuarded error: $error\n$stack',
logLevel: LogLevel.error,
); );
} },
} );
final storage = StorageProvider();
await storage.requestPermission();
isar = await storage.initDB(null, inspector: kDebugMode);
runApp(ProviderScope(child: MyApp(), retry: (retryCount, error) => null));
unawaited(_postLaunchInit(storage)); // Defer non-essential async operations
} }
Future<void> _postLaunchInit(StorageProvider storage) async { Future<void> _postLaunchInit(StorageProvider storage) async {