Skip to content

feat(proxy): expose TunnelError with the response status - #354

Open
orenjp wants to merge 1 commit into
hyperium:masterfrom
orenjp:tunnel-error-status
Open

orenjp wants to merge 1 commit into
hyperium:masterfrom
orenjp:tunnel-error-status

Conversation

@orenjp

@orenjp orenjp commented Nov 23, 2026 •

Copy link
Copy Markdown

when a CONNECT through Tunnel fails, you get a TunnelError back, but the type isn't exported, so the only thing you can really do with it is read the message. two cases bit me:

  • a 407 throws away the Proxy-Authenticate headers, so there's no way to know if the proxy wants Negotiate, NTLM or Basic
  • any other refusal is just "unsuccessful" and the status is gone, so a 403 from a proxy policy looks exactly like a 502 from a dead upstream

this PR exports TunnelError and adds two methods:

  • status() gives the status the proxy answered with (Some(407) for auth required), or None if there wasn't a valid one
  • proxy_authenticate() gives the Proxy-Authenticate values of a 407 as-is, no parsing

i went with an opaque struct (the old variants are now a private kind) because of what you wrote in #162 and in the pattern matching post, so more stuff can be added later as methods without breaking anyone. Display and source() are the same as before, so anyone matching on the log text is fine. Tunnel's response type isn't touched, and it merges cleanly with #320.

one thing to be upfront about: someone could already name the type through <Tunnel<C> as Service<Uri>>::Error and match on the variants, and that would stop compiling. i couldn't find anyone doing it and cargo-semver-checks is happy, but wanted to say it.

how i think it can help: reqwest uses Tunnel for https through a proxy, and people keep hitting this (seanmonstar/reqwest#2252, astral-sh/uv#23284). with this, a reqwest user can walk source(), downcast to TunnelError, and e.g. tell the user "your proxy wants NTLM" or "the proxy blocked this (403)" instead of "tunnel error: unsuccessful". it only needs a cargo update, nothing changes in reqwest. i checked that with reqwest 0.13.5 against a small local proxy.

  • The description, docs, and comments (words meant for humans) are written by a human, not by an LLM.

Export `TunnelError` from `connect::proxy`, so a caller can downcast the error
of a failed `CONNECT` and classify it without matching error text.

`TunnelError::status()` returns the status of a refused `CONNECT`.
`TunnelError::proxy_authenticate()` returns the `Proxy-Authenticate` values of
a 407 response. The error stays opaque, so more information can come later as
new methods.
@seanmonstar

Copy link
Copy Markdown
Member

I don't think I want to export a new error type.

I do welcome improving the error messages that get reported.

@orenjp

orenjp commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

I don't think I want to export a new error type.

I do welcome improving the error messages that get reported.

@seanmonstar
How you suggest users to match the different scenarios of proxy auth? And act differently

@orenjp

orenjp commented Nov 24, 2026

Copy link
Copy Markdown
Author

Error messages will give at most

tunnel error: proxy authorization required (Negotiate, NTLM)
tunnel error: unsuccessful (403 Forbidden)

but its not matchable, without a contract (the error type i suggest)

This branch has not been deployed

No deployments
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