浏览代码

collect invalid db errors to sentry

Tienson Qin 4 天之前
父节点
当前提交
bbc415c1be
共有 3 个文件被更改,包括 29 次插入21 次删除
  1. 2 2
      deps/db/src/logseq/db.cljs
  2. 22 17
      deps/db/src/logseq/db/frontend/validate.cljs
  3. 5 2
      src/main/frontend/worker/db_worker.cljs

+ 2 - 2
deps/db/src/logseq/db.cljs

@@ -117,7 +117,7 @@
               pipeline-f @*transact-pipeline-fn
               tx-report (if-let [f pipeline-f] (f tx-report*) tx-report*)
               _ (throw-if-page-has-block-parent! (:db-after tx-report) (:tx-data tx-report))
-              validate-result (db-validate/validate-tx-report tx-report nil)]
+              [validate-result errors] (db-validate/validate-tx-report tx-report nil)]
           (if validate-result
             (when (and tx-report (seq (:tx-data tx-report)))
               ;; perf enhancement: avoid repeated call on `d/with`
@@ -127,7 +127,7 @@
             (do
               ;; notify ui
               (when-let [f @*transact-invalid-callback]
-                (f tx-report))
+                (f tx-report errors))
               (throw (ex-info "DB write failed with invalid data" {:tx-data tx-data}))))
           tx-report)
         (d/transact! conn tx-data tx-meta)))

+ 22 - 17
deps/db/src/logseq/db/frontend/validate.cljs

@@ -24,7 +24,7 @@
 (defn validate-tx-report
   "Validates the datascript tx-report for entities that have changed. Returns
   boolean indicating if db is valid"
-  [{:keys [db-after tx-data _tx-meta]} validate-options]
+  [{:keys [db-after tx-data tx-meta]} validate-options]
   (let [changed-ids (->> tx-data (keep :e) distinct)
         tx-datoms (mapcat #(d/datoms db-after :eavt %) changed-ids)
         ent-maps* (map (fn [[db-id m]]
@@ -38,23 +38,28 @@
                               ;; remove :db/id as it adds needless declarations to schema
                               #(validator [(dissoc % :db/id)])
                               ent-maps)]
-        ;; (prn "changed eids:" changed-ids :tx-meta tx-meta)
         (if (seq invalid-ent-maps)
-          (let [explainer (get-schema-explainer (:closed-schema? validate-options))]
-            (prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
-            (doseq [m invalid-ent-maps]
-              (let [m' (update m :block/properties (fn [properties]
-                                                     (map (fn [[p v]]
-                                                            [(:db/ident p) v])
-                                                          properties)))
-                    data {:entity-map m'
-                          :errors (me/humanize (explainer [(dissoc m :db/id)]))}]
-                (try
-                  (pprint/pprint data)
-                  (catch :default _e
-                    (prn data)))))
-            false)
-          true)))))
+          (do
+            (prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids :tx-meta tx-meta)
+            (let [explainer (get-schema-explainer (:closed-schema? validate-options))
+                  errors (doall
+                          (map
+                           (fn [m]
+                             (let [m' (update m :block/properties (fn [properties]
+                                                                    (map (fn [[p v]]
+                                                                           [(:db/ident p) v])
+                                                                         properties)))
+                                   data {:entity-map m'
+                                         :errors (me/humanize (explainer [(dissoc m :db/id)]))}]
+                               (try
+                                 (pprint/pprint data)
+                                 (catch :default _e
+                                   (prn data)))
+                               data))
+                           invalid-ent-maps))]
+
+              [false errors]))
+          [true nil])))))
 
 (defn group-errors-by-entity
   "Groups malli errors by entities. db is used for providing more debugging info"

+ 5 - 2
src/main/frontend/worker/db_worker.cljs

@@ -888,12 +888,15 @@
           service)))))
 
 (defn- notify-invalid-data
-  [{:keys [tx-meta]}]
+  [{:keys [tx-meta]} errors]
   ;; don't notify on production when undo/redo failed
   (when-not (and (or (:undo? tx-meta) (:redo? tx-meta))
                  (not worker-util/dev?))
     (shared-service/broadcast-to-clients! :notification
-                                          [["Invalid DB!"] :error])))
+                                          [["Invalid DB!"] :error])
+    (worker-util/post-message :capture-error
+                              {:error (ex-info "Invalid DB" {})
+                               :payload {:errors errors}})))
 
 (defn init
   "web worker entry"