Răsfoiți Sursa

fix: notify broken config and restore to the default configuraiton

Tienson Qin 3 ani în urmă
părinte
comite
218bb04bd2

+ 15 - 11
src/main/frontend/handler/common.cljs

@@ -17,7 +17,8 @@
             ["ignore" :as Ignore]
             [lambdaisland.glogi :as log]
             [promesa.core :as p]
-            [frontend.handler.notification :as notification]))
+            [frontend.handler.notification :as notification]
+            [borkdude.rewrite-edn :as rewrite]))
 
 (defn get-ref
   [repo-url]
@@ -121,16 +122,8 @@
   [content]
   (safe-read-string content
                     (fn [_e]
-                      (notification/show!
-                       [:div {:style {:z-index 999}}
-                        [:h1
-                         "Invalid configuration."]
-                        [:p "You can send the file \"config.edn\" to "
-                         [:code
-                          "[email protected]"]
-                         [:span ", we'll repair and send it back to you."]]]
-                       :error
-                       false))))
+                      (state/pub-event! [:backup/broken-config (state/get-current-repo) content])
+                      (reader/read-string config/config-default-content))))
 
 (defn reset-config!
   [repo-url content]
@@ -241,3 +234,14 @@
       (d/set-style! context-menu
                     :left (str client-x "px")
                     :top (str (+ scroll-y client-y) "px")))))
+
+(defn parse-config
+  "Parse configuration from file `content` such as from config.edn."
+  [path content]
+  (try
+    (rewrite/parse-string content)
+    (catch js/Error e
+      (println "Parsing config file failed: ")
+      (js/console.error e)
+      (state/pub-event! [:backup/broken-config (state/get-current-repo) content])
+      (rewrite/parse-string config/config-default-content))))

+ 11 - 0
src/main/frontend/handler/events.cljs

@@ -289,6 +289,17 @@
         (when (and pending? (seq (state/all-available-coming-updates)))
           (plugin/open-waiting-updates-modal!))))))
 
+(defmethod handle :backup/broken-config [[_ repo path content]]
+  (when (and repo content)
+    (let [path (config/get-config-path)
+          broken-path (str path "-broken")]
+      (p/let [_ (fs/write-file! repo (config/get-repo-dir repo) broken-path content {})]
+        (notification/show!
+         [:p.content
+          "It seems that your config.edn is broken. We've restored it with the default content and saved the previous content to the file logseq/config.edn-broken."]
+         :error
+         false)))))
+
 (defn run!
   []
   (let [chan (state/get-events-chan)]

+ 1 - 6
src/main/frontend/handler/file.cljs

@@ -346,12 +346,7 @@
   (when-let [repo (state/get-current-repo)]
     (when-let [content (db/get-file path)]
       (common-handler/read-config content)
-      (let [result (try
-                     (rewrite/parse-string content)
-                     (catch js/Error e
-                       (println "Parsing config file failed: ")
-                       (js/console.dir e)
-                       {}))
+      (let [result (common-handler/parse-config path content)
             ks (if (vector? k) k [k])
             new-result (rewrite/assoc-in result ks v)
             new-content (str new-result)]

+ 1 - 6
src/main/frontend/modules/shortcut/data_helper.cljs

@@ -140,12 +140,7 @@
   (let [repo (state/get-current-repo)
         path (cfg/get-config-path)]
     (when-let [content (db/get-file path)]
-      (let [result (try
-                     (rewrite/parse-string content)
-                     (catch js/Error e
-                       (println "Parsing config file failed: ")
-                       (js/console.dir e)
-                       {}))
+      (let [result (common-handler/parse-config path content)
             new-result (rewrite/update
                         result
                         :shortcuts