Explorar o código

fix: Improve error handling in `mkdir` function (#9247)

* Improve error handling in `mkdir-if-not-exists` function
This PR improves the error handling in the `mkdir-if-not-exists` function by logging the "EEXIST" error (which indicates the directory already exists) instead of throwing it. Other errors will continue to be handled as before.
- [x] closes #9182

* enhance: move error handling to fs/node.js

* fix: make EEXIST silent and only throw other errors
Bad3r %!s(int64=2) %!d(string=hai) anos
pai
achega
aebfae729f
Modificáronse 2 ficheiros con 23 adicións e 10 borrados
  1. 6 8
      src/main/frontend/fs.cljs
  2. 17 2
      src/main/frontend/fs/node.cljs

+ 6 - 8
src/main/frontend/fs.cljs

@@ -194,14 +194,12 @@
 
 (defn mkdir-if-not-exists
   [dir]
-  (->
-   (when dir
-     (util/p-handle
-      (stat dir)
-      (fn [_stat])
-      (fn [_error]
-        (mkdir! dir))))
-   (p/catch (fn [error] (js/console.error error)))))
+  (when dir
+    (util/p-handle
+     (stat dir)
+     (fn [_stat])
+     (fn [_error]
+       (mkdir! dir)))))
 
 ;; FIXME: counterintuitive return value
 (defn create-if-not-exists

+ 17 - 2
src/main/frontend/fs/node.cljs

@@ -80,24 +80,33 @@
 (defrecord Node []
   protocol/Fs
   (mkdir! [_this dir]
-    (ipc/ipc "mkdir" dir))
+    (-> (ipc/ipc "mkdir" dir)
+        (p/then (fn [_] (js/console.log (str "Directory created: " dir))))
+        (p/catch (fn [error]
+                   (when (not= (.-code error) "EEXIST")
+                     (js/console.error (str "Error creating directory: " dir) error))))))
+
   (mkdir-recur! [_this dir]
     (ipc/ipc "mkdir-recur" dir))
+
   (readdir [_this dir]                   ; recursive
     (p/then (ipc/ipc "readdir" dir)
             bean/->clj))
+
   (unlink! [_this repo path _opts]
     (ipc/ipc "unlink"
              (config/get-repo-dir repo)
              path))
   (rmdir! [_this _dir]
-    ;; Too dangerious!!! We'll never implement this.
+    ;; !Too dangerous! We'll never implement this.
     nil)
+
   (read-file [_this dir path _options]
     (let [path (if (nil? dir)
                  path
                  (path/path-join dir path))]
       (ipc/ipc "readFile" path)))
+
   (write-file! [this repo dir path content opts]
     (p/let [fpath (path/path-join dir path)
             stat (p/catch
@@ -106,18 +115,24 @@
             parent-dir (path/parent fpath)
             _ (protocol/mkdir-recur! this parent-dir)]
       (write-file-impl! repo dir path content opts stat)))
+
   (rename! [_this _repo old-path new-path]
     (ipc/ipc "rename" old-path new-path))
+
   (stat [_this fpath]
     (-> (ipc/ipc "stat" fpath)
         (p/then bean/->clj)))
+
   (open-dir [_this dir]
     (open-dir dir))
+
   (get-files [_this dir]
     (-> (ipc/ipc "getFiles" dir)
         (p/then (fn [result]
                   (:files (bean/->clj result))))))
+
   (watch-dir! [_this dir options]
     (ipc/ipc "addDirWatcher" dir options))
+
   (unwatch-dir! [_this dir]
     (ipc/ipc "unwatchDir" dir)))