Skip to content

Commit 2bc6c30

Browse files
committed
someday I will stop minor tweaking
1 parent ed9eb8c commit 2bc6c30

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/Client/Infrastructure/RequestHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,18 @@ private static async ValueTask ThrowResponseError(
201201
{
202202
if (response.Content.Headers.ContentType?.MediaType != ContentTypes.Json)
203203
{
204-
throw PlayerClientException.Create(response);
204+
throw PlayerClientException.Create(response.StatusCode, response.ReasonPhrase);
205205
}
206206

207207
var errorResponse = (ErrorResponse?)await ParseResponse(
208208
response, ErrorResponseType, DefaultSerializerOptions, true, cancellationToken)
209209
.ConfigureAwait(false);
210210

211-
throw PlayerClientException.Create(response, errorResponse?.Error?.Message, errorResponse?.Error?.Parameter);
211+
throw PlayerClientException.Create(
212+
response.StatusCode,
213+
response.ReasonPhrase,
214+
errorResponse?.Error?.Message,
215+
errorResponse?.Error?.Parameter);
212216
}
213217

214218
private static PlayerClientException InvalidResponse()

src/Client/PlayerClientException.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,20 @@ public PlayerClientException(
4444
}
4545

4646
internal static PlayerClientException Create(
47-
HttpResponseMessage message,
47+
HttpStatusCode statusCode,
48+
string? reasonPhrase = null,
4849
string? serverErrorMessage = null,
4950
string? errorParameterName = null)
5051
{
5152
var messageBuilder = new StringBuilder(150);
5253

5354
messageBuilder.Append(
5455
CultureInfo.InvariantCulture,
55-
$"Response status code does not indicate success: {(int)message.StatusCode}");
56+
$"Response status code does not indicate success: {(int)statusCode}");
5657

57-
if (message.ReasonPhrase != null)
58+
if (reasonPhrase != null)
5859
{
59-
messageBuilder.Append($" ({message.ReasonPhrase})");
60+
messageBuilder.Append($" ({reasonPhrase})");
6061
}
6162

6263
messageBuilder.Append('.');
@@ -77,7 +78,7 @@ internal static PlayerClientException Create(
7778
return new PlayerClientException(
7879
messageBuilder.ToString(),
7980
HttpRequestError.Unknown,
80-
message.StatusCode,
81+
statusCode,
8182
serverErrorMessage,
8283
errorParameterName);
8384
}

tests/Client.Tests/PlayerClientExceptionTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Net;
2-
using System.Net.Http;
3-
using Beefweb.Client.Infrastructure;
42
using FluentAssertions;
53
using Xunit;
64

@@ -11,7 +9,7 @@ public class PlayerClientExceptionTests
119
[Fact]
1210
public void Create_Simple()
1311
{
14-
var exception = PlayerClientException.Create(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
12+
var exception = PlayerClientException.Create(HttpStatusCode.MethodNotAllowed, "Method Not Allowed");
1513
exception.Message.Should().Be("Response status code does not indicate success: 405 (Method Not Allowed).");
1614
exception.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed);
1715
exception.ServerErrorMessage.Should().BeNull();
@@ -22,7 +20,8 @@ public void Create_Simple()
2220
public void Create_WithServerMessage()
2321
{
2422
var exception = PlayerClientException.Create(
25-
new HttpResponseMessage(HttpStatusCode.InternalServerError),
23+
HttpStatusCode.InternalServerError,
24+
"Internal Server Error",
2625
"it hits the fan");
2726

2827
exception.Message.Should().Be(
@@ -37,8 +36,10 @@ public void Create_WithServerMessage()
3736
public void Create_WithServerMessage_WithParameterName()
3837
{
3938
var exception = PlayerClientException.Create(
40-
new HttpResponseMessage(HttpStatusCode.BadRequest),
41-
"parameter is required", "index");
39+
HttpStatusCode.BadRequest,
40+
"Bad Request",
41+
"parameter is required",
42+
"index");
4243

4344
exception.Message.Should().Be(
4445
"Response status code does not indicate success: 400 (Bad Request). Server error: parameter is required. Parameter name: index.");

0 commit comments

Comments
 (0)