Explorar el Código

Exceptions during block extraction shouldn't delete data

This will prevent future occurrences of #5548
Gabriel Horner hace 3 años
padre
commit
01b28e26ee

+ 37 - 45
deps/graph-parser/src/logseq/graph_parser/block.cljc → deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -1,7 +1,4 @@
 (ns logseq.graph-parser.block
-  ;; Disable clj linters since we don't support clj
-  #?(:clj {:clj-kondo/config {:linters {:unresolved-namespace {:level :off}
-                                        :unresolved-symbol {:level :off}}}})
   "Block related code needed for graph-parser"
   (:require [clojure.string :as string]
             [clojure.walk :as walk]
@@ -12,9 +9,7 @@
             [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.mldoc :as gp-mldoc]
-            [logseq.graph-parser.date-time-util :as date-time-util]
-            #?(:org.babashka/nbb [logseq.graph-parser.log :as log]
-               :default [lambdaisland.glogi :as log])))
+            [logseq.graph-parser.date-time-util :as date-time-util]))
 
 (defn heading-block?
   [block]
@@ -546,45 +541,42 @@
      :date-formatter and :db"
   [blocks content with-id? format {:keys [user-config] :as options}]
   {:pre [(seq blocks) (string? content) (boolean? with-id?) (contains? #{:markdown :org} format)]}
-  (try
-    (let [encoded-content (utf8/encode content)
-          [blocks body pre-block-properties]
-          (loop [headings []
-                 blocks (reverse blocks)
-                 timestamps {}
-                 properties {}
-                 body []]
-            (if (seq blocks)
-              (let [[block pos-meta] (first blocks)
-                    ;; fix start_pos
-                    pos-meta (assoc pos-meta :end_pos
-                                    (if (seq headings)
-                                      (get-in (last headings) [:meta :start_pos])
-                                      nil))]
-                (cond
-                  (paragraph-timestamp-block? block)
-                  (let [timestamps (extract-timestamps block)
-                        timestamps' (merge timestamps timestamps)]
-                    (recur headings (rest blocks) timestamps' properties body))
-
-                  (gp-property/properties-ast? block)
-                  (let [properties (extract-properties format (second block) user-config)]
-                    (recur headings (rest blocks) timestamps properties body))
-
-                  (heading-block? block)
-                  (let [block (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)]
-                    (recur (conj headings block) (rest blocks) {} {} []))
-
-                  :else
-                  (recur headings (rest blocks) timestamps properties (conj body block))))
-              [(-> (reverse headings)
-                   sanity-blocks-data)
-               body
-               properties]))
-          result (with-pre-block-if-exists blocks body pre-block-properties encoded-content options)]
-      (map #(dissoc % :block/meta) result))
-    (catch :default e
-      (log/error :extract-blocks-failure e))))
+  (let [encoded-content (utf8/encode content)
+        [blocks body pre-block-properties]
+        (loop [headings []
+               blocks (reverse blocks)
+               timestamps {}
+               properties {}
+               body []]
+          (if (seq blocks)
+            (let [[block pos-meta] (first blocks)
+                  ;; fix start_pos
+                  pos-meta (assoc pos-meta :end_pos
+                                  (if (seq headings)
+                                    (get-in (last headings) [:meta :start_pos])
+                                    nil))]
+              (cond
+                (paragraph-timestamp-block? block)
+                (let [timestamps (extract-timestamps block)
+                      timestamps' (merge timestamps timestamps)]
+                  (recur headings (rest blocks) timestamps' properties body))
+
+                (gp-property/properties-ast? block)
+                (let [properties (extract-properties format (second block) user-config)]
+                  (recur headings (rest blocks) timestamps properties body))
+
+                (heading-block? block)
+                (let [block (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)]
+                  (recur (conj headings block) (rest blocks) {} {} []))
+
+                :else
+                (recur headings (rest blocks) timestamps properties (conj body block))))
+            [(-> (reverse headings)
+                 sanity-blocks-data)
+             body
+             properties]))
+        result (with-pre-block-if-exists blocks body pre-block-properties encoded-content options)]
+    (map #(dissoc % :block/meta) result)))
 
 (defn with-parent-and-left
   [page-id blocks]

+ 15 - 1
deps/graph-parser/test/logseq/graph_parser_test.cljs

@@ -2,6 +2,7 @@
   (:require [cljs.test :refer [deftest testing is]]
             [logseq.graph-parser :as graph-parser]
             [logseq.graph-parser.db :as gp-db]
+            [logseq.graph-parser.block :as gp-block]
             [datascript.core :as d]))
 
 (deftest parse-file
@@ -26,4 +27,17 @@
                        @conn)
                   (map first)
                   (map :block/properties)))
-          "id as linked ref has correct :block/properties"))))
+          "id as linked ref has correct :block/properties")))
+
+  (testing "unexpected failure during block extraction"
+    (let [conn (gp-db/start-conn)
+          deleted-page (atom nil)]
+      (with-redefs [gp-block/with-pre-block-if-exists (fn stub-failure [& _args]
+                                              (throw (js/Error "Testing unexpected failure")))]
+        (try
+          (graph-parser/parse-file conn "foo.md" "- id:: 628953c1-8d75-49fe-a648-f4c612109098"
+                                  {:delete-blocks-fn (fn [page _file]
+                                                       (reset! deleted-page page))})
+          (catch :default _)))
+      (is (= nil @deleted-page)
+          "Page should not be deleted when there is unexpected failure"))))