Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StatusCode to HttpRequestException #32455

Merged
merged 12 commits into from
Feb 24, 2020
4 changes: 3 additions & 1 deletion src/libraries/System.Net.Http/ref/System.Net.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public abstract partial class HttpContent : System.IDisposable
protected HttpContent() { }
public System.Net.Http.Headers.HttpContentHeaders Headers { get { throw null; } }
public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream stream) { throw null; }
public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream stream, System.Net.TransportContext context) { throw null; }
public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream stream, System.Net.TransportContext context, System.Threading.CancellationToken cancellationToken) { throw null; }
public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken) { throw null; }
protected virtual System.Threading.Tasks.Task<System.IO.Stream> CreateContentReadStreamAsync() { throw null; }
protected virtual System.Threading.Tasks.Task<System.IO.Stream> CreateContentReadStreamAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public void Dispose() { }
Expand Down Expand Up @@ -186,6 +186,8 @@ public partial class HttpRequestException : System.Exception
public HttpRequestException() { }
public HttpRequestException(string message) { }
public HttpRequestException(string message, System.Exception inner) { }
public HttpRequestException(string message, System.Exception inner, System.Net.HttpStatusCode? statusCode) { }
public System.Net.HttpStatusCode? StatusCode { get { throw null; } }
}
public partial class HttpRequestMessage : System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public HttpRequestException(string message, Exception inner)
}
}

/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestException" /> class with a specific message that describes the current exception, an inner exception, and an HTTP status code.
/// </summary>
/// <param name="message">A message that describes the current exception.</param>
/// <param name="inner">The inner exception.</param>
/// <param name="statusCode">The HTTP status code.</param>
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
public HttpRequestException(string message, Exception inner, HttpStatusCode? statusCode)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
: this(message, inner)
{
StatusCode = statusCode;
}

/// <summary>
/// The HTTP status code to be returned with the exception.
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public HttpStatusCode? StatusCode { get; }
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

// This constructor is used internally to indicate that a request was not successfully sent due to an IOException,
// and the exception occurred early enough so that the request may be retried on another connection.
internal HttpRequestException(string message, Exception inner, RequestRetryType allowRetry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ public HttpResponseMessage EnsureSuccessStatusCode()
{
if (!IsSuccessStatusCode)
{
throw new HttpRequestException(SR.Format(
System.Globalization.CultureInfo.InvariantCulture,
SR.net_http_message_not_success_statuscode,
(int)_statusCode,
ReasonPhrase));
throw new HttpRequestException(
SR.Format(
System.Globalization.CultureInfo.InvariantCulture,
SR.net_http_message_not_success_statuscode,
(int)_statusCode,
ReasonPhrase),
null,
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
_statusCode);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ public void EnsureSuccessStatusCode_VariousStatusCodes_ThrowIfNot2xx()
{
using (var m = new HttpResponseMessage(HttpStatusCode.MultipleChoices))
{
Assert.Throws<HttpRequestException>(() => m.EnsureSuccessStatusCode());
var ex = Assert.Throws<HttpRequestException>(() => m.EnsureSuccessStatusCode());
Assert.Equal(HttpStatusCode.MultipleChoices, ex.StatusCode);
}

using (var m = new HttpResponseMessage(HttpStatusCode.BadGateway))
{
Assert.Throws<HttpRequestException>(() => m.EnsureSuccessStatusCode());
var ex = Assert.Throws<HttpRequestException>(() => m.EnsureSuccessStatusCode());
Assert.Equal(HttpStatusCode.BadGateway, ex.StatusCode);
}

using (var response = new HttpResponseMessage(HttpStatusCode.OK))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Net.Http.Tests
{
public class HttpRequestExceptionTests
{
[Fact]
public void DefaultConstructors_HasNoStatusCode()
{
var exception = new HttpRequestException();
Assert.Null(exception.StatusCode);

exception = new HttpRequestException("message");
Assert.Null(exception.StatusCode);

exception = new HttpRequestException("message", new InvalidOperationException());
Assert.Null(exception.StatusCode);
}

[Fact]
public void StoresStatusCode()
{
var exception = new HttpRequestException("message", null, HttpStatusCode.InternalServerError);
Assert.Equal(HttpStatusCode.InternalServerError, exception.StatusCode);
}

[Fact]
public void StoresNonStandardStatusCode()
{
var statusCode = (HttpStatusCode)999;

var exception = new HttpRequestException("message", null, statusCode);
Assert.Equal(statusCode, exception.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
<Compile Include="MockContent.cs" />
<Compile Include="StreamToStreamCopyTest.cs" />
<Compile Include="HttpEnvironmentProxyTest.cs" />
<Compile Include="HttpRequestExceptionTests.cs" />
<Compile Include="HttpWindowsProxyTest.cs" />
<Compile Include="SystemProxyInfoTest.cs" />
<Compile Include="..\..\src\System\Net\Http\SocketsHttpHandler\HttpNoProxy.cs">
Expand Down