From c9a4622cf1709e8b4a6456a373010ab9ff0887e7 Mon Sep 17 00:00:00 2001 From: Paul Biggar Date: Sat, 12 Mar 2022 13:24:43 -0500 Subject: [PATCH] Don't spam warnings on expected exceptions (#82) * Don't spam warnings on expected exceptions * Put try/except in the right place --- watchgod/watcher.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/watchgod/watcher.py b/watchgod/watcher.py index 3fae213..c4c07b7 100644 --- a/watchgod/watcher.py +++ b/watchgod/watcher.py @@ -56,11 +56,21 @@ def _walk_dir(self, dir_path: str, changes: Set['FileChange'], new_files: Dict[s if self.ignored_paths is not None and os.path.join(dir_path, entry) in self.ignored_paths: continue - if entry.is_dir(): - if self.should_watch_dir(entry): - self._walk_dir(entry.path, changes, new_files) - elif self.should_watch_file(entry): - self._watch_file(entry.path, changes, new_files, entry.stat()) + try: + if entry.is_dir(): + if self.should_watch_dir(entry): + self._walk_dir(entry.path, changes, new_files) + elif self.should_watch_file(entry): + self._watch_file(entry.path, changes, new_files, entry.stat()) + except FileNotFoundError as e: + # sometimes we can't find the file. If it was deleted since + # `entry` was allocated, then it doesn't matter and can be + # ignored. It might also be a bad symlink, in which case we + # should silently skip it - users don't want to constantly spam + # warnings, esp if they can't remove the symlink (eg from a + # node_modules directory). + pass + def check(self) -> Set['FileChange']: changes: Set['FileChange'] = set() @@ -68,7 +78,7 @@ def check(self) -> Set['FileChange']: try: self._walk(self.root_path, changes, new_files) except OSError as e: - # happens when a directory has been deleted between checks + # check for unexpected errors logger.warning('error walking file system: %s %s', e.__class__.__name__, e) # look for deleted