Skip to content

Commit

Permalink
Include HTTP status in ContainerHttpException (#41041)
Browse files Browse the repository at this point in the history
  • Loading branch information
martincostello authored May 21, 2024
1 parent ace21a6 commit c7e020f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
catch (HttpRequestException e) when (e.InnerException is IOException ioe && ioe.Message.Equals("The response ended prematurely.", StringComparison.OrdinalIgnoreCase))
{
string message = Resource.GetString(nameof(Strings.AmazonRegistryFailed));
throw new ContainerHttpException(message, request.RequestUri?.ToString());
throw new ContainerHttpException(message, request.RequestUri?.ToString(), e.StatusCode);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;

namespace Microsoft.NET.Build.Containers;

internal sealed class ContainerHttpException : Exception
{
private const string ErrorPrefix = "Containerize: error CONTAINER004:";
string? uri;
public ContainerHttpException(string message, string? targetUri)
: base($"{ErrorPrefix} {message}\nURI: {targetUri ?? "Unknown"}")
public ContainerHttpException(string message, string? targetUri, HttpStatusCode? statusCode)
: base($"{ErrorPrefix} {message}\nURI: {targetUri ?? "Unknown"}\nHTTP status code: {statusCode?.ToString() ?? "Unknown"}")
{
uri = targetUri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ private async Task<HttpResponseMessage> GetAsync(string repositoryName, string d
private async Task<T> LogAndThrowContainerHttpException<T>(HttpResponseMessage response, CancellationToken cancellationToken)
{
await response.LogHttpResponseAsync(_logger, cancellationToken).ConfigureAwait(false);
throw new ContainerHttpException(Resource.GetString(nameof(Strings.RegistryPullFailed)), response.RequestMessage?.RequestUri?.ToString());
throw new ContainerHttpException(Resource.GetString(nameof(Strings.RegistryPullFailed)), response.RequestMessage?.RequestUri?.ToString(), response.StatusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public async Task PutAsync(string repositoryName, string reference, ManifestV2 m
if (!putResponse.IsSuccessStatusCode)
{
await putResponse.LogHttpResponseAsync(_logger, cancellationToken).ConfigureAwait(false);
throw new ContainerHttpException(Resource.FormatString(nameof(Strings.RegistryPushFailed), putResponse.StatusCode), putResponse.RequestMessage?.RequestUri?.ToString());
throw new ContainerHttpException(Resource.FormatString(nameof(Strings.RegistryPushFailed), putResponse.StatusCode), putResponse.RequestMessage?.RequestUri?.ToString(), putResponse.StatusCode);
}
}

private async Task<T> LogAndThrowContainerHttpException<T>(HttpResponseMessage response, CancellationToken cancellationToken)
{
await response.LogHttpResponseAsync(_logger, cancellationToken).ConfigureAwait(false);
throw new ContainerHttpException(Resource.GetString(nameof(Strings.RegistryPullFailed)), response.RequestMessage?.RequestUri?.ToString());
throw new ContainerHttpException(Resource.GetString(nameof(Strings.RegistryPullFailed)), response.RequestMessage?.RequestUri?.ToString(), response.StatusCode);
}
}

0 comments on commit c7e020f

Please sign in to comment.