« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/playlist-utils.js18
-rw-r--r--todo.txt1
2 files changed, 17 insertions, 2 deletions
diff --git a/src/playlist-utils.js b/src/playlist-utils.js
index 765a317..479b0c5 100644
--- a/src/playlist-utils.js
+++ b/src/playlist-utils.js
@@ -293,7 +293,7 @@ function isTrack(obj) {
   // return typeof array[1] === 'string'
 }
 
-function safeUnlink(file, playlist) {
+async function safeUnlink(file, playlist) {
   if (!playlist) {
     throw new Error('No playlist given to safe-unlink.')
   }
@@ -311,7 +311,21 @@ function safeUnlink(file, playlist) {
     )
   }
 
-  return unlink(file)
+  try {
+    await unlink(file)
+  } catch(err) {
+    if (err.code === 'ENOENT') {
+      console.trace(
+        `Attempted to delete file "${file}" which does not exist. This ` +
+        'could be because of a temporary file being automatically deleted ' +
+        'by the system before now, or because of http-music attempting to ' +
+        'delete a temporary file which it has already deleted; otherwise ' +
+        'this is almost certainly a bug.'
+      )
+    } else {
+      throw err
+    }
+  }
 }
 
 module.exports = {
diff --git a/todo.txt b/todo.txt
index 8f537c8..34cafab 100644
--- a/todo.txt
+++ b/todo.txt
@@ -302,6 +302,7 @@ TODO: safeUnlink probably shouldn't throw/reject if it fails to delete its
       given file (e.g. if a temporary file was automatically purged by the
       system before safeUnlink). Having a warning might be better? (Since
       *most* of the time it's probably a bug.)
+      (Done!)
 
 TODO: A shuffle-groups picker would be fun! (That is, it would take a
       grouplike, then shuffle all the items on the TOP level; so it would