0

I am saving a FileSystemDirectoryHandle to IndexedDB, so that the web application can reuse it the next time it is started. This usually works fine. But when the directory has been removed from disk in the meantime, and the app still tries to work with it (for example by trying to obtain a file handle inside the directory via directoryHandle.getFileHandle()), an error is thrown, unsurprisingly: NotFoundError: A requested file or directory could not be found at the time an operation was processed.

How to check if the directory still exists before doing anything else with it? In Node.js, there are functions for checking if a directory exists like fs.access and fs.existsSync. But I do not find anything like that with the File System Access API.

Sebastian
  • 1,710
  • 2
  • 16
  • 28

1 Answers1

1

Your best option right now is indeed try..catch-ing the desired operation and checking the type of the error to equal NotFoundError. The spec has two open issues around improving this: Issue #121 and Issue #244.

DenverCoder9
  • 2,024
  • 11
  • 32
  • Thanks for the hint. The thing is that I don't want to execute the desired operation at the same time as the check for the directory. The app should inform the user already at startup that the directory is no longer there. I guess I will just have to create a test operation at startup (e. g. creating an empty file called `test.txt`) inside the directory and on success undo it again. – Sebastian Feb 21 '22 at 14:08
  • 1
    Yes, a no-op (or rather an operation that you undo immediately) is the way to go for now. There are thought around proper watching and change notifications, you can subscribe to this [issue](https://github.com/WICG/file-system-access/issues/72) for updates on this. – DenverCoder9 Feb 22 '22 at 15:10