fix(go): fall back on empty error messages and prefix inferred messages with the error code
Why
Follow-up to #202, which made Go typed errors build Error() from a nested or inferred message field. That left two gaps for a common error envelope like:
{"error": {"code": "rate_limit", "message": "slow down"}}
- Empty messages. The Go template only checked
!= nil(or returned a value field directly), so"message": ""madeerr.Error()return"". TypeScript, Python and C# all treat an empty message as missing and use their fallback. - Lost error code. Go callers almost always surface errors through
err.Error()(fmt.Errorf("...: %w", err),log.Print(err)). Before #202 the JSON dump includedcode; after it, logs only show the bare message, which makes failures harder to tell apart.
What changed
- Every string message leaf now requires a non-empty value before it is returned. Otherwise
Error()uses the existing fallback: the JSON dump, or"unknown error"for a top-level marked field. This applies to pointer,OptionalNullableand value fields, including union error members. - When the message is inferred (top-level
messageorerror.message, nox-speakeasy-error-messagemarker) and the struct that holds it has a scalarcodesibling (string, integer, number or enum),Error()returns"<code>: <message>". An absent, null or emptycode(including an empty string-backed enum) falls back to the bare message. - Messages chosen with
x-speakeasy-error-messageare left as they are apart from the empty check. The marker is an explicit choice of whatError()should say, so the generator does not add to it.
Generated output for {error: {code, message}}:
func (e *CreateWidgetError) Error() string {
if e != nil {
if v0 := e.ErrorInfo; v0 != nil {
if v0.Message != nil && *v0.Message != "" {
if v0.Code != nil && *v0.Code != "" {
return fmt.Sprintf("%v: %s", *v0.Code, *v0.Message)
}
return *v0.Message
}
}
}
data, _ := json.Marshal(e)
return string(data)
}
Review SDKs (zSDKs/sdk-go, and zSDKs/sdk-cli, which embeds the Go SDK): Error() bodies gain the empty check, and RequestTimeoutError (inferred message with an integer code) now returns "<code>: <message>".
Review notes
- The code prefix is Go-only. Other targets expose the code through the typed error (and Go callers still can via
errors.As), but in GoError()is usually the only thing that reaches a log. I limited it to inferred messages so a marked field keeps its exact output. - Only a field named
codeis considered. Fields likestatusortypeare not, to keep the inference narrow and predictable. - This changes
err.Error()for Go SDKs whose errors have an inferred message plus acodefield. Inferred messages only reached Go in #202, so the exposure is limited to SDKs regenerated since then.
Testing
go test ./pkg/generate/snapshots/ -run TestSnapGo: passed.TestSnapGoErrorMessageLeafTypesadds schemas and contract cases for an empty message (pointer,OptionalNullable, value and top-level marked), nested and top-levelcode+message(string, integer andOptionalNullablecode; code absent, null or empty) and a marked message with acodesibling. Updated the three other Go error snapshots for the new empty check.TARGET=primary make test-go: 773 passed. The 501x-speakeasy-errorscase now expects"501: an error occurred". AddedTestErrorMessageEmptyAndCode.TARGET=review make build-go: only theError()bodies listed above changed.TARGET=review make build-cli: only the sameError()bodies changed.make check-template-go,npm run formatandgolangci-lint run ./pkg/generate/snapshots/...: pass.make lint: golangci-lint reported a gofmt issue intemplates/perms.go, which this PR does not touch. Localgofmt -l(Go 1.26) reports the file as clean, so it looks like a local toolchain mismatch. Relying on CI for the full lint run.
Public-safety check
- This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
- Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (
.claude/skills/public-repo-communication/SKILL.md). - Generated fixtures and review SDK changes are public-safe.
- I reviewed
git diff --check.
Summary by cubic
Fixes two gaps in Go-generated Error() bodies: empty string messages now fall back like missing ones, and inferred messages are prefixed with a sibling code field.
Previously "message": "" made err.Error() return an empty string; it now falls back to the JSON dump or "unknown error". Inferred messages (no x-speakeasy-error-message marker) that share a struct with a scalar code now return "<code>: <message>", so failures are easier to tell apart in logs. A string-backed enum code with an empty value falls back to the bare message instead of producing ": <message>". Marked messages keep their exact output.
Behavior changes
- Empty messages now behave like null or missing messages across pointer,
OptionalNullable, value, and union error members. - Only a field named
codeis considered;statusandtypeare ignored. - The change is Go-only — other targets expose the code through the typed error, and Go callers still can via
errors.As. - Changes
err.Error()output for Go SDKs regenerated since #202 whose errors have an inferred message plus acodefield.