Cannot handle an error inside a promise


Cannot handle an error inside a promise



I have this function that takes a path and fetches some information about it (using node.js capabilities)


import fs from 'fs'
import fse from 'fs-extra'
import { join } from 'path'


/**
* Take in file path and return contents with stat
* @param {String} path
* @return {Array}
*/
export default path => {
return readdir(path).then(files => {
const statsPromises = files.map((file, i) => {
const p = join(path, file)
return stat(p).then(stat =>
({
// THIS ONE LINE BELOW THROWS UN-HANDLED ERRORS FOR SOME PATHS
// Error: EPERM: operation not permitted, scandir
folderItemsCount: stat.isDirectory() ? fs.readdirSync(p).length : null,
name: file,
path: p,
stat
})
, err => console.log("Path access error ", err))
})
return Promise.all(statsPromises)
}).then(contents =>
contents.filter(Boolean)
)
}


// ============== HELPER FUNCTIONS ==============
/**
* Get Array for path contents. Checks if path exists
* @param {String} path
* @return {Promise}
*/
export function readdir (path) {
return pathExists(path)
? fse.readdir(path)
: Promise.reject(new Error('Path does not exist'))
}

/**
* Alias of fse.existsSync
* @param {String} path
* @return {Boolean}
*/
export function pathExists (path) {
return fse.existsSync(path)
}

/**
* Get path stat
* @param {String} path
* @return {Promise}
*/
export function stat (path) {
return fse.stat(path)
}



I don't know how to handle the error for the following line:


folderItemsCount: stat.isDirectory() ? fs.readdirSync(p).length : null



I want it to:



return a number of items if it can access a folder (this does work when it checks accessible dirs)



return a string "unknown" (or null) if it cannot access a folder instead of throwing an un-handled error: Error: EPERM: operation not permitted, scandir


Error: EPERM: operation not permitted, scandir



The line below handles stat() but not scandir()


stat()


scandir()


, err => console.log("Path access error ", err))




2 Answers
2



Without digging too much into your code, a promise either resolves or rejects, which your handlers either hit the 'then' or the 'catch'. I see you're using .then, but not .catch. Also keep in mind that Promise.all will reject if any promise in that list rejects, so you've got an all-or-nothing promise there.





I'm new to promises, not sure how to improve it. But it works fine if I'm not fetching folderItemsCount:. If it cannot access a path, it just logs the path that it couldn't access in stat() with this line: , err => console.log("Path access error ", err)). But I need to fetch folderItemsCount: and it's not getting handled by anything right now, hence scandir() throws an un-handled error for some paths
– Un1
Jun 30 at 19:33



folderItemsCount:


stat()


, err => console.log("Path access error ", err))


folderItemsCount:


scandir()



I solved the problem by adding a try/catch


try/catch


export default path => {
return readdir(path).then(files => {
const statsPromises = files.map((file, i) => {
const p = join(path, file)
return stat(p).then(stat =>
let countResult
if (stat.isDirectory()) {
try {
countResult = fs.readdirSync(p).length
}
catch (error) {
countResult = 'unknown'
}
} else { countResult = null }
return ({
folderItemsCount: countResult,
name: file,
path: p,
stat
})
, err => console.log("Path access error ", err))
})
return Promise.all(statsPromises)
}).then(contents =>
contents.filter(Boolean)
)
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API