1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:44:45 -07:00

godoc/vfs/zipfs: use an os.IsNotExist-compatible error

When a file is not found, it's common to return an error that can
be detected with os.IsNotExist helper. It's possible to use
os.PathError type to satisfy that requirement while still providing
the path information in the error.

Add a test that files that are not found return a non-nil error, and
that the error satisfies os.IsNotFound.

Change-Id: I5f1a26b18f2556af822ede73306541e8575ede28
Reviewed-on: https://go-review.googlesource.com/19503
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Dmitri Shuralyov 2016-02-14 19:32:03 -08:00 committed by Andrew Gerrand
parent 93ea01aea0
commit 5a22c00969
2 changed files with 11 additions and 1 deletions

View File

@ -112,7 +112,7 @@ func (fs *zipFS) stat(abspath string) (int, zipFI, error) {
i, exact := fs.list.lookup(zippath)
if i < 0 {
// zippath has leading '/' stripped - print it explicitly
return -1, zipFI{}, fmt.Errorf("file not found: /%s", zippath)
return -1, zipFI{}, &os.PathError{Path: "/" + zippath, Err: os.ErrNotExist}
}
_, name := path.Split(zippath)
var file *zip.File

View File

@ -150,6 +150,16 @@ func TestZipFSStatFuncs(t *testing.T) {
}
}
func TestZipFSNotExist(t *testing.T) {
_, err := fs.Open("/does-not-exist")
if err == nil {
t.Fatalf("Expected an error.\n")
}
if !os.IsNotExist(err) {
t.Errorf("Expected an error satisfying os.IsNotExist: %v\n", err)
}
}
func TestZipFSOpenSeek(t *testing.T) {
for _, test := range tests {
if test.IsRegular {