From 34c97cf7ec79f1fda13e11fe4fe8a82cfc6be7b2 Mon Sep 17 00:00:00 2001 From: NBA2K1 <78034913+NBA2K1@users.noreply.github.com> Date: Mon, 11 May 2026 19:45:27 +0200 Subject: [PATCH] Add graceful handling for database init failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Wrap `storage.initDB` in try/catch - Log initialization errors with stack trace - Show a minimal `_StartupErrorApp` when startup fails - Skip post‑launch initialization if DB init did not succeed - Introduce simple error UI with selectable error text --- lib/main.dart | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 54f41ed8..16343102 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -113,9 +113,19 @@ void main(List args) async { } 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? startupError; + try { + isar = await storage.initDB(null, inspector: kDebugMode); + } catch (e, st) { + AppLogger.log('DB init failed: $e\n$st', logLevel: LogLevel.error); + startupError = e; + } + runApp( + startupError != null + ? _StartupErrorApp(error: startupError.toString()) + : ProviderScope(child: MyApp(), retry: (retryCount, error) => null), + ); + if (startupError == null) unawaited(_postLaunchInit(storage)); }, (Object error, StackTrace stack) { AppLogger.log( @@ -126,6 +136,40 @@ void main(List args) async { ); } +class _StartupErrorApp extends StatelessWidget { + final String error; + const _StartupErrorApp({required this.error}); + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + body: Center( + child: Padding( + padding: const EdgeInsets.all(24), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Icon(Icons.error_outline, size: 64, color: Colors.red), + const SizedBox(height: 16), + const Text( + 'Failed to start Mangayomi', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 8), + SelectableText( + error, + style: const TextStyle(fontFamily: 'monospace', fontSize: 12), + ), + ], + ), + ), + ), + ), + ); + } +} + Future _postLaunchInit(StorageProvider storage) async { await AppLogger.init(); unawaited(MDownloader.initializeIsolatePool(poolSize: 6));