Skip to content

Commit

Permalink
Handle remote provider errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Scepheo committed Jun 17, 2019
1 parent 65bb325 commit 0dd2757
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/GitMan/Context.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GitMan.Actions;
using GitMan.Clients;
using GitMan.Providers;
using GitMan.Config;
using System;
using System.Collections.Generic;
Expand Down
19 changes: 16 additions & 3 deletions src/GitMan/Providers/AzureProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text;
using System.Text.Json;

namespace GitMan.Clients
namespace GitMan.Providers
{
internal class AzureProvider : RemoteProvider
{
Expand Down Expand Up @@ -94,7 +94,20 @@ private JsonDocument GetResponse(string path)
using var response = client.GetAsync(uri).Result;
var json = response.Content.ReadAsStringAsync().Result;
var document = JsonDocument.Parse(json);
return document;

if (response.IsSuccessStatusCode)
{
return document;
}
else
{
var statusCode = response.StatusCode;
var message = document.RootElement.TryGetProperty("message", out var messageProperty)
? messageProperty.GetString()
: "An unknown error occurred";
var exception = new RemoteProviderException(statusCode, message);
throw exception;
}
}

public override RemoteRepository[] GetRepositories()
Expand All @@ -105,7 +118,7 @@ public override RemoteRepository[] GetRepositories()
var count = repositories.GetArrayLength();
var azureRepos = new RemoteRepository[count];
var index = 0;

foreach (var repository in repositories.EnumerateArray())
{
var name = repository.GetProperty("name").GetString();
Expand Down
17 changes: 15 additions & 2 deletions src/GitMan/Providers/GitHubProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using System.Text.Json;

namespace GitMan.Clients
namespace GitMan.Providers
{
internal class GitHubProvider : RemoteProvider
{
Expand Down Expand Up @@ -65,7 +65,20 @@ private JsonDocument GetResponse(string path)
using var response = client.GetAsync(uri).Result;
var json = response.Content.ReadAsStringAsync().Result;
var document = JsonDocument.Parse(json);
return document;

if (response.IsSuccessStatusCode)
{
return document;
}
else
{
var statusCode = response.StatusCode;
var message = document.RootElement.TryGetProperty("message", out var messageProperty)
? messageProperty.GetString()
: "An unknown error occurred";
var exception = new RemoteProviderException(statusCode, message);
throw exception;
}
}

public override RemoteRepository[] GetRepositories()
Expand Down
54 changes: 41 additions & 13 deletions src/GitMan/Providers/RemoteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Windows.Forms;

namespace GitMan.Clients
namespace GitMan.Providers
{
internal abstract class RemoteProvider
{
Expand All @@ -22,8 +22,10 @@ public MenuItem MakeRemoteProviderItem(
string repositoryFolder,
RepositoryDirectory existingRepositories)
{
var loadItem = new MenuItem("Loading...");
var dummyItems = new[] { loadItem };
// This _should_ never be shown to the user, and is only here to
// ensure the little drop-down arrow shows up
var dummyItem = new MenuItem("<DUMMY>");
var dummyItems = new[] { dummyItem };

MenuItem menuItem = default;

Expand All @@ -36,18 +38,44 @@ void onPopup(object sender, EventArgs eventArgs)
var originalCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

var repositories = GetRepositories()
.Where(repository => !existingRepositories.Any(existing => existing.Name == repository.Name))
.OrderBy(repository => repository.Name);
var trying = true;

var menuItems = repositories
.Select(repository => repository.GetMenuItem(
repositoryFolder,
DefaultConfig))
.ToArray();
while (trying)
{
try
{
var repositories = GetRepositories()
.Where(repository => !existingRepositories.Any(existing => existing.Name == repository.Name))
.OrderBy(repository => repository.Name);

menuItem.MenuItems.Clear();
menuItem.MenuItems.AddRange(menuItems);
var menuItems = repositories
.Select(repository => repository.GetMenuItem(
repositoryFolder,
DefaultConfig))
.ToArray();

menuItem.MenuItems.Clear();
menuItem.MenuItems.AddRange(menuItems);
}
catch (Exception exception)
{
var text = exception.Message;
const string caption = "An error occurred";
var buttons = MessageBoxButtons.RetryCancel;
var icon = MessageBoxIcon.Error;

var response = MessageBox.Show(text, caption, buttons, icon);

if (response == DialogResult.Retry)
{
continue;
}
else
{
break;
}
}
}

Cursor.Current = originalCursor;
}
Expand Down
12 changes: 12 additions & 0 deletions src/GitMan/Providers/RemoteProviderException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Net;

namespace GitMan.Providers
{
internal class RemoteProviderException : Exception
{
public RemoteProviderException(HttpStatusCode statusCode, string message)
: base($"Remove provider returned {(int)statusCode} - {statusCode}:\n{message}")
{ }
}
}
2 changes: 1 addition & 1 deletion src/GitMan/Providers/RemoteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Windows.Forms;

namespace GitMan.Clients
namespace GitMan.Providers
{
internal class RemoteRepository
{
Expand Down

0 comments on commit 0dd2757

Please sign in to comment.