Skip to content

Carry errors across the wire as JSON, not as Elixir terms - #2

Merged
HackTuah merged 1 commit into
mainfrom
slice/002-wire-error-payloads
Sep 6, 2026
Merged

Carry errors across the wire as JSON, not as Elixir terms#2
HackTuah merged 1 commit into
mainfrom
slice/002-wire-error-payloads

Conversation

@HackTuah

@HackTuah HackTuah commented Sep 6, 2026

Copy link
Copy Markdown
Member

One commit, red first. Independent of PR #1 — branched from main, touches no file that PR touches.

The defect

A failed tool call returned this to the client:

"structuredContent": {"error":
  "%{reason: \"invalid arguments: missing required property: a\", tool: :t}"}

inspect/1 output on an Elixir map — map-literal syntax, a bare atom with its leading colon, Elixir-escaped inner quotes. The useful parts (which tool, which property, why) were all present and recoverable only by parsing Elixir, by a client with no reason to know what language the server is written in.

It was wrong at every protocol revision, which is why it was kept out of the negotiation slice rather than folded into it.

Red first

5 tests, 3 failures
  a validation failure carries structured fields, not an inspected map
  the human-readable content carries no Elixir syntax either
  a host's error term is carried as JSON, not as an inspected term

Green: 5/5, suite 33/33, gate exit 0.

What changed

was  "error": "%{reason: \"invalid arguments: missing required property: a\", tool: :t}"
now  "error": {"tool": "widget",
               "reason": "invalid arguments: missing required property: a"}
     "text":  "widget: invalid arguments: missing required property: a"

structuredContent carries the error as data, so a client can read a field. content carries a sentence a person can read. format_reason/1 and its inspect/1 fallback are gone, and no inspect/1 remains in server.ex.

A host's error term is carried as JSON too: a dispatch returning {:error, %{code: "upstream_timeout", retry_after: 30}} reaches the client as an object with those fields. A plain-string error survives unchanged.

One thing worth noticing in the tests

They assert the absence of Elixir syntax by pattern — map literals and bare atoms — rather than comparing against a fixed expected string. A regression of a different shape is still caught, where a string comparison would only catch this exact one.

One existing assertion used =~ against what was a string and is now an object; it now reads the reason field. That is a consequence of the shape change, not an accommodation of it.

A failed tool call returned this to the client:

    "structuredContent": {"error":
      "%{reason: \"invalid arguments: missing required property: a\", tool: :t}"}

That is inspect/1 output on a map: map-literal syntax, a bare atom with its
leading colon, Elixir-escaped inner quotes. The useful parts -- which tool,
which property, why -- were all present and recoverable only by parsing Elixir.

It was wrong at every protocol revision, which is why it was not folded into the
negotiation slice.

Red first:

    5 tests, 3 failures
      a validation failure carries structured fields, not an inspected map
      the human-readable content carries no Elixir syntax either
      a host's error term is carried as JSON, not as an inspected term

Green: 5 tests 0 failures, suite 33 0, gate exit 0.

    was  "error": "%{reason: \"invalid arguments: ...\", tool: :t}"
    now  "error": {"tool": "widget",
                   "reason": "invalid arguments: missing required property: a"}
         "text":  "widget: invalid arguments: missing required property: a"

structuredContent carries the error as data so a client can read a field;
content carries a sentence a person can read. format_reason/1 and its inspect/1
fallback are gone, and no inspect/1 remains in server.ex.

A host's error term is carried as JSON too, so a dispatch returning
{:error, %{code: "upstream_timeout", retry_after: 30}} reaches the client as an
object with those fields rather than as a stringified term. A plain-string error
survives unchanged.

One existing assertion used =~ against what was a string and is now an object;
it now reads the reason field. That is a consequence of the shape change.

The tests assert the absence of Elixir syntax by pattern -- map literals and
bare atoms -- rather than by comparing to a fixed string, so a future regression
of a different shape is still caught.

Signed-off-by: Ayla Croft <aylacroft@proton.me>
@HackTuah
HackTuah force-pushed the slice/002-wire-error-payloads branch from 021645a to 231677f Compare September 6, 2026 20:47
@HackTuah
HackTuah merged commit 2add129 into main Sep 6, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant