Skip to content

fix(go): fall back on empty error messages and prefix inferred messages with the error code - #215

Merged
TristanSpeakEasy merged 2 commits into
mainfrom
fix/go-error-message-empty-and-code
Nov 16, 2026
Merged

TristanSpeakEasy merged 2 commits into
mainfrom
fix/go-error-message-empty-and-code

Conversation

@TristanSpeakEasy

@TristanSpeakEasy TristanSpeakEasy commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

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": "" made err.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 fix(go): read nested and inferred error messages in typed errors #202 the JSON dump included code; 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, OptionalNullable and value fields, including union error members.
  • When the message is inferred (top-level message or error.message, no x-speakeasy-error-message marker) and the struct that holds it has a scalar code sibling (string, integer, number or enum), Error() returns "<code>: <message>". An absent, null or empty code (including an empty string-backed enum) falls back to the bare message.
  • Messages chosen with x-speakeasy-error-message are left as they are apart from the empty check. The marker is an explicit choice of what Error() 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 Go Error() 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 code is considered. Fields like status or type are not, to keep the inference narrow and predictable.
  • This changes err.Error() for Go SDKs whose errors have an inferred message plus a code field. Inferred messages only reached Go in fix(go): read nested and inferred error messages in typed errors #202, so the exposure is limited to SDKs regenerated since then.

Testing

  • go test ./pkg/generate/snapshots/ -run TestSnapGo: passed. TestSnapGoErrorMessageLeafTypes adds schemas and contract cases for an empty message (pointer, OptionalNullable, value and top-level marked), nested and top-level code + message (string, integer and OptionalNullable code; code absent, null or empty) and a marked message with a code sibling. Updated the three other Go error snapshots for the new empty check.
  • TARGET=primary make test-go: 773 passed. The 501 x-speakeasy-errors case now expects "501: an error occurred". Added TestErrorMessageEmptyAndCode.
  • TARGET=review make build-go: only the Error() bodies listed above changed.
  • TARGET=review make build-cli: only the same Error() bodies changed.
  • make check-template-go, npm run format and golangci-lint run ./pkg/generate/snapshots/...: pass.
  • make lint: golangci-lint reported a gofmt issue in templates/perms.go, which this PR does not touch. Local gofmt -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 code is considered; status and type are 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 fix(go): read nested and inferred error messages in typed errors #202 whose errors have an inferred message plus a code field.

Written for commit 14d9d7c. Summary will update on new commits.

View guided diff Turn on auto-fix

@TristanSpeakEasy
TristanSpeakEasy requested a review from a team as a code owner November 12, 2026 23:14
@TristanSpeakEasy TristanSpeakEasy added bug Something isn't working go Trigger (90) snapshot tests for go labels Nov 12, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 16 files

Shadow auto-approve: would not auto-approve because issues were found.

View guided diff | Turn on auto-fix | Re-trigger cubic

@speakeasy-generator-bot

speakeasy-generator-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Generator Snapshot Testing

Snapshot tests are running.

Snapshot results: https://github.com/speakeasy-api/openapi-generation-snapshots/issues/168#issuecomment-6903522371

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 11 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Shadow auto-approve: would auto-approve. Go error-message generation now treats empty messages as missing and prefixes inferred messages with a sibling code field; regenerated SDKs and snapshots confirm the bounded fix. The dismissed boolean-code finding is a deliberate scope limit, not a blocking tradeoff.
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack

View guided diff | Turn on auto-fix | Re-trigger cubic

@TristanSpeakEasy TristanSpeakEasy added the review:bypass Merge without human review approval. Required status checks still apply. label Nov 16, 2026

@speakeasybot speakeasybot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto-approved: review:bypass label applied by @TristanSpeakEasy. Required status checks still gate this merge.

@TristanSpeakEasy
TristanSpeakEasy added this pull request to the merge queue Nov 16, 2026
Merged via the queue into main with commit ed9b064 Nov 16, 2026
260 of 261 checks passed
@TristanSpeakEasy
TristanSpeakEasy deleted the fix/go-error-message-empty-and-code branch November 16, 2026 00:25
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working go Trigger (90) snapshot tests for go review:bypass Merge without human review approval. Required status checks still apply.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants