Skip to content

error: add ConnectError::into_http_response for short-circuiting layers - #111

Merged
iainmcgin merged 2 commits into
mainfrom
iain/error-into-http-response
May 18, 2026
Merged

iainmcgin merged 2 commits into
mainfrom
iain/error-into-http-response

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

Tower layers wrapping ConnectRpcService — auth, rate limiting, validation — need to deny a request without dispatching it. They cannot reuse the service's internal protocol-aware error rendering (it's private), so today they hand-roll a Connect-unary JSON response regardless of the inbound protocol. Connect-unary clients are happy; gRPC, gRPC-Web, and Connect-streaming clients see a transport-level failure instead of a structured permission_denied/unauthenticated error, because they expected an HTTP 200 with trailers / an EndStreamResponse envelope.

What

ConnectError::into_http_response(self, &http::HeaderMap) -> http::Response<ConnectRpcBody> detects the inbound protocol from the request headers via the (already public) Protocol::detect and delegates to the existing internal helpers — streaming_error_response for Connect streaming / gRPC / gRPC-Web, error_response_either for Connect unary. Unrecognized or absent Content-Type (Connect GET, non-RPC traffic) falls back to Connect-unary JSON, the most universally parseable shape.

// inside a tower::Service::call wrapping ConnectRpcService:
if !authorized {
    return Box::pin(std::future::ready(Ok(
        ConnectError::permission_denied("access denied")
            .into_http_response(req.headers()),
    )));
}

Confidence

11 unit tests covering the dispatch matrix: Connect unary (proto + json + no-Content-Type + unknown Content-Type), Connect streaming (proto + json codec), gRPC (proto + json), gRPC-Web (binary + text mode), and Content-Type with parameters. Each asserts HTTP status, response Content-Type, and the protocol-specific body shape (envelope flag bytes, grpc-status header, JSON code/message fields). cargo fmt clean, cargo clippy --all-targets -- -D warnings clean, cargo doc introduces no new warnings.

🤖 Generated with Claude Code

Tower layers wrapping ConnectRpcService (auth, rate limiting, validation)
need to deny a request without dispatching it. They cannot reuse the
service's internal protocol-aware error rendering, which is private, so
they end up hand-rolling the Connect-unary JSON shape regardless of the
inbound protocol. That works for Connect-unary clients but produces a
transport error rather than a structured permission_denied/unauthenticated
for gRPC, gRPC-Web, and Connect-streaming clients.

ConnectError::into_http_response detects the protocol from the request
headers via Protocol::detect and delegates to the existing internal
helpers, so a layer can produce the same wire shape the service itself
would. Falls back to Connect-unary JSON for unrecognized or absent
Content-Type (Connect GET, non-RPC requests).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

github-actions Bot commented May 18, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@iainmcgin
iainmcgin marked this pull request as ready for review May 18, 2026 16:51
@iainmcgin
iainmcgin requested a review from rpb-ant May 18, 2026 16:51
@iainmcgin
iainmcgin merged commit c887734 into main May 18, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants