Skip to content

Commit

Permalink
Handle GQL error response (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArinGhazarian authored Jun 28, 2022
1 parent 56019ae commit 027a1e0
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Octoshift/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ mutation startRepositoryMigration(
var response = await _client.PostAsync(url, payload);
var data = JObject.Parse(response);

EnsureSuccessGraphQLResponse(data);

return (string)data["data"]["startRepositoryMigration"]["repositoryMigration"]["id"];
}

Expand Down Expand Up @@ -617,5 +619,15 @@ private static Mannequin BuildMannequin(JToken mannequin)
: null
};
}

private void EnsureSuccessGraphQLResponse(JObject response)
{
if (response.TryGetValue("errors", out var jErrors) && jErrors is JArray { Count: > 0 } errors)
{
var error = (JObject)errors[0];
var errorMessage = error.TryGetValue("message", out var jMessage) ? (string)jMessage : null;
throw new OctoshiftCliException($"{errorMessage ?? "UNKNOWN"}");
}
}
}
}
120 changes: 120 additions & 0 deletions src/OctoshiftCLI.Tests/GithubApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,126 @@ mutation startRepositoryMigration(
expectedRepositoryMigrationId.Should().Be(actualRepositoryMigrationId);
}

[Fact]
public async Task StartMigration_Throws_When_GraphQL_Response_Has_Errors()
{
// Arrange
const string expectedErrorMessage = "Please make sure that githubPat includes the workflow scope";
const string response = $@"
{{
""data"": {{
""startRepositoryMigration"": null
}},
""errors"": [
{{
""type"": ""FORBIDDEN"",
""path"": [
""startRepositoryMigration""
],
""locations"": [
{{
""line"": 13,
""column"": 17
}}
],
""message"": ""{expectedErrorMessage}""
}}
]
}}";

_githubClientMock
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(response);

// Act, Assert
await _githubApi.Invoking(api => api.StartMigration(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()))
.Should()
.ThrowAsync<OctoshiftCliException>()
.WithMessage(expectedErrorMessage);
}

[Fact]
public async Task StartMigration_Does_Not_Include_Error_Message_If_Missing()
{
// Arrange
const string response = @"
{
""data"": {
""startRepositoryMigration"": null
},
""errors"": [
{
""type"": ""FORBIDDEN"",
""path"": [
""startRepositoryMigration""
],
""locations"": [
{
""line"": 13,
""column"": 17
}
]
}
]
}";

const string expectedErrorMessage = "UNKNOWN";

_githubClientMock
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(response);

// Act, Assert
await _githubApi.Invoking(api => api.StartMigration(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()))
.Should()
.ThrowAsync<OctoshiftCliException>()
.WithMessage(expectedErrorMessage);
}

[Fact]
public async Task StartMigration_Does_Not_Throw_When_Errors_Is_Empty()
{
// Arrange
const string response = @"
{
""data"": {
""startRepositoryMigration"": {
""repositoryMigration"": {
""id"": ""RM_kgC4NjFhNmE2NGU2ZWE1YTQwMDA5ODliZjhi""
}
}
},
""errors"": []
}";

_githubClientMock
.Setup(m => m.PostAsync(It.IsAny<string>(), It.IsAny<object>()))
.ReturnsAsync(response);

// Act, Assert
await _githubApi.Invoking(api => api.StartMigration(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()))
.Should()
.NotThrowAsync<OctoshiftCliException>();
}

[Fact]
public async Task GetMigration_Returns_The_Migration_State_And_Repository_Name()
{
Expand Down

0 comments on commit 027a1e0

Please sign in to comment.