[go] net/http: add ProxyConnectError type for proxy CONNECT failures

3 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Feb 6, 2026, 10:15:23 AM (16 hours ago) Feb 6
to goph...@pubsubhelper.golang.org, Logan Davies, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

net/http: add ProxyConnectError type for proxy CONNECT failures

## Summary

Introduces `ProxyConnectError` type to provide structured access to proxy response details when HTTP CONNECT requests fail with non-200 status codes. Maintains full backward compatibility with existing error strings.

## Problem

When proxies return non-200 status codes during CONNECT, Go currently returns generic error strings, making it impossible to access potentially useful diagnostic information like `X-Squid-Error` headers, custom error codes, or response bodies.

## Motivation

This addresses a long-standing issue (#30560) where users cannot access proxy error details during failed CONNECT attempts. Real-world scenarios include:

- **Squid proxies** returning 503 errors with `X-Squid-Error` headers containing detailed failure reasons
- **Corporate proxies** providing custom error codes and authentication requirements
- **Load balancers** including retry hints or upstream service information
- **Health check failures** where response bodies contain diagnostic data

Currently, all this information is lost, and users only see generic errors like "Service Unavailable".

## Solution

\`\`\`go
// ProxyConnectError holds the response to an unsuccessful proxy CONNECT request.
type ProxyConnectError struct {
Response *Response
}
\`\`\`

## Backward Compatibility

The \`Error()\` method returns only the status text (e.g., "Service Unavailable") to match existing behavior exactly. Only callers explicitly using \`errors.As()\` gain access to the structured response.

## Testing

- New test \`TestRoundTripReturnsProxyConnectError\` verifies error type and backward compatibility
- All existing proxy tests pass

## Related Work

Based on #44123 by @logandavies181, updated for current \`master\` and adapted to resolve 4 years of code changes.

Fixes #30560
Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
GitHub-Last-Rev: 320d2a29a15ee86e903b07f92d99364b3940cc6f
GitHub-Pull-Request: golang/go#77479

Change diff

diff --git a/src/net/http/transport.go b/src/net/http/transport.go
index 924e7cf..44d89b5 100644
--- a/src/net/http/transport.go
+++ b/src/net/http/transport.go
@@ -1764,6 +1764,20 @@
RoundTripErr() error
}

+// ProxyConnectError holds the response to an unsuccessful proxy CONNECT request.
+type ProxyConnectError struct {
+ Response *Response
+}
+
+func (p *ProxyConnectError) Error() string {
+ // Don't return full Response.Status for backwards compatibility
+ f := strings.SplitN(p.Response.Status, " ", 2)
+ if len(f) < 2 {
+ return "unknown status code"
+ }
+ return f[1]
+}
+
var testHookProxyConnectTimeout = context.WithTimeout

func (t *Transport) dialConn(ctx context.Context, cm connectMethod, isClientConn bool, internalStateHook func()) (pconn *persistConn, err error) {
@@ -1929,12 +1943,10 @@
}

if resp.StatusCode != 200 {
- _, text, ok := strings.Cut(resp.Status, " ")
conn.Close()
- if !ok {
- return nil, errors.New("unknown status code")
+ return nil, &ProxyConnectError{
+ Response: resp,
}
- return nil, errors.New(text)
}
}

diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go
index 37e6cbb..a4d2dbe 100644
--- a/src/net/http/transport_test.go
+++ b/src/net/http/transport_test.go
@@ -4329,6 +4329,41 @@
}
}

+// Test for issue 38143
+// Return an error containing the proxy CONNECT request if status is not 200
+func TestRoundTripReturnsProxyConnectError(t *testing.T) {
+ s := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+ w.WriteHeader(StatusServiceUnavailable)
+ }))
+ defer s.Close()
+
+ proxyURL, err := url.Parse(s.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tr := &Transport{
+ Proxy: ProxyURL(proxyURL),
+ }
+
+ req, err := NewRequest("GET", "https://example.com", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ _, got := tr.RoundTrip(req)
+
+ switch errType := got.(type) {
+ case *ProxyConnectError:
+ default:
+ t.Errorf("Wrong error type returned, Got: %T, Want: *ProxyConnectError", errType)
+ }
+
+ // Test backwards compatibility
+ if got.Error() != "Service Unavailable" {
+ t.Error("Non-backwards compatible error message from *ProxyConnectError")
+ }
+}
+
// tests that putting an idle conn after a call to CloseIdleConns does return it
func TestTransportCloseIdleConnsThenReturn(t *testing.T) {
tr := &Transport{}

Change information

Files:
  • M src/net/http/transport.go
  • M src/net/http/transport_test.go
Change size: M
Delta: 2 files changed, 51 insertions(+), 4 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
Gerrit-Change-Number: 742760
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Logan Davies <logan.d...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Feb 6, 2026, 10:15:26 AM (16 hours ago) Feb 6
to Logan Davies, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You have a long 243 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
    Gerrit-Change-Number: 742760
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Logan Davies <logan.d...@gmail.com>
    Gerrit-Comment-Date: Fri, 06 Feb 2026 15:15:20 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Feb 6, 2026, 10:25:53 AM (16 hours ago) Feb 6
    to Logan Davies, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Damien Neil

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Damien Neil
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
    Gerrit-Change-Number: 742760
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Logan Davies <logan.d...@gmail.com>
    Gerrit-CC: Russ Cox <r...@golang.org>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    unsatisfied_requirement
    open
    diffy

    Florent Lecoultre (Gerrit)

    unread,
    Feb 6, 2026, 10:32:19 AM (16 hours ago) Feb 6
    to Logan Davies, Gerrit Bot, goph...@pubsubhelper.golang.org, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Damien Neil

    Florent Lecoultre added 1 comment

    Patchset-level comments
    File-level comment, Patchset 1:
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. You have a long 243 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
    2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Florent Lecoultre

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Damien Neil
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
      Gerrit-Change-Number: 742760
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-CC: Florent Lecoultre <florent....@datadoghq.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Logan Davies <logan.d...@gmail.com>
      Gerrit-CC: Russ Cox <r...@golang.org>
      Gerrit-Attention: Damien Neil <dn...@google.com>
      Gerrit-Comment-Date: Fri, 06 Feb 2026 15:32:11 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Feb 6, 2026, 10:41:14 AM (15 hours ago) Feb 6
      to Logan Davies, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Damien Neil

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #3 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Damien Neil
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
      Gerrit-Change-Number: 742760
      Gerrit-PatchSet: 3
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Sean Liao (Gerrit)

      unread,
      Feb 6, 2026, 2:48:13 PM (11 hours ago) Feb 6
      to Logan Davies, Gerrit Bot, goph...@pubsubhelper.golang.org, Florent Lecoultre, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Damien Neil

      Sean Liao voted and added 1 comment

      Votes added by Sean Liao

      Hold+1

      1 comment

      Patchset-level comments
      File-level comment, Patchset 3 (Latest):
      Sean Liao . resolved

      new api would need an accepted proposal

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Damien Neil
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Holds
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
        Gerrit-Change-Number: 742760
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Sean Liao <se...@liao.dev>
        Gerrit-CC: Florent Lecoultre <florent....@datadoghq.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Logan Davies <logan.d...@gmail.com>
        Gerrit-CC: Russ Cox <r...@golang.org>
        Gerrit-Attention: Damien Neil <dn...@google.com>
        Gerrit-Comment-Date: Fri, 06 Feb 2026 19:48:06 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy

        Damien Neil (Gerrit)

        unread,
        Feb 6, 2026, 6:12:44 PM (8 hours ago) Feb 6
        to Logan Davies, Gerrit Bot, goph...@pubsubhelper.golang.org, Florent Lecoultre, Russ Cox, Gopher Robot, golang-co...@googlegroups.com

        Damien Neil added 1 comment

        Patchset-level comments
        Sean Liao . unresolved

        new api would need an accepted proposal

        Damien Neil

        As Sean says, any new exported symbols must go through the proposal process:
        https://github.com/golang/proposal

        The general idea of `ProxyConnectError` sounds reasonable to me on first glance. The proposal process tends to grind very slowly, but if you want to file a short proposal for this I think it has a reasonable chance of being accepted.

        Open in Gerrit

        Related details

        Attention set is empty
        Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement is not satisfiedNo-Holds
          • requirement is not satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: comment
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
          Gerrit-Change-Number: 742760
          Gerrit-PatchSet: 3
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Sean Liao <se...@liao.dev>
          Gerrit-CC: Florent Lecoultre <florent....@datadoghq.com>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-CC: Logan Davies <logan.d...@gmail.com>
          Gerrit-CC: Russ Cox <r...@golang.org>
          Gerrit-Comment-Date: Fri, 06 Feb 2026 23:12:40 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: No
          Comment-In-Reply-To: Sean Liao <se...@liao.dev>
          unsatisfied_requirement
          open
          diffy

          jub0bs (Gerrit)

          unread,
          Feb 6, 2026, 9:59:11 PM (4 hours ago) Feb 6
          to Logan Davies, Gerrit Bot, goph...@pubsubhelper.golang.org, Florent Lecoultre, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com

          jub0bs added 1 comment

          File src/net/http/transport.go
          Line 1774, Patchset 3 (Latest): f := strings.SplitN(p.Response.Status, " ", 2)
          jub0bs . unresolved

          Why not simply use `strings.Cut`?

          Open in Gerrit

          Related details

          Attention set is empty
          Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement is not satisfiedNo-Holds
          • requirement is not satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: comment
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: Iecc50ac4f30f4b84e974489e0de8aa85acb1bdc4
          Gerrit-Change-Number: 742760
          Gerrit-PatchSet: 3
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Sean Liao <se...@liao.dev>
          Gerrit-CC: Florent Lecoultre <florent....@datadoghq.com>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-CC: Logan Davies <logan.d...@gmail.com>
          Gerrit-CC: Russ Cox <r...@golang.org>
          Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
          Gerrit-Comment-Date: Sat, 07 Feb 2026 02:59:04 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: No
          unsatisfied_requirement
          open
          diffy
          Reply all
          Reply to author
          Forward
          0 new messages