Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Preserve original cause in serdes load errors when app DB tx is aborted (#72725)

Rodrigo López Dato10972d2b3583
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
index b5d594238b8..d2ab8f34ab6 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
@@ -63,11 +63,25 @@
(throw e)))))]
(reduce loader ctx deps)))))
+(defn- safe-local-id
+ "Looks up the local primary key for `path`, swallowing any exception from the DB lookup.
+
+ [[path-error-data]] is called from catch blocks inside the outer load transaction. When the original
+ failure was a SQL error it will have poisoned the transaction, so any subsequent query (including
+ [[serdes/load-find-local]]) will throw `current transaction is aborted, commands ignored until end of
+ transaction block` and shadow the real cause. We prefer losing `:local-id` over losing the exception chain."
+ [path]
+ (try
+ (when-let [entity (serdes/load-find-local path)]
+ ((t2/select-pks-fn entity) entity))
+ (catch Exception e
+ (log/debugf e "Could not look up local id for %s while building load error data" (serdes/log-path-str path))
+ nil)))
+
(defn- path-error-data [error-type expanding path]
(let [last-model (:model (last path))]
{:path (mapv (partial into {}) path)
- :local-id (when-let [entity (serdes/load-find-local path)]
- ((t2/select-pks-fn entity) entity))
+ :local-id (safe-local-id path)
:deps-chain expanding
:model last-model
:table (some->> last-model (keyword "model") t2/table-name)
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
index cb08fbe579f..e44a8b3e73f 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
@@ -1524,6 +1524,23 @@
(is (= (str "qwe_" (:name coll))
(t2/select-one-fn :name :model/Collection :id (:id coll))))))))))
+(deftest path-error-data-handles-lookup-failure-test
+ (testing "path-error-data returns a well-formed map even when serdes/load-find-local throws
+ (e.g. because the outer load transaction is already poisoned by the real failure).
+ This is what lets the caller's ex-info still chain the original cause instead of
+ being replaced by a `current transaction is aborted` error from the enrichment lookup."
+ (with-redefs [serdes/load-find-local (fn [_] (throw (ex-info "current transaction is aborted" {})))]
+ (let [path [{:model "Card" :id "some-entity-id"}]
+ result (#'serdes.load/path-error-data
+ :metabase-enterprise.serialization.v2.load/load-failure
+ #{}
+ path)]
+ (is (nil? (:local-id result))
+ "local-id is nil when the lookup fails")
+ (is (= "Card" (:model result)))
+ (is (= :metabase-enterprise.serialization.v2.load/load-failure (:error result)))
+ (is (= [{:model "Card" :id "some-entity-id"}] (:path result)))))))
+
(deftest circular-links-test
(ts/with-dbs [source-db dest-db]
(ts/with-db source-db