pythonqtpyqtpysideqfilesystemwatcher

QFileSystemWatcher error handling with PySide/PyQt - Python 3.x


My program utilises Qt's QFileSystemWatcher function to monitor a network directory (not on the local machine itself) for changes, and then run a script when a change is found. This function performs as required for the most part. The program is designed to run 24/7, which has raised some issues using this particular function.

The error which is causing issues is as follows:

QFileSystemWatcher: FindNextChangeNotification failed!! (The specified network name is no longer available.)

The functionality I'd like to implement is as follows:

  1. Build in error handling surrounding network availability for QFileSystemWatcher
  2. If the network becomes unavailable and the error is raised, go to Script()
  3. Run Script() for handling the unavailable network

Given that the QFileSystemWatcher function is established in the initialisation of the program, I'm not sure how to go about error handling. Here's the basic outline of my current code:

class Main(QMain, Ui_Main):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

        self.DirectoryWatcher = QtCore.QFileSystemWatcher([r'U:\NetworkAddress\Directory'])
        self.DirectoryWatcher.directoryChanged.connect(self.GoToThisDirectory)

    def GoToThisDirectory(self):
        print("foo")

Is there a way to explicitly establish error handling for the 'FindNextChangeNotification' error? Any input would be greatly appreciated!


Solution

  • As per ekhumoro's comments above, I've managed to solve this question using both the qInstallMsgHandler and sys.excepthook functions.

    import sys
    import os
    from PySide.QtCore import qInstallMsgHandler
    
    def myCustomHandler(ErrorType, ErrorContext):
        print("Qt error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Context: " + str(ErrorContext))
    
        #Error logging code
        #Error emailing code
    
        os.execv(sys.executable, [sys.executable] + sys.argv)
    
    qInstallMsgHandler(myCustomHandler)
    
    def ErrorHandling(ErrorType, ErrorValue, TraceBack):
        print("System error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Value: " + str(ErrorValue))
        print("Traceback: " + str(TraceBack))
    
        #Error logging code
        #Error emailing code
    
        os.execv(sys.executable, [sys.executable] + sys.argv)
    
    sys.excepthook = ErrorHandling
    
    #Rest of the script
    

    My solution addresses Qt and Python/system-related errors separately, but handles them in the same way. The error is logged in a .log file, emailed to the system administrator and the software is restarted. Thanks for guiding me in the right direction ekhumoro!