Skip to content

Import context in server codecs for WebSocket error encoders - #4073

Open
uberbrodt wants to merge 1 commit into
goadesign:v3from
uberbrodt:fix/websocket-error-context
Open

uberbrodt wants to merge 1 commit into
goadesign:v3from
uberbrodt:fix/websocket-error-context

Conversation

@uberbrodt

Copy link
Copy Markdown

A service whose only HTTP endpoints are WebSocket streams generates a server encode_decode.go that does not compile when any of those endpoints maps an error to a status code:

var _ = Service("watcher", func() {
	Method("watch", func() {
		Payload(func() {
			Attribute("id", String)
			Required("id")
		})
		StreamingResult(Int)
		Error("not_found")
		HTTP(func() {
			GET("/watch/{id}")
			Response("not_found", StatusNotFound)
		})
	})
})
gen/http/watcher/server/encode_decode.go:38:36: undefined: context
gen/http/watcher/server/encode_decode.go:40:18: undefined: context

The server codec renders an error encoder for every non-JSON-RPC endpoint with HTTP errors, WebSocket endpoints included, and the error encoder signature uses context.Context. httpServerCodecUsesContext skipped WebSocket endpoints entirely, so the codec file never planned the context import. A service that has at least one ordinary HTTP endpoint hides the bug, because that endpoint's response encoder brings the import in.

httpServerCodecUsesContext now reports true for any non-JSON-RPC endpoint with HTTP errors, and still skips the response encoder case for WebSocket and redirect endpoints. Behavior only changes for WebSocket endpoints with mapped errors.

The new test renders a WebSocket-only service with and without a mapped error. It checks the planned server codec imports, then compiles the generated packages.

go test ./http/... ./codegen/...

@raphael

raphael commented Oct 28, 2026

Copy link
Copy Markdown
Member

This is great, thank you!

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.

2 participants