diff --git a/fsnotify.go b/fsnotify.go index 0f4ee52..e900a90 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -65,5 +65,6 @@ func (e Event) String() string { // Common errors that can be reported by a watcher var ( - ErrEventOverflow = errors.New("fsnotify queue overflow") + ErrNonExistentWatch = errors.New("fsnotify: can't remove non-existent watcher") + ErrEventOverflow = errors.New("fsnotify: queue overflow") ) diff --git a/inotify.go b/inotify.go index 24724a6..3cd8fec 100644 --- a/inotify.go +++ b/inotify.go @@ -134,7 +134,7 @@ func (w *Watcher) Remove(name string) error { // Remove it from inotify. if !ok { - return fmt.Errorf("can't remove non-existent inotify watch for: %s", name) + return fmt.Errorf("%w: %s", ErrNonExistentWatch, name) } // We successfully removed the watch if InotifyRmWatch doesn't return an diff --git a/inotify_test.go b/inotify_test.go index ef8ab78..361f198 100644 --- a/inotify_test.go +++ b/inotify_test.go @@ -8,6 +8,7 @@ package fsnotify import ( + "errors" "fmt" "os" "path/filepath" @@ -303,6 +304,8 @@ func TestInotifyRemoveTwice(t *testing.T) { err = w.Remove(testFile) if err == nil { t.Fatalf("no error on removing invalid file") + } else if !errors.Is(err, ErrNonExistentWatch) { + t.Fatalf("unexpected error %v on removing invalid file", err) } w.mu.Lock() @@ -384,7 +387,7 @@ func TestInotifyOverflow(t *testing.T) { for dn := 0; dn < numDirs; dn++ { testSubdir := fmt.Sprintf("%s/%d", testDir, dn) - err := os.Mkdir(testSubdir, 0777) + err := os.Mkdir(testSubdir, 0o777) if err != nil { t.Fatalf("Cannot create subdir: %v", err) } diff --git a/kqueue.go b/kqueue.go index 6fb8d85..11c8df5 100644 --- a/kqueue.go +++ b/kqueue.go @@ -74,7 +74,7 @@ func (w *Watcher) Close() error { w.isClosed = true // copy paths to remove while locked - var pathsToRemove = make([]string, 0, len(w.watches)) + pathsToRemove := make([]string, 0, len(w.watches)) for name := range w.watches { pathsToRemove = append(pathsToRemove, name) } @@ -107,7 +107,7 @@ func (w *Watcher) Remove(name string) error { watchfd, ok := w.watches[name] w.mu.Unlock() if !ok { - return fmt.Errorf("can't remove non-existent kevent watch for: %s", name) + return fmt.Errorf("%w: %s", ErrNonExistentWatch, name) } const registerRemove = unix.EV_DELETE diff --git a/windows.go b/windows.go index 02ce7de..d3b4670 100644 --- a/windows.go +++ b/windows.go @@ -316,7 +316,7 @@ func (w *Watcher) remWatch(pathname string) error { watch := w.watches.get(ino) w.mu.Unlock() if watch == nil { - return fmt.Errorf("can't remove non-existent watch for: %s", pathname) + return fmt.Errorf("%w: %s", ErrNonExistentWatch, pathname) } if pathname == dir { w.sendEvent(watch.path, watch.mask&sysFSIGNORED)