diff --git a/src/Chronos.Protocol/Client/ChronosClient.cs b/src/Chronos.Protocol/Client/ChronosClient.cs
index d2010c47..bf42522e 100644
--- a/src/Chronos.Protocol/Client/ChronosClient.cs
+++ b/src/Chronos.Protocol/Client/ChronosClient.cs
@@ -97,7 +97,8 @@ public sealed class ChronosClient : IDisposable
var answer = await IdentifyAsync(response, ct).ConfigureAwait(false);
if (answer.Info is { } info) return info;
if (answer.Chronos == false) throw new NotChronosServerException(BaseAddress);
- // Chronos's own error, or busy: as any other request reports it.
+ // Chronos's own error, already read (the body cannot be read twice), or busy: as any other request reports it.
+ if (answer.Error is { } error) throw new ChronosApiException(response.StatusCode, error);
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
throw new NotChronosServerException(BaseAddress);
}
@@ -112,8 +113,11 @@ public sealed class ChronosClient : IDisposable
private static bool IsChronos(ServerInfo? info) =>
info is { Name.Length: > 0, Version.Length: > 0, ApiVersion: > 0, Capabilities: not null, Formats: not null, Limits: not null };
- /// What a server/info answer is: Chronos's (with its info when it succeeded), something else, or unknown (busy).
- private readonly record struct Identity(bool? Chronos, ServerInfo? Info = null, bool Answered = true);
+ ///
+ /// What a server/info answer is: Chronos's (with its info when it succeeded, or its error when it failed),
+ /// something else, or unknown (busy).
+ ///
+ private readonly record struct Identity(bool? Chronos, ServerInfo? Info = null, bool Answered = true, ApiError? Error = null);
///
/// Tells a server/info answer of Chronos from any other. Chronos answers it anonymously with its JSON and never
@@ -138,7 +142,7 @@ public sealed class ChronosClient : IDisposable
}
if (!response.Headers.Contains(ApiVersion.Header)) return new(false);
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError, ct).ConfigureAwait(false);
- return new(error is { Code.Length: > 0 } && !error.Code.StartsWith("http_", StringComparison.Ordinal));
+ return error is { Code.Length: > 0 } && !error.Code.StartsWith("http_", StringComparison.Ordinal) ? new(true, Error: error) : new(false);
}
catch (Exception ex) when (ex is JsonException or NotSupportedException or HttpRequestException)
{
diff --git a/tests/Chronos.Server.Tests/Api/ServerIdentityTests.cs b/tests/Chronos.Server.Tests/Api/ServerIdentityTests.cs
index fd14968d..d3912ce1 100644
--- a/tests/Chronos.Server.Tests/Api/ServerIdentityTests.cs
+++ b/tests/Chronos.Server.Tests/Api/ServerIdentityTests.cs
@@ -77,6 +77,24 @@ public sealed class ServerIdentityTests(TestContext context)
Assert.Equal(0, other.Authorized, "no request carried credentials");
}
+ [Test]
+ public async Task AChronosErrorFromTheInfoIsReportedAsThatError()
+ {
+ // Chronos's own error (its header and code) is read once to tell it from other services: it must still come back
+ // as the server's error, and an unknown certificate still asks for trust.
+ await using var other = await OtherService.StartAsync(context);
+ foreach (var (path, status, code) in new[] { ("chronos500/", HttpStatusCode.InternalServerError, "internal"), ("chronos400/", HttpStatusCode.BadRequest, "bad_request") })
+ {
+ using var plain = new ChronosClient(new Uri(other.Http, path), new CertificatePinning([]), "tests");
+ var error = await Assert.ThrowsAsync(() => plain.GetInfoAsync(context.Cancellation), path);
+ Assert.Equal(status, error.Status, path);
+ Assert.Equal(code, error.Error.Code, path);
+
+ using var untrusted = new ChronosClient(new Uri(other.Https, path), new CertificatePinning([]), "tests");
+ await Assert.ThrowsAsync(() => untrusted.GetInfoAsync(context.Cancellation), path);
+ }
+ }
+
[Test]
public async Task AChronosServerBehindAnUnknownCertificateStillAsksForTrust()
{
@@ -149,6 +167,13 @@ public sealed class ServerIdentityTests(TestContext context)
// Errors in JSON with a code of their own, as many web services write them.
foreach (var (path, status) in new[] { ("json401", 401), ("json403", 403), ("json404", 404) })
app.MapGet($"/{path}/api/v1/server/info", () => Results.Content("""{"code":"denied","message":"Sign in"}""", "application/json", statusCode: status));
+ // Errors as Chronos writes them: its header and a code of its own.
+ foreach (var (path, status, code) in new[] { ("chronos500", 500, "internal"), ("chronos400", 400, "bad_request") })
+ app.MapGet($"/{path}/api/v1/server/info", (HttpContext http) =>
+ {
+ http.Response.Headers[ApiVersion.Header] = "1";
+ return Results.Content($$"""{"code":"{{code}}","message":"Failed"}""", "application/json", statusCode: status);
+ });
// More than a server info ever holds.
app.MapGet("/huge/api/v1/server/info", () => Results.Content("" + new string('x', 2 << 20), "text/html"));
app.MapGet("/hugejson/api/v1/server/info", () => Results.Content("{\"name\":\"" + new string('x', 2 << 20) + "\"}", "application/json"));