From 41797fb2a7955dca30d5e837f312208d62730eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C4=B0rgin?= Date: Sun, 22 Nov 2026 18:06:00 +0300 Subject: [PATCH 1/2] fix: an API exception message does not double the server's closing period The default message in packages/server/src/middleware/defect-response.ts at the opencode v2.3.2 pin already ends in a period. Preserve closing periods, exclamation marks and question marks when composing the API exception message. Add GetMessageAsync_Should_Read_The_Readable_500_On_An_Undeclared_Status, GetMessageAsync_Should_Keep_The_Bare_Status_Sentence_For_An_Empty_500, and ListMessages_Should_Read_The_Readable_500_As_The_Declared_UnknownError contract rows. --- CHANGELOG.md | 3 ++ .../Operations/OpenCodeErrorReader.cs | 3 +- .../OpenCodeClientContractTests.cs | 41 +++++++++++++++++++ .../Support/WireBodyData.cs | 3 ++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28787fd0..fe0fc6f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ 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\"}"; From 533ad2d21d26cc2f8e50e93054e1d4b3acf7fa7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C4=B0rgin?= Date: Sun, 22 Nov 2026 18:07:17 +0300 Subject: [PATCH 2/2] docs(changelog): wrap the exception-period fix entry like its neighbours --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe0fc6f4..4737771e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,10 @@ Nightly builds of `master` are on `{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 "..". + +- 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