diff --git a/src/watchdog/events.py b/src/watchdog/events.py index 73e82e8ea..39bc380f3 100755 --- a/src/watchdog/events.py +++ b/src/watchdog/events.py @@ -95,6 +95,7 @@ EVENT_TYPE_DELETED = 'deleted' EVENT_TYPE_CREATED = 'created' EVENT_TYPE_MODIFIED = 'modified' +EVENT_TYPE_ERROR = 'error' class FileSystemEvent: @@ -243,6 +244,22 @@ class DirMovedEvent(FileSystemMovedEvent): is_directory = True +class FileObserverErrorEvent(FileSystemEvent): + """File system event representing an error in the monitor.""" + event_type = EVENT_TYPE_ERROR + + def __init__(self, src_path, exception): + self._exception = exception + super(FileObserverErrorEvent, self).__init__(src_path) + + @property + def exception(self): + return self._exception + + def __repr__(self): + return ("<%(class_name)s: exception=%(exception)r>" + ) % (dict(class_name=self.__class__.__name__, + exception=repr(self.exception))) class FileSystemEventHandler: """ diff --git a/src/watchdog/observers/api.py b/src/watchdog/observers/api.py index 6a30903d4..cd8350f06 100644 --- a/src/watchdog/observers/api.py +++ b/src/watchdog/observers/api.py @@ -19,6 +19,7 @@ import threading from pathlib import Path +from watchdog.events import FileObserverErrorEvent from watchdog.utils import BaseThread from watchdog.utils.bricks import SkipRepeatsQueue @@ -145,7 +146,10 @@ def queue_events(self, timeout): def run(self): while self.should_keep_running(): - self.queue_events(self.timeout) + try: + self.queue_events(self.timeout) + except Exception as ex: + self.queue_event(FileObserverErrorEvent(self._watch.path, ex)) class EventDispatcher(BaseThread):