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
pull/9259/head
Bad3r 2023-04-27 11:47:57 +00:00 committed by GitHub
parent abcc6c5bcf
commit aebfae729f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View File

@ -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

View File

@ -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)))