Skip to content

proposal: net/http: expose 'too many redirects' error #41628

@MexHigh

Description

@MexHigh

What version of Go are you using (go version)?

$ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/.cache/go-build"
GOENV="/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go-packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build224090969=/tmp/go-build -gno-record-gcc-switches"

What did you do?

When trying to filter the error type from an error returned by client.Do(request), there is only the option to determine if it was a timeout. There is no other method to determine other specific types of errors.

client := &http.Client{}
request, _ := http.NewRequest("GET", "https://example.org", nil)
response, err := client.Do(request)

if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() {
  panic("Timeout!")
} else if err != nil {
  panic(err) // this could be a 'too many redirects' error
}

What did you expect to see?

client := &http.Client{}
request, _ := http.NewRequest("GET", "https://example.org", nil)
response, err := client.Do(request)

if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() {
  panic("Timeout!")
} else if ok && urlErr.TooManyRedirects() {
  panic("Too many redirects!")
} else if err != nil {
  panic(err)
}

I managed to scrape the error myself with the strings.Contains(err.Error(), "stopped after 10 redirects") function, but this just isn't elegant at all and my cause collisions with other errors (or even strings).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions