Skip to content

Commit

Permalink
fix: Improve error handling in mkdir function (logseq#9247)
Browse files Browse the repository at this point in the history
* 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 logseq#9182

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

* fix: make EEXIST silent and only throw other errors
  • Loading branch information
Bad3r authored Apr 27, 2023
1 parent abcc6c5 commit aebfae7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/main/frontend/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/main/frontend/fs/node.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))

0 comments on commit aebfae7

Please sign in to comment.