Skip to content

Commit c30ba32

Browse files
committed
show response body when call to GitHub fails
1 parent fcb30a4 commit c30ba32

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

MarkdownLinkCheckLogParser/src/MarkdownLinkCheckLogParserCli/GitHub/Exceptions/GitHubHttpClientException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace MarkdownLinkCheckLogParserCli.GitHub.Exceptions;
22

33
public class GitHubHttpClientException : Exception
44
{
5-
internal GitHubHttpClientException(HttpStatusCode statusCode, HttpMethod method, Uri? requestUri)
6-
: base($"Failed to download workflow run logs. Got {(int)statusCode} {statusCode} from {method} {requestUri}.")
5+
internal GitHubHttpClientException(HttpMethod method, Uri? requestUri, HttpStatusCode statusCode, string responseBody)
6+
: base($"Failed to download workflow run logs. Got {(int)statusCode} {statusCode} from {method} {requestUri}.{Environment.NewLine}With response body:{Environment.NewLine}{responseBody}")
77
{
88
}
99
}

MarkdownLinkCheckLogParser/src/MarkdownLinkCheckLogParserCli/GitHub/GitHubHttpClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public async Task<ZipArchive> DownloadWorkflowRunLogsAsync(GitHubRepository repo
3232
var httpResponse = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
3333
if (!httpResponse.IsSuccessStatusCode)
3434
{
35-
throw new GitHubHttpClientException(httpResponse.StatusCode, httpRequest.Method, httpRequest.RequestUri);
35+
var errorResponseBody = await httpResponse.Content.ReadAsStringAsync();
36+
throw new GitHubHttpClientException(httpRequest.Method, httpRequest.RequestUri, httpResponse.StatusCode, errorResponseBody);
3637
}
3738

3839
var responseStream = await httpResponse.Content.ReadAsStreamAsync();

MarkdownLinkCheckLogParser/tests/MarkdownLinkCheckLogParserCli.Tests/Auxiliary/StatusCodeHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ namespace MarkdownLinkCheckLogParserCli.Tests.Auxiliary;
33
internal sealed class StatusCodeHandler : DelegatingHandler
44
{
55
private readonly HttpStatusCode _statusCode;
6+
private readonly string _responseBody;
67

7-
public StatusCodeHandler(HttpStatusCode statusCode)
8+
public StatusCodeHandler(HttpStatusCode statusCode, string responseBody)
89
{
910
_statusCode = statusCode;
11+
_responseBody = responseBody;
1012
}
1113

1214
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
1315
{
14-
var httpResponseMessage = new HttpResponseMessage(_statusCode);
16+
var httpResponseMessage = new HttpResponseMessage(_statusCode)
17+
{
18+
Content = new StringContent(_responseBody),
19+
};
1520
return Task.FromResult(httpResponseMessage);
1621
}
1722
}

MarkdownLinkCheckLogParser/tests/MarkdownLinkCheckLogParserCli.Tests/CliCommands/ParseLogCommandDependencyErrorTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class ParseLogCommandDependencyErrorTests
1616
[InlineData(HttpStatusCode.InternalServerError)]
1717
public async Task GitHubHttpClientFailsToDownloadLogs(HttpStatusCode gitHubHttpClientResponseStatusCode)
1818
{
19-
using var handler = new StatusCodeHandler(gitHubHttpClientResponseStatusCode);
19+
var errorResponseBody = "Oops, something went wrong.";
20+
using var handler = new StatusCodeHandler(gitHubHttpClientResponseStatusCode, errorResponseBody);
2021
using var httpClient = new HttpClient(handler)
2122
{
2223
BaseAddress = new Uri("https://api.github.com"),
@@ -34,11 +35,13 @@ public async Task GitHubHttpClientFailsToDownloadLogs(HttpStatusCode gitHubHttpC
3435
var exception = await Should.ThrowAsync<CommandException>(() => command.ExecuteAsync(console).AsTask());
3536
var expectedErrorMessage = @$"An error occurred trying to execute the command to parse the log from a Markdown link check step.
3637
Error:
37-
- Failed to download workflow run logs. Got {(int)gitHubHttpClientResponseStatusCode} {gitHubHttpClientResponseStatusCode} from GET https://api.github.com/repos/repo-name/actions/runs/run-id/logs.";
38+
- Failed to download workflow run logs. Got {(int)gitHubHttpClientResponseStatusCode} {gitHubHttpClientResponseStatusCode} from GET https://api.github.com/repos/repo-name/actions/runs/run-id/logs.
39+
With response body:
40+
{errorResponseBody}";
3841
exception.Message.ShouldBe(expectedErrorMessage);
3942
exception.InnerException.ShouldNotBeNull();
4043
exception.InnerException.ShouldBeAssignableTo<GitHubHttpClientException>();
41-
exception.InnerException.Message.ShouldBe($"Failed to download workflow run logs. Got {(int)gitHubHttpClientResponseStatusCode} {gitHubHttpClientResponseStatusCode} from GET https://api.github.com/repos/repo-name/actions/runs/run-id/logs.");
44+
exception.InnerException.Message.ShouldBe($"Failed to download workflow run logs. Got {(int)gitHubHttpClientResponseStatusCode} {gitHubHttpClientResponseStatusCode} from GET https://api.github.com/repos/repo-name/actions/runs/run-id/logs.{Environment.NewLine}With response body:{Environment.NewLine}{errorResponseBody}");
4245
}
4346

4447
/// <summary>

0 commit comments

Comments
 (0)