Skip to content

Commit bbc415c

Browse files
committed
collect invalid db errors to sentry
1 parent 30a1156 commit bbc415c

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

deps/db/src/logseq/db.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
pipeline-f @*transact-pipeline-fn
118118
tx-report (if-let [f pipeline-f] (f tx-report*) tx-report*)
119119
_ (throw-if-page-has-block-parent! (:db-after tx-report) (:tx-data tx-report))
120-
validate-result (db-validate/validate-tx-report tx-report nil)]
120+
[validate-result errors] (db-validate/validate-tx-report tx-report nil)]
121121
(if validate-result
122122
(when (and tx-report (seq (:tx-data tx-report)))
123123
;; perf enhancement: avoid repeated call on `d/with`
@@ -127,7 +127,7 @@
127127
(do
128128
;; notify ui
129129
(when-let [f @*transact-invalid-callback]
130-
(f tx-report))
130+
(f tx-report errors))
131131
(throw (ex-info "DB write failed with invalid data" {:tx-data tx-data}))))
132132
tx-report)
133133
(d/transact! conn tx-data tx-meta)))

deps/db/src/logseq/db/frontend/validate.cljs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
(defn validate-tx-report
2525
"Validates the datascript tx-report for entities that have changed. Returns
2626
boolean indicating if db is valid"
27-
[{:keys [db-after tx-data _tx-meta]} validate-options]
27+
[{:keys [db-after tx-data tx-meta]} validate-options]
2828
(let [changed-ids (->> tx-data (keep :e) distinct)
2929
tx-datoms (mapcat #(d/datoms db-after :eavt %) changed-ids)
3030
ent-maps* (map (fn [[db-id m]]
@@ -38,23 +38,28 @@
3838
;; remove :db/id as it adds needless declarations to schema
3939
#(validator [(dissoc % :db/id)])
4040
ent-maps)]
41-
;; (prn "changed eids:" changed-ids :tx-meta tx-meta)
4241
(if (seq invalid-ent-maps)
43-
(let [explainer (get-schema-explainer (:closed-schema? validate-options))]
44-
(prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids)
45-
(doseq [m invalid-ent-maps]
46-
(let [m' (update m :block/properties (fn [properties]
47-
(map (fn [[p v]]
48-
[(:db/ident p) v])
49-
properties)))
50-
data {:entity-map m'
51-
:errors (me/humanize (explainer [(dissoc m :db/id)]))}]
52-
(try
53-
(pprint/pprint data)
54-
(catch :default _e
55-
(prn data)))))
56-
false)
57-
true)))))
42+
(do
43+
(prn "Invalid datascript entities detected amongst changed entity ids:" changed-ids :tx-meta tx-meta)
44+
(let [explainer (get-schema-explainer (:closed-schema? validate-options))
45+
errors (doall
46+
(map
47+
(fn [m]
48+
(let [m' (update m :block/properties (fn [properties]
49+
(map (fn [[p v]]
50+
[(:db/ident p) v])
51+
properties)))
52+
data {:entity-map m'
53+
:errors (me/humanize (explainer [(dissoc m :db/id)]))}]
54+
(try
55+
(pprint/pprint data)
56+
(catch :default _e
57+
(prn data)))
58+
data))
59+
invalid-ent-maps))]
60+
61+
[false errors]))
62+
[true nil])))))
5863

5964
(defn group-errors-by-entity
6065
"Groups malli errors by entities. db is used for providing more debugging info"

src/main/frontend/worker/db_worker.cljs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,12 +888,15 @@
888888
service)))))
889889

890890
(defn- notify-invalid-data
891-
[{:keys [tx-meta]}]
891+
[{:keys [tx-meta]} errors]
892892
;; don't notify on production when undo/redo failed
893893
(when-not (and (or (:undo? tx-meta) (:redo? tx-meta))
894894
(not worker-util/dev?))
895895
(shared-service/broadcast-to-clients! :notification
896-
[["Invalid DB!"] :error])))
896+
[["Invalid DB!"] :error])
897+
(worker-util/post-message :capture-error
898+
{:error (ex-info "Invalid DB" {})
899+
:payload {:errors errors}})))
897900

898901
(defn init
899902
"web worker entry"

0 commit comments

Comments
 (0)