Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 687e162

Browse files
Adding an extension method to easily parse error api output
1 parent 804a815 commit 687e162

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,7 @@ private async Task<GitHubRepository> CreateRepositoryInternal(string repositoryN
247247
};
248248
}
249249

250-
if (ret.Output.Any())
251-
{
252-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
253-
}
254-
255-
throw new ApiClientException("Publish failed");
250+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Publish failed");
256251
}
257252
catch (Exception ex)
258253
{
@@ -294,12 +289,7 @@ private async Task GetOrganizationInternal(Action<Organization[]> onSuccess, Act
294289
return;
295290
}
296291

297-
if (ret.Output.Any())
298-
{
299-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
300-
}
301-
302-
throw new ApiClientException("Error getting organizations");
292+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error getting organizations");
303293
}
304294
catch (Exception ex)
305295
{
@@ -332,12 +322,7 @@ private async Task<GitHubUser> GetCurrentUserInternal()
332322
};
333323
}
334324

335-
if (ret.Output.Any())
336-
{
337-
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
338-
}
339-
340-
throw new ApiClientException("Error validating current user");
325+
throw new ApiClientException(ret.GetApiErrorMessage() ?? "Error validating current user");
341326
}
342327
catch (KeychainEmptyException)
343328
{

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ private async Task<LoginResultData> TryLogin(
193193
return new LoginResultData(resultCodes, message, host, ret.Output[0]);
194194
}
195195

196-
if (ret.Output.Any())
197-
{
198-
return new LoginResultData(LoginResultCodes.Failed, ret.Output[0], host);
199-
}
200-
201-
return new LoginResultData(LoginResultCodes.Failed, "Failed.", host);
196+
return new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host);
202197
}
203198
}
204199

src/GitHub.Api/Tasks/OctorunTask.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Text.RegularExpressions;
67
using System.Threading;
78
using GitHub.Logging;
89

@@ -133,4 +134,19 @@ public OctorunResult(string status, string[] output)
133134
public bool IsError => Status.Equals("error", StringComparison.InvariantCultureIgnoreCase);
134135
public bool IsTwoFactorRequired => Status.Equals("2fa", StringComparison.InvariantCultureIgnoreCase);
135136
}
137+
138+
static class OctorunResultExtensions {
139+
private static Regex ApiErrorMessageRegex = new Regex(@"\""message\"":\""(.*?)\""", RegexOptions.Compiled);
140+
141+
internal static string GetApiErrorMessage(this OctorunResult octorunResult)
142+
{
143+
if (!octorunResult.IsError || !octorunResult.Output.Any())
144+
{
145+
return null;
146+
}
147+
148+
var match = ApiErrorMessageRegex.Match(octorunResult.Output[0]);
149+
return match.Success ? match.Groups[1].Value : octorunResult.Output[0];
150+
}
151+
}
136152
}

0 commit comments

Comments
 (0)