diff --git a/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs b/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs index 83c2c86..ab2a418 100644 --- a/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs +++ b/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs @@ -82,7 +82,15 @@ public LibraryForm(OnlineLibrary library) async Task RefreshAsync() { - await _library.UpdateLibraryAsync(); + try + { + await _library.UpdateLibraryAsync(); + } + catch (Exception) + { + MessageBox.Show(this, $"Error refreshing list of libraries. It's possible you reached your refresh limit, please wait one hour for the rate limit to reset.", MessageBoxType.Error); + } + var values = _library.Libraries.Values; var ordered = values.OrderBy(i => i.Name).ToList(); @@ -105,15 +113,24 @@ async Task DownloadAsync() { var item = (LibraryItem)_detailView.DataContext; - var success = item switch + try { - { IsUpdateAvailable: true } => await _library.TryDownloadLibraryAsync(item), - { IsDownloaded: true } => _library.TryRemoveDownloadedLibrary(item), - _ => throw new ArgumentException("Invalid action") - }; - - if (!success) - MessageBox.Show(this, $"{ItemActions(item)} error on {item.Name}", MessageBoxType.Error); + switch (item) + { + case { IsUpdateAvailable: true }: + await _library.DownloadLibraryAsync(item); + break; + case { IsDownloaded: true }: + _library.RemoveDownloadedLibrary(item); + break; + default: + throw new ArgumentException("Invalid action"); + } + } + catch (Exception e) + { + MessageBox.Show(this, $"{ItemActions(item)} error on {item.Name}.\n\n{e.Message}", MessageBoxType.Error); + } _detailView.UpdateBindings(BindingUpdateMode.Destination); _grid.ReloadData(_grid.SelectedRow); diff --git a/src/Robots/IO/OnlineLibrary.cs b/src/Robots/IO/OnlineLibrary.cs index bc453fb..f89d7cd 100644 --- a/src/Robots/IO/OnlineLibrary.cs +++ b/src/Robots/IO/OnlineLibrary.cs @@ -1,5 +1,5 @@ -using Octokit; -using System.Security.Cryptography; +using System.Security.Cryptography; +using Octokit; namespace Robots; @@ -34,47 +34,35 @@ public async Task UpdateLibraryAsync() AddDiskLibraries(FileIO.LocalLibraryPath, true); } - public async Task TryDownloadLibraryAsync(LibraryItem library) + public async Task DownloadLibraryAsync(LibraryItem library) { if (!library.IsUpdateAvailable) throw new ArgumentException("Library does not require update."); - if (!await TryDownloadFileAsync(library.Name + ".xml")) - return false; - - if (!await TryDownloadFileAsync(library.Name + ".3dm")) - return false; + await DownloadFileAsync(library.Name + ".xml"); + await DownloadFileAsync(library.Name + ".3dm"); var xmlPath = Path.Combine(FileIO.OnlineLibraryPath, library.Name + ".xml"); var sha = GetLocalSha(xmlPath); if (sha != library.OnlineSha) - return false; + throw new InvalidDataException("Downloaded file does not match online file."); library.DownloadedSha = sha; LibraryChanged?.Invoke(); - return true; } - public bool TryRemoveDownloadedLibrary(LibraryItem item) + public void RemoveDownloadedLibrary(LibraryItem item) { var folder = FileIO.OnlineLibraryPath; string pathXml = Path.Combine(folder, item.Name + ".xml"); string path3dm = Path.Combine(folder, item.Name + ".3dm"); - try - { - File.Delete(pathXml); - File.Delete(path3dm); - } - catch (Exception) - { - return false; - } + File.Delete(pathXml); + File.Delete(path3dm); item.DownloadedSha = null; LibraryChanged?.Invoke(); - return true; } async Task AddOnlineLibrariesAsync() @@ -171,7 +159,7 @@ string GetSha1(byte[] contentBytes) return hashText; } - async Task TryDownloadFileAsync(string fileName) + async Task DownloadFileAsync(string fileName) { var folder = FileIO.OnlineLibraryPath; @@ -180,13 +168,9 @@ async Task TryDownloadFileAsync(string fileName) string downloadPath = Path.Combine(folder, fileName); var response = await _http.GetAsync(fileName); - - if (response.StatusCode != System.Net.HttpStatusCode.OK) - return false; + response.EnsureSuccessStatusCode(); var bytes = await response.Content.ReadAsByteArrayAsync(); File.WriteAllBytes(downloadPath, bytes); - - return true; } } \ No newline at end of file