Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/OpenCode.Sdk.Tests/OpenCodeClientContractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnknownOpenCodeError>();
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<UnknownError>();
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()
{
Expand Down
3 changes: 3 additions & 0 deletions tests/OpenCode.Sdk.Tests/Support/WireBodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}";
Expand Down
Loading