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

UnexpectedHttpStatusCodeException: more verbose message #333

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,13 @@ namespace EasyNetQ.Management.Client
public class UnexpectedHttpStatusCodeException : System.Exception
{
public UnexpectedHttpStatusCodeException() { }
public UnexpectedHttpStatusCodeException(System.Net.Http.HttpResponseMessage response) { }
public UnexpectedHttpStatusCodeException(System.Net.HttpStatusCode statusCode) { }
public UnexpectedHttpStatusCodeException(string message) { }
protected UnexpectedHttpStatusCodeException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public UnexpectedHttpStatusCodeException(string message, System.Exception inner) { }
protected UnexpectedHttpStatusCodeException(string message, System.Net.HttpStatusCode statusCode) { }
public System.Net.HttpStatusCode StatusCode { get; }
public int StatusCodeNumber { get; }
public static System.Threading.Tasks.Task<EasyNetQ.Management.Client.UnexpectedHttpStatusCodeException> FromHttpResponseMessageAsync(System.Net.Http.HttpResponseMessage response, System.Threading.CancellationToken cancellationToken = default) { }
}
}
namespace EasyNetQ.Management.Client.Model
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Net;
using System.Threading;

namespace EasyNetQ.Management.Client.Internals;

internal static class HttpResponseMessageExtensions
{
public static void EnsureExpectedStatusCode(this HttpResponseMessage response, Func<HttpStatusCode, bool> isExpected)
public static async Task EnsureExpectedStatusCodeAsync(this HttpResponseMessage response, Func<HttpStatusCode, bool> isExpected, CancellationToken cancellationToken = default)
{
if (!isExpected(response.StatusCode))
throw new UnexpectedHttpStatusCodeException(response);
throw await UnexpectedHttpStatusCodeException.FromHttpResponseMessageAsync(response, cancellationToken).ConfigureAwait(false);
}
}
16 changes: 8 additions & 8 deletions Source/EasyNetQ.Management.Client/ManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public async Task<bool> CheckAsync(
if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
return false;

throw new UnexpectedHttpStatusCodeException(response);
throw await UnexpectedHttpStatusCodeException.FromHttpResponseMessageAsync(response, cancellationToken).ConfigureAwait(false);
}

public async Task<TResult> GetAsync<TResult>(
Expand All @@ -194,7 +194,7 @@ public async Task<TResult> GetAsync<TResult>(
using var request = CreateRequest(HttpMethod.Get, path, queryParameters);
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode == HttpStatusCode.OK);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode == HttpStatusCode.OK, cancellationToken).ConfigureAwait(false);

try
{
Expand All @@ -217,7 +217,7 @@ public async Task<TResult> PostAsync<TItem, TResult>(
using var request = CreateRequest(HttpMethod.Post, path, null, requestContent);
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);

try
{
Expand All @@ -240,7 +240,7 @@ public async Task PostAsync<TItem>(
using var request = CreateRequest(HttpMethod.Post, path, null, requestContent);
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);
}

public async Task PostAsync(
Expand All @@ -251,15 +251,15 @@ public async Task PostAsync(
using var request = CreateRequest(HttpMethod.Post, path);
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);
}

public async Task DeleteAsync(RelativePath path, CancellationToken cancellationToken = default)
{
using var request = CreateRequest(HttpMethod.Delete, path);
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode == HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode == HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);
}

public async Task PutAsync<TBody>(
Expand All @@ -272,15 +272,15 @@ public async Task PutAsync<TBody>(
using var request = CreateRequest(HttpMethod.Put, path, null, requestContent);
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode is HttpStatusCode.Created or HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode is HttpStatusCode.Created or HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);
}

public async Task PutAsync(RelativePath path, CancellationToken cancellationToken = default)
{
using var request = CreateRequest(HttpMethod.Put, path);
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

response.EnsureExpectedStatusCode(statusCode => statusCode is HttpStatusCode.Created or HttpStatusCode.NoContent);
await response.EnsureExpectedStatusCodeAsync(statusCode => statusCode is HttpStatusCode.Created or HttpStatusCode.NoContent, cancellationToken).ConfigureAwait(false);
}

private HttpRequestMessage CreateRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Runtime.Serialization;
using System.Text;

namespace EasyNetQ.Management.Client;

Expand All @@ -13,27 +14,13 @@ public class UnexpectedHttpStatusCodeException : Exception
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
//

private const string NoRequest = "<null>";

public HttpStatusCode StatusCode { get; private init; }
public int StatusCodeNumber => (int)StatusCode;

public UnexpectedHttpStatusCodeException()
{
}

public UnexpectedHttpStatusCodeException(HttpStatusCode statusCode) :
base($"Unexpected Status Code: {(int)statusCode} {statusCode}")
{
StatusCode = statusCode;
}

public UnexpectedHttpStatusCodeException(HttpResponseMessage response) :
base($"Unexpected Status Code: {(int)response.StatusCode} {response.StatusCode} from request: {response.RequestMessage?.ToString() ?? NoRequest}")
{
StatusCode = response.StatusCode;
}

public UnexpectedHttpStatusCodeException(string message) : base(message)
{
}
Expand All @@ -48,4 +35,51 @@ StreamingContext context
) : base(info, context)
{
}

protected UnexpectedHttpStatusCodeException(string message, HttpStatusCode statusCode) : base(message)
{
StatusCode = statusCode;
}

public static async Task<UnexpectedHttpStatusCodeException> FromHttpResponseMessageAsync(HttpResponseMessage response, CancellationToken cancellationToken = default)
{
var sb = new StringBuilder("Unexpected response: StatusCode: ");
sb.Append((int)response.StatusCode);
sb.Append(" ");
sb.Append(response.StatusCode);
sb.Append(", Content: ");
if (response.Content != null)
{
try
{
#if NET5_0_OR_GREATER
var content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
#else
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
#endif
sb.Append('\'');
sb.Append(content);
sb.Append('\'');
}
catch
{
sb.Append("<not a string>");
}
}
else
{
sb.Append("<null>");
}
sb.Append(" from request: ");
if (response.RequestMessage != null)
{
sb.Append(response.RequestMessage);
}
else
{
sb.Append("<null>");
}

return new UnexpectedHttpStatusCodeException(sb.ToString(), response.StatusCode);
}
}
Loading