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

net/http: toHTTPError() should use errors.Is() function #44923

Closed
claudiofelber opened this issue Mar 10, 2021 · 3 comments
Closed

net/http: toHTTPError() should use errors.Is() function #44923

claudiofelber opened this issue Mar 10, 2021 · 3 comments
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@claudiofelber
Copy link

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

$ go version
go version go1.16 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Claudio\AppData\Local\go-build
set GOENV=C:\Users\Claudio\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Claudio\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Claudio\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Development\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Development\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Claudio\AppData\Local\Temp\go-build3762994767=/tmp/go-build -gno-record-gcc-switches

What did you do?

I am using http.FileSystem with some code that wraps errors with a stack trace:

import (
    ...
    "github.com/pkg/errors"
)

func (cfs *CachingFileSystem) Open(name string) (http.File, error) {
    name = strings.TrimPrefix(name, "/")
    originalName := cfs.reverseCacheMap[name]
    if originalName == "" {
        originalName = name
    }
    f, err := cfs.FileSystem.Open(originalName)
    if os.IsNotExist(err) {
        f, err = cfs.openExternal(originalName)
    }
    return f, errors.WithStack(err)
}

When a file does not exists, the wrapped os.ErrNotExist is not detected by the net/http toHTTPError() function, because it is still using os.IsNotExists(err) and therefore the function returns StatusInternalServerError instead of StatusNotFound. See:

go/src/net/http/fs.go

Lines 673 to 675 in 1811aea

if os.IsNotExist(err) {
return "404 page not found", StatusNotFound
}

By replacing os.IsNotExists(err) with errors.Is(err, os.ErrNotExist) the os.ErrNotExists condition is dectected correctly:

if errors.Is(err, os.ErrNotExist) {
    return "404 page not found", StatusNotFound
}

What did you expect to see?

toHTTPError() returning StatusNotFound

What did you see instead?

toHTTPError() returning StatusInternalServerError

@seankhliao seankhliao changed the title net/http toHTTPError() should use errors.Is() function net/http: toHTTPError() should use errors.Is() function Mar 10, 2021
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 10, 2021
@seankhliao
Copy link
Member

CC @bradfitz @rsc @empijei via owners.

@neild
Copy link
Contributor

neild commented Mar 21, 2021

This should compare against fs.ErrNotExist and fs.ErrPermission. (Which are the same errors as in the io package, but fs is more canonical.

The mapDirOpenError function should also be updated to use errors.Is.

With those caveats, I think this change seems reasonable.

ggaaooppeenngg added a commit to ggaaooppeenngg/go that referenced this issue Mar 31, 2021
compare error by errors.Is to detect wrapped fs errors.

Fixes golang#44923
ggaaooppeenngg added a commit to ggaaooppeenngg/go that referenced this issue Mar 31, 2021
Compare error by errors.Is to detect wrapped fs errors.

Fixes golang#44923
@gopherbot
Copy link

Change https://golang.org/cl/306051 mentions this issue: net/http: using errors.Is in fs error detection

@golang golang locked and limited conversation to collaborators Apr 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants