]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: build error chains in transport that can be unwrapped
authorMarcus Weiner <marcus.weiner@gmail.com>
Wed, 9 Nov 2022 10:37:28 +0000 (10:37 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 10 Nov 2022 18:45:41 +0000 (18:45 +0000)
In some places of the HTTP transport errors were constructed that
wrapped other errors without providing the ability to call
`errors.Unwrap` on them to get the underlying error.
These places have been fixed to use `%w` when using `fmt.Errorf`
or to implement `Unwrap() error`.

Fixes #56435

Change-Id: Ieed3359281574485c8d0b18298e25e5f1e14555c
GitHub-Last-Rev: 504efbc507a50bd2cf63001511733e232927089f
GitHub-Pull-Request: golang/go#56451
Reviewed-on: https://go-review.googlesource.com/c/go/+/445775
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Damien Neil <dneil@google.com>

src/net/http/transport.go

index 2a508ec41b1e0a91f06b94f5e4ee59b16b2a0005..184cf275183388a3bfde9340676d773a70013c91 100644 (file)
@@ -2057,7 +2057,7 @@ func (pc *persistConn) mapRoundTripError(req *transportRequest, startBytesWritte
                if pc.nwrite == startBytesWritten {
                        return nothingWrittenError{err}
                }
-               return fmt.Errorf("net/http: HTTP/1.x transport connection broken: %v", err)
+               return fmt.Errorf("net/http: HTTP/1.x transport connection broken: %w", err)
        }
        return err
 }
@@ -2264,7 +2264,7 @@ func (pc *persistConn) readLoopPeekFailLocked(peekErr error) {
                // common case.
                pc.closeLocked(errServerClosedIdle)
        } else {
-               pc.closeLocked(fmt.Errorf("readLoopPeekFailLocked: %v", peekErr))
+               pc.closeLocked(fmt.Errorf("readLoopPeekFailLocked: %w", peekErr))
        }
 }
 
@@ -2398,6 +2398,10 @@ type nothingWrittenError struct {
        error
 }
 
+func (nwe nothingWrittenError) Unwrap() error {
+       return nwe.error
+}
+
 func (pc *persistConn) writeLoop() {
        defer close(pc.writeLoopDone)
        for {
@@ -2635,7 +2639,7 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
                                req.logf("writeErrCh resv: %T/%#v", err, err)
                        }
                        if err != nil {
-                               pc.close(fmt.Errorf("write error: %v", err))
+                               pc.close(fmt.Errorf("write error: %w", err))
                                return nil, pc.mapRoundTripError(req, startBytesWritten, err)
                        }
                        if d := pc.t.ResponseHeaderTimeout; d > 0 {