Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: net/url: add method to filter 'too many redirects' out of url.Error #41628

Open
MexHigh opened this issue Sep 25, 2020 · 1 comment

Comments

@MexHigh
Copy link

MexHigh commented Sep 25, 2020

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).

@seankhliao
Copy link
Member

Probably a better idea to expose the error var for matching with errors.Is

return errors.New("stopped after 10 redirects")

@andybons andybons changed the title net/url: Bool method to filter 'too many redirects' out of url.Error proposal: net/url: add method to filter 'too many redirects' out of url.Error Sep 29, 2020
@gopherbot gopherbot added this to the Proposal milestone Sep 29, 2020
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants