diff --git a/CHANGELOG.md b/CHANGELOG.md index 28787fd0..4737771e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ Nightly builds of `master` are on [GitHub Packages](README.md#nightly-builds-github-packages) as nightlies of the next version, `{version}-nightly.{yyyyMMdd}.g{shortSha}`. +### 🐛 Fixes + +- An `OpenCodeApiException` whose server message already ends in `.`, `!` or `?` no longer gets a + second period, so the readable 500 opencode answers handler defects with reads + "… Check server logs for details." instead of ending in "..". + ## [2.0.1] - 2026-11-22 **`2.0.1` pins opencode `2.3.2`.** It is a patch that moves the protocol pin, so generated types diff --git a/src/OpenCode.Sdk/Internal/Operations/OpenCodeErrorReader.cs b/src/OpenCode.Sdk/Internal/Operations/OpenCodeErrorReader.cs index 8f955ad6..785a4005 100644 --- a/src/OpenCode.Sdk/Internal/Operations/OpenCodeErrorReader.cs +++ b/src/OpenCode.Sdk/Internal/Operations/OpenCodeErrorReader.cs @@ -85,7 +85,8 @@ public static OpenCodeApiException CreateApiException(int status, IOpenCodeError if (selected is { Length: > 0 }) { - message = $"The opencode API returned status {statusText} ('{error.Tag}'): {selected}."; + var end = selected[^1] is '.' or '!' or '?' ? string.Empty : "."; + message = $"The opencode API returned status {statusText} ('{error.Tag}'): {selected}{end}"; } } diff --git a/tests/OpenCode.Sdk.Tests/OpenCodeClientContractTests.cs b/tests/OpenCode.Sdk.Tests/OpenCodeClientContractTests.cs index 71f8e764..135355a6 100644 --- a/tests/OpenCode.Sdk.Tests/OpenCodeClientContractTests.cs +++ b/tests/OpenCode.Sdk.Tests/OpenCodeClientContractTests.cs @@ -290,6 +290,47 @@ public async Task GetMessageAsync_Should_Downgrade_A_Known_Tag_At_The_Wrong_Stat await Assert.That(((UnknownOpenCodeError)exception.Error!).Tag).IsEqualTo("SessionNotFoundError"); } + [Test] + public async Task GetMessageAsync_Should_Read_The_Readable_500_On_An_Undeclared_Status() + { + using var scenario = ContractScenario.Responding(HttpStatusCode.InternalServerError, WireBodyData.DefectResponse); + + var exception = await ThrowsApiFailureAsync(async () => _ = await scenario.Client.Sessions.GetSessionClient("s").GetMessageAsync("m")); + + await Assert.That(exception.Status).IsEqualTo(500); + await Assert.That(exception.Error).IsTypeOf(); + await Assert.That(((UnknownOpenCodeError)exception.Error!).Tag).IsEqualTo("UnknownError"); + await Assert.That(exception.Message).IsEqualTo( + "The opencode API returned status 500 ('UnknownError'): " + + "Server failed to handle the request. Check server logs for details."); + await Assert.That(exception.RawBody).Contains("\"ref\":\"err_0a1b2c3d\""); + } + + [Test] + public async Task GetMessageAsync_Should_Keep_The_Bare_Status_Sentence_For_An_Empty_500() + { + using var scenario = ContractScenario.Responding(HttpStatusCode.InternalServerError, string.Empty); + + var exception = await ThrowsApiFailureAsync(async () => _ = await scenario.Client.Sessions.GetSessionClient("s").GetMessageAsync("m")); + + await Assert.That(exception.Message).IsEqualTo("The opencode API returned status 500."); + await Assert.That(exception.Error).IsNull(); + } + + [Test] + public async Task ListMessages_Should_Read_The_Readable_500_As_The_Declared_UnknownError() + { + using var scenario = ContractScenario.Responding(HttpStatusCode.InternalServerError, WireBodyData.DefectResponse); + + var exception = await ThrowsApiFailureAsync(async () => _ = await scenario.Client.Sessions.GetSessionClient("s").ListMessagesAsync()); + + await Assert.That(exception.Error).IsTypeOf(); + var error = (UnknownError)exception.Error!; + await Assert.That(error.Message).IsEqualTo("Server failed to handle the request. Check server logs for details."); + await Assert.That(error.Ref).IsEqualTo("err_0a1b2c3d"); + await Assert.That(exception.Message).EndsWith("Server failed to handle the request. Check server logs for details."); + } + [Test] public async Task GetInfoAsync_Should_Preserve_The_Raw_Body_For_Malformed_Errors() { diff --git a/tests/OpenCode.Sdk.Tests/Support/WireBodyData.cs b/tests/OpenCode.Sdk.Tests/Support/WireBodyData.cs index 945f3ed8..97621b4e 100644 --- a/tests/OpenCode.Sdk.Tests/Support/WireBodyData.cs +++ b/tests/OpenCode.Sdk.Tests/Support/WireBodyData.cs @@ -66,6 +66,9 @@ public static byte[] InfoWithMalformedUtf8UnknownField() public const string UnknownError = "{\"_tag\":\"UnknownError\",\"message\":\"boom\"}"; + public const string DefectResponse = + "{\"_tag\":\"UnknownError\",\"message\":\"Server failed to handle the request. Check server logs for details.\",\"ref\":\"err_0a1b2c3d\"}"; + public const string InvalidCursorError = "{\"_tag\":\"InvalidCursorError\",\"message\":\"stale\"}"; public const string InvalidRequestError = "{\"_tag\":\"InvalidRequestError\",\"message\":\"bad\"}";