From 5a22c00969ca85e2a020645ccaf02fffa89d95ca Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sun, 14 Feb 2016 19:32:03 -0800 Subject: [PATCH] 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 --- godoc/vfs/zipfs/zipfs.go | 2 +- godoc/vfs/zipfs/zipfs_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/godoc/vfs/zipfs/zipfs.go b/godoc/vfs/zipfs/zipfs.go index ca69d8c8ad..e554446e46 100644 --- a/godoc/vfs/zipfs/zipfs.go +++ b/godoc/vfs/zipfs/zipfs.go @@ -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 diff --git a/godoc/vfs/zipfs/zipfs_test.go b/godoc/vfs/zipfs/zipfs_test.go index 57efb49d83..19407e489c 100644 --- a/godoc/vfs/zipfs/zipfs_test.go +++ b/godoc/vfs/zipfs/zipfs_test.go @@ -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 {