Skip to content

Commit c33860b

Browse files
fix: include request URL in NetworkException (#31)
NetworkException previously surfaced only the status code and response body, so callers wrapping the SDK could not tell whether a 401 came from PKG (MPK / signing-key fetch) or one of the three Cryptify upload phases without digging through stack traces. Add a Url property, include the URL in the message, and propagate it from both EnsureSuccessAsync helpers via response.RequestMessage.RequestUri. Closes #26 Co-authored-by: dobby-yivi-agent[bot] <275734547+dobby-yivi-agent[bot]@users.noreply.github.com>
1 parent a3762d0 commit c33860b

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/Api/CryptifyClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ private static async Task EnsureSuccessAsync(HttpResponseMessage response)
130130
if (!response.IsSuccessStatusCode)
131131
{
132132
var body = await response.Content.ReadAsStringAsync();
133-
throw new NetworkException((int)response.StatusCode, body);
133+
var url = response.RequestMessage?.RequestUri?.ToString() ?? "<unknown>";
134+
throw new NetworkException((int)response.StatusCode, body, url);
134135
}
135136
}
136137
}

src/Api/PkgClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ private static async Task EnsureSuccessAsync(HttpResponseMessage response)
7373
if (!response.IsSuccessStatusCode)
7474
{
7575
var body = await response.Content.ReadAsStringAsync();
76-
throw new NetworkException((int)response.StatusCode, body);
76+
var url = response.RequestMessage?.RequestUri?.ToString() ?? "<unknown>";
77+
throw new NetworkException((int)response.StatusCode, body, url);
7778
}
7879
}
7980
}

src/Exceptions/PostGuardException.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ public class NetworkException : PostGuardException
1010
{
1111
public int StatusCode { get; }
1212
public string Body { get; }
13+
public string Url { get; }
1314

14-
public NetworkException(int statusCode, string body)
15-
: base($"HTTP {statusCode}: {body}")
15+
public NetworkException(int statusCode, string body, string url)
16+
: base($"HTTP {statusCode} at {url}: {body}")
1617
{
1718
StatusCode = statusCode;
1819
Body = body;
20+
Url = url;
1921
}
2022
}
2123

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using E4A.PostGuard.Exceptions;
2+
3+
namespace E4A.PostGuard.Tests;
4+
5+
public class NetworkExceptionTests
6+
{
7+
[Fact]
8+
public void Ctor_PopulatesUrlProperty()
9+
{
10+
var ex = new NetworkException(401, "Unauthorized", "https://pkg.postguard.eu/v2/irma/sign/key");
11+
12+
Assert.Equal(401, ex.StatusCode);
13+
Assert.Equal("Unauthorized", ex.Body);
14+
Assert.Equal("https://pkg.postguard.eu/v2/irma/sign/key", ex.Url);
15+
}
16+
17+
[Fact]
18+
public void Ctor_IncludesUrlAndStatusInMessage()
19+
{
20+
var ex = new NetworkException(500, "boom", "https://cryptify.postguard.eu/fileupload/init");
21+
22+
Assert.Contains("500", ex.Message);
23+
Assert.Contains("https://cryptify.postguard.eu/fileupload/init", ex.Message);
24+
Assert.Contains("boom", ex.Message);
25+
}
26+
27+
[Fact]
28+
public void Ctor_AllowsEmptyBody()
29+
{
30+
var ex = new NetworkException(204, "", "https://pkg.postguard.eu/v2/parameters");
31+
32+
Assert.Equal("", ex.Body);
33+
Assert.Equal("https://pkg.postguard.eu/v2/parameters", ex.Url);
34+
}
35+
}

0 commit comments

Comments
 (0)