diff --git a/src/Containers/Microsoft.NET.Build.Containers/AmazonECRMessageHandler.cs b/src/Containers/Microsoft.NET.Build.Containers/AmazonECRMessageHandler.cs index afdaf77e5152..a5bbe038684f 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/AmazonECRMessageHandler.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/AmazonECRMessageHandler.cs @@ -26,7 +26,7 @@ protected override async Task 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 { diff --git a/src/Containers/Microsoft.NET.Build.Containers/Exceptions/ContainerHttpException.cs b/src/Containers/Microsoft.NET.Build.Containers/Exceptions/ContainerHttpException.cs index 50033808efe0..e4a7e5a0728a 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Exceptions/ContainerHttpException.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Exceptions/ContainerHttpException.cs @@ -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; } diff --git a/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultBlobOperations.cs b/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultBlobOperations.cs index d258ebfc0886..e443394e5534 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultBlobOperations.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultBlobOperations.cs @@ -76,6 +76,6 @@ private async Task GetAsync(string repositoryName, string d private async Task LogAndThrowContainerHttpException(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); } } diff --git a/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultManifestOperations.cs b/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultManifestOperations.cs index 994f97162551..a712de302604 100644 --- a/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultManifestOperations.cs +++ b/src/Containers/Microsoft.NET.Build.Containers/Registry/DefaultManifestOperations.cs @@ -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 LogAndThrowContainerHttpException(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); } }