diff options
author | Peng Gao <peng.gao.dut@gmail.com> | 2021-03-31 15:00:45 +0000 |
---|---|---|
committer | Damien Neil <dneil@google.com> | 2021-04-16 16:36:57 +0000 |
commit | 2f0e5bf907b16a8106ae68920edffe90362e16ce (patch) | |
tree | c8491debb3f248fee6c068e64ed997e460ba8e98 | |
parent | abbb82957d823f249dd39e38d448691e86c978bc (diff) | |
download | go-git-2f0e5bf907b16a8106ae68920edffe90362e16ce.tar.gz |
net/http: using errors.Is in fs error detection
Compare error by errors.Is to detect wrapped fs errors.
Fixes #44923
Change-Id: Idf32b96a661728278b7006c3b3bcc581b8588259
GitHub-Last-Rev: dba01ddae06947fb8c6047ddfba108acd650f446
GitHub-Pull-Request: golang/go#45314
Reviewed-on: https://go-review.googlesource.com/c/go/+/306051
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
-rw-r--r-- | src/net/http/fs.go | 6 | ||||
-rw-r--r-- | src/net/http/fs_test.go | 10 |
2 files changed, 9 insertions, 7 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go index a28ae85958..57e731e481 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -46,7 +46,7 @@ type Dir string // to a possibly better non-nil error. In particular, it turns OS-specific errors // about opening files in non-directories into fs.ErrNotExist. See Issue 18984. func mapDirOpenError(originalErr error, name string) error { - if os.IsNotExist(originalErr) || os.IsPermission(originalErr) { + if errors.Is(originalErr, fs.ErrNotExist) || errors.Is(originalErr, fs.ErrPermission) { return originalErr } @@ -670,10 +670,10 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec // and historically Go's ServeContent always returned just "404 Not Found" for // all errors. We don't want to start leaking information in error messages. func toHTTPError(err error) (msg string, httpStatus int) { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { return "404 page not found", StatusNotFound } - if os.IsPermission(err) { + if errors.Is(err, fs.ErrPermission) { return "403 Forbidden", StatusForbidden } // Default: diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go index 1bf207e0f7..b42ade1e8a 100644 --- a/src/net/http/fs_test.go +++ b/src/net/http/fs_test.go @@ -1265,16 +1265,18 @@ func TestFileServerNotDirError(t *testing.T) { if err == nil { t.Fatal("err == nil; want != nil") } - if !os.IsNotExist(err) { - t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err)) + if !errors.Is(err, fs.ErrNotExist) { + t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err, + errors.Is(err, fs.ErrNotExist)) } _, err = dir.Open("/index.html/not-a-dir/not-a-file") if err == nil { t.Fatal("err == nil; want != nil") } - if !os.IsNotExist(err) { - t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err)) + if !errors.Is(err, fs.ErrNotExist) { + t.Errorf("err = %v; errors.Is(err, fs.ErrNotExist) = %v; want true", err, + errors.Is(err, fs.ErrNotExist)) } }) } |