Skip to content

Commit

Permalink
Move catch exceptions to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Jan 21, 2022
1 parent e4bfa25 commit 34a3da1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
35 changes: 26 additions & 9 deletions src/Robots.Grasshopper/RobotSystem/LibraryForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down
38 changes: 11 additions & 27 deletions src/Robots/IO/OnlineLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Octokit;
using System.Security.Cryptography;
using System.Security.Cryptography;
using Octokit;

namespace Robots;

Expand Down Expand Up @@ -34,47 +34,35 @@ public async Task UpdateLibraryAsync()
AddDiskLibraries(FileIO.LocalLibraryPath, true);
}

public async Task<bool> 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()
Expand Down Expand Up @@ -171,7 +159,7 @@ string GetSha1(byte[] contentBytes)
return hashText;
}

async Task<bool> TryDownloadFileAsync(string fileName)
async Task DownloadFileAsync(string fileName)
{
var folder = FileIO.OnlineLibraryPath;

Expand All @@ -180,13 +168,9 @@ async Task<bool> 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;
}
}

0 comments on commit 34a3da1

Please sign in to comment.