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,6 +54,28 @@ String? customDns;
void main(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isLinux && runWebViewTitleBarWidget(args)) return;
// Widget-layer errors (build / layout / paint)
FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.presentError(details); // keep default red-screen in debug
AppLogger.log(
'FlutterError: ${details.exceptionAsString()}\n${details.stack}',
logLevel: LogLevel.error,
);
};
// Async errors that escape the Flutter framework (PlatformDispatcher)
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
AppLogger.log(
'PlatformDispatcher error: $error\n$stack',
logLevel: LogLevel.error,
);
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();
@ -79,7 +101,15 @@ void main(List<String> args) async {
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
unawaited(_postLaunchInit(storage));
},
(Object error, StackTrace stack) {
AppLogger.log(
'runZonedGuarded error: $error\n$stack',
logLevel: LogLevel.error,
);
},
);
}
Future<void> _postLaunchInit(StorageProvider storage) async {