Skip to content

Commit

Permalink
core/utils/fs: Check for doc presence when handling trash errors (#2120)
Browse files Browse the repository at this point in the history
The error message thrown by Electron's `shell.trashItem()` method is
localized on macOS. Therefore, we can't detect missing document error
by matching the error message against a crafted RegExp as this would
mean building a RegExp that matches against every possible language.

e.g. when deleting missing `dir/file.txt`, the message would be:
- in English, `The file « file.txt » doesn't exist`
- in French, `Le fichier « file.txt » n'existe pas`

The only way to be sure `shell.trashItem()` failed because the
document does not exist anymore is to check whether the document
exists or not (minus some potential race condition issues).
This is slightly less efficient but works on all platforms for
every language.
  • Loading branch information
taratatach authored Aug 3, 2021
1 parent d8ad701 commit 8ca1b5d
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions core/utils/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const Promise = require('bluebird')
const childProcess = require('child_process')
const { shell } = require('electron')
const fse = require('fs-extra')

const logger = require('./logger')

Expand All @@ -18,10 +19,10 @@ const log = logger({
*
* Errors are logged, not thrown.
*/
async function hideOnWindows(path /*: string */) /*: Promise<void> */ {
async function hideOnWindows(fullpath /*: string */) /*: Promise<void> */ {
if (process.platform !== 'win32') return
try {
await childProcess.execAsync(`attrib +h "${path}"`)
await childProcess.execAsync(`attrib +h "${fullpath}"`)
} catch (err) {
log.warn(err)
}
Expand All @@ -41,19 +42,15 @@ async function sendToTrash(fullpath /*: string */) {
try {
await shell.trashItem(fullpath)
} catch (err) {
if (
err.message === 'Failed to move item to trash' || // Error message on Linux
err.message === 'Failed to parse path' || // Error message on Windows
/doesn’t exist/.test(err.message) // Error message on macOS
) {
if (await fse.exists(fullpath)) {
throw err
} else {
const error = new Error()
error.code = 'ENOENT'
error.path = fullpath
error.message = `${error.code}: No such file or directory, sendToTrash '${error.path}'`
throw error
}

throw err
}
}

Expand Down

0 comments on commit 8ca1b5d

Please sign in to comment.