Skip to content

Commit 3817d14

Browse files
authored
Merge pull request #111 from Zechiax/fixHttpException
Requests could throw HttpRequestException instead of ModrinthApiException
2 parents a2cf888 + 74dbe65 commit 3817d14

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

src/Modrinth.Net/Exceptions/ModrinthApiException.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,17 @@ public class ModrinthApiException : Exception
1515
/// <param name="response"> The HTTP response message. </param>
1616
/// <param name="error"> The error returned by the Modrinth API. </param>
1717
/// <param name="innerException"> The inner exception. </param>
18-
public ModrinthApiException(string message, HttpResponseMessage response,
18+
public ModrinthApiException(string message, HttpResponseMessage? response = null,
1919
ResponseError? error = null, Exception? innerException = null) : base(message, innerException)
2020
{
2121
Error = error;
2222
Response = response;
2323
}
2424

25-
/// <summary>
26-
/// The status code of the HTTP response.
27-
/// </summary>
28-
[Obsolete("Use Response.StatusCode instead. This property will be removed in a future version of the API.")]
29-
public HttpStatusCode StatusCode => Response.StatusCode;
30-
31-
/// <summary>
32-
/// The HTTP response content.
33-
/// </summary>
34-
[Obsolete("Use Response.Content instead. This property will be removed in a future version of the API.")]
35-
public HttpContent Content => Response.Content;
36-
3725
/// <summary>
3826
/// The HTTP response message.
3927
/// </summary>
40-
public HttpResponseMessage Response { get; }
28+
public HttpResponseMessage? Response { get; }
4129

4230
/// <summary>
4331
/// The error returned by the Modrinth API.

src/Modrinth.Net/Http/Requester.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
104104
var retryCount = 0;
105105
while (true)
106106
{
107-
var response = await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
107+
HttpResponseMessage response;
108+
try
109+
{
110+
response = await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
111+
}
112+
catch (HttpRequestException e)
113+
{
114+
throw new ModrinthApiException("An error occurred while sending the request.", innerException: e);
115+
}
108116

109117
if (response.IsSuccessStatusCode) return response;
110118

test/Modrinth.Net.Test/Modrinth.Net.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
1313
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0"/>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
15+
<PackageReference Include="Moq" Version="4.20.70" />
1516
<PackageReference Include="NUnit" Version="4.0.1"/>
1617
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
1718
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Modrinth.Exceptions;
2+
using Modrinth.Http;
3+
4+
namespace Modrinth.Net.Test;
5+
6+
[TestFixture]
7+
public class RequesterTests
8+
{
9+
[Test]
10+
public void Requester_ShouldNotThrow_HttpException()
11+
{
12+
// Arrange
13+
var mockHttpClient = new Mock<HttpClient>();
14+
mockHttpClient.Setup(client => client.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
15+
.Throws(new HttpRequestException("An error occurred while sending the request."));
16+
17+
var config = new ModrinthClientConfig
18+
{
19+
BaseUrl = "http://test.com",
20+
UserAgent = "TestAgent",
21+
ModrinthToken = "TestToken"
22+
};
23+
24+
var requester = new Requester(config, mockHttpClient.Object);
25+
26+
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
27+
28+
Assert.ThrowsAsync<ModrinthApiException>(async () => await requester.GetJsonAsync<object>(request));
29+
}
30+
31+
}

test/Modrinth.Net.Test/Usings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
global using NUnit.Framework;
1+
global using NUnit.Framework;
2+
global using Moq;

0 commit comments

Comments
 (0)