Entry checking crashes further code when there is no entry found

I have this code below in a panel-plugin which works only if the myfile.txt exists and returns “true”, but if the file doesn’t exist then all the code after gets frozen and I am not getting a “false” in the console log

const fs = require("uxp").storage.localFileSystem;
const folder= await fs.getDataFolder();
const file= await folder.getEntry("myfile.txt");
console.log(file.isFile);

Is this a bug ?
XD version: 29.0.32 (windows)

Same is happening with file.name or file.isEntry etc.
I get no error in the console.

PS: to create the file in that directory:

const codeFile = await settingsFolder.createFile("myfile.txt", { overwrite: true});

I guess it’s because the “isFile” is a property of an Entry, when there is no entry (in this case the file), then it probably gets upset that you want to reference the property “isFile” of undefined.
You could, however, make some sort of helper function like this :

// File   -> A file name+ext (e.x "myFile.json")
// Folder -> A folder instance (not a path) || Empty
// Folder param is empty => folder = DataFolder
const isAFile = (file, folder=null) => {
    return new Promise(async (res, rej) => {
        try {
            folder        = folder === null ? await fs.getDataFolder() : folder
            let entries   = await folder.getEntries()
            let exists    = entries.filter(e => e.name === file && e.isFile).length > 0
            res(exists)
        } 
        catch(err) { rej(err) }
    })
}

I would do it like this, but if someone has a more “conventional” approach, I’d like to know as well…

1 Like