Skip to content

Commit

Permalink
feat: Improve some error handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa committed Sep 22, 2024
1 parent 1192b67 commit 2ef2cc4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
25 changes: 17 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ Future<void> main(List<String> args) async {

break;
}
} catch (error, stack) {
debugPrint('error: $error');
debugPrintStack(stackTrace: stack);
} catch (error, stackTrace) {
logging.handleError(
error,
stackTrace,
'Failed to open a secondary window',
);
}

return;
Expand Down Expand Up @@ -178,9 +181,11 @@ Future<void> main(List<String> args) async {
try {
FirebaseConfiguration.ensureInitialized();
} catch (error, stackTrace) {
debugPrint('Error initializing firebase messaging: $error');
debugPrintStack(stackTrace: stackTrace);
logging.handleError(error, stackTrace);
logging.handleError(
error,
stackTrace,
'Error initializing firebase messaging',
);
}
}

Expand Down Expand Up @@ -309,8 +314,12 @@ class _UnityAppState extends State<UnityApp>
debugPrint('Disposing player ${player.hashCode}');
try {
await player.dispose();
} catch (e) {
debugPrint('Error disposing player: $e');
} catch (error, stackTrace) {
logging.handleError(
error,
stackTrace,
'Error disposing player $player',
);
}
}
});
Expand Down
8 changes: 5 additions & 3 deletions lib/providers/app_provider_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ abstract class UnityProvider extends ChangeNotifier {
Future<void> reloadInterface() => initialize();

SafeLocalStorage? storage;
String? key;

@protected
Future<void> initializeStorage(SafeLocalStorage storage, String key) async {
this.key = key;
try {
this.storage = storage;
} catch (e) {
Expand All @@ -41,7 +43,7 @@ abstract class UnityProvider extends ChangeNotifier {
} catch (error, stackTrace) {
await save();

handleError(error, stackTrace);
handleError(error, stackTrace, 'Failed to restore $key');
}
}

Expand All @@ -64,8 +66,8 @@ abstract class UnityProvider extends ChangeNotifier {
Future<void>? write(dynamic data) {
try {
return storage?.write(data);
} catch (error, stack) {
handleError(error, stack);
} catch (error, stackTrace) {
handleError(error, stackTrace, 'Failed to write data to $key');
return Future.value();
}
}
Expand Down
24 changes: 16 additions & 8 deletions lib/providers/settings_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ class _SettingsOption<T> {
if (getDefault != null) serializedData ??= saveAs(await getDefault!());
serializedData ??= defAsString;
_value = loadFrom(serializedData);
} catch (e) {
debugPrint('Error loading data for $key: $e\nFallback to default');
} catch (error, stackTrace) {
handleError(
error,
stackTrace,
'Error loading data for $key. Fallback to default value',
);
_value = (await getDefault?.call()) ?? def;
}
}
Expand Down Expand Up @@ -587,9 +591,12 @@ class SettingsProvider extends UnityProvider {
Future<void> initialize() async {
try {
await initializeStorage(settings, 'settings');
} catch (_, __) {
debugPrint('Error initializing settings storage. Fallback to memory');
// handleError(error, stackTrace);
} catch (error, stackTrace) {
handleError(
error,
stackTrace,
'Error initializing settings storage. Fallback to memory',
);
}
final data = await tryReadStorage(() => settings.read());

Expand All @@ -608,10 +615,11 @@ class SettingsProvider extends UnityProvider {
try {
return <String, String>{setting.key: setting.saveAs(setting.value)};
} catch (error, stackTrace) {
debugPrint(
'Error saving setting ${setting.key}: $error\n$stackTrace',
handleError(
error,
stackTrace,
'Error saving setting ${setting.key}',
);
handleError(error, stackTrace);
}
return <String, String>{};
}(),
Expand Down

0 comments on commit 2ef2cc4

Please sign in to comment.