From 7722013125f8519669c89b9bdea72d3cd6ef22ad Mon Sep 17 00:00:00 2001 From: Blake Burkhart Date: Tue, 2 Nov 2021 20:37:30 -0500 Subject: [PATCH] Improve error handling --- Playlist.cs | 36 ++++++++++++++++++++++-------------- PlaylistViewModel.cs | 5 +++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Playlist.cs b/Playlist.cs index daaa390..a6e0688 100644 --- a/Playlist.cs +++ b/Playlist.cs @@ -95,22 +95,30 @@ private void UpdatePlaylistFile() public override void OnApplicationStarted(OnApplicationStartedEventArgs args) { - // Initialization is done inside OnApplicationStarted, otherwise - // loadPlaylistFile runs too early in Playnite's startup and - // cannot call PlayniteApi.Database.Games.Get() - - PlaylistGames = new ObservableCollection(LoadPlaylistFile()); - PlaylistGames.CollectionChanged += (sender, changedArgs) => - { - UpdatePlaylistFile(); - }; - PlayniteApi.Database.Games.ItemCollectionChanged += (sender, changedArgs) => + try { - foreach (Game game in changedArgs.RemovedItems) + // Initialization is done inside OnApplicationStarted, otherwise + // loadPlaylistFile runs too early in Playnite's startup and + // cannot call PlayniteApi.Database.Games.Get() + + PlaylistGames = new ObservableCollection(LoadPlaylistFile()); + PlaylistGames.CollectionChanged += (sender, changedArgs) => { - PlaylistGames.Remove(game); - } - }; + UpdatePlaylistFile(); + }; + PlayniteApi.Database.Games.ItemCollectionChanged += (sender, changedArgs) => + { + foreach (Game game in changedArgs.RemovedItems) + { + PlaylistGames.Remove(game); + } + }; + } + catch (Exception e) + { + logger.Error(e, "Error loading PlaylistGames in OnApplicationStarted"); + PlayniteApi.Notifications.Add($"{Id}-OnApplicationStarted", $"Playlist extension could not load file: {e.Message}", NotificationType.Error); + } } } } \ No newline at end of file diff --git a/PlaylistViewModel.cs b/PlaylistViewModel.cs index 42b969c..c1469ff 100644 --- a/PlaylistViewModel.cs +++ b/PlaylistViewModel.cs @@ -1,5 +1,6 @@ using Playnite.SDK; using Playnite.SDK.Models; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -49,8 +50,8 @@ public IEnumerable playlistGames, IPlayniteAPI playniteApi) { - PlaylistGames = playlistGames; - this.playniteApi = playniteApi; + PlaylistGames = playlistGames ?? throw new ArgumentNullException(nameof(playlistGames)); + this.playniteApi = playniteApi ?? throw new ArgumentNullException(nameof(playniteApi)); NavigateBackCommand = new RelayCommand((a) => {