Skip to content

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

Surface YAML parse reason in remote-sync ingestion errors (#75896)

Eric Normand6cc41b5ad9b1
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index b8a4d44feb4..1c58e2792d9 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -154,6 +154,15 @@
(some-> e ex-cause ex-message (str/includes? "database not found"))
(format "Import failed: A referenced database does not exist on this instance. %s" (ex-message (ex-cause e)))
+ (seq (:ingest-errors (ex-data e)))
+ (let [ingest-errors (:ingest-errors (ex-data e))]
+ (format "Failed to read %d file(s) from the repository: %s"
+ (count ingest-errors)
+ (str/join "; " (for [ie ingest-errors
+ :let [{:keys [file reason]} (ex-data ie)]]
+ (cond-> file
+ reason (str ": " reason))))))
+
:else
(format "Failed to reload from git repository: %s" (ex-message e))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/ingestable.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/ingestable.clj
index 7db471cafe5..75b952dbc0d 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/ingestable.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/ingestable.clj
@@ -10,7 +10,25 @@
[metabase.util.log :as log]
[metabase.util.yaml :as yaml]
[toucan2.core :as t2])
- (:import (metabase_enterprise.remote_sync.source.protocol SourceSnapshot)))
+ (:import
+ (metabase_enterprise.remote_sync.source.protocol SourceSnapshot)
+ (org.yaml.snakeyaml.error MarkedYAMLException)))
+
+(set! *warn-on-reflection* true)
+
+(defn- error-reason
+ "A concise, single-line reason for an ingestion failure, suitable for display and machine-readable
+ storage. For YAML errors, SnakeYAML's first message line is a generic context (e.g. \"while scanning
+ for the next token\"); the useful diagnostic is the problem plus its mark, so those are extracted.
+ Otherwise falls back to the first line of the message."
+ [e]
+ (if (instance? MarkedYAMLException e)
+ (let [^MarkedYAMLException e e
+ mark (.getProblemMark e)]
+ (cond-> (.getProblem e)
+ ;; marks are 0-based; report them 1-based to match editors
+ mark (str (format " (line %d, column %d)" (inc (.getLine mark)) (inc (.getColumn mark))))))
+ (some-> (ex-message e) str/split-lines first str/trim)))
(defn- ingest-content
[file-content]
@@ -31,14 +49,16 @@
(source.p/read-file snapshot path)
(catch Exception e
(log/warn (u/strip-error e "Error reading file during ingestion"))
- (swap! errors conj (ex-info (format "Failed to read file: %s" path) {:file path} e))
+ (swap! errors conj (ex-info (format "Failed to read file: %s" path)
+ {:file path :reason (error-reason e)} e))
nil))
loaded (try
(when content
(serdes/path (ingest-content content)))
(catch Exception e
(log/warn (u/strip-error e "Error parsing file during ingestion"))
- (swap! errors conj (ex-info (format "Failed to parse file: %s" path) {:file path} e))
+ (swap! errors conj (ex-info (format "Failed to parse file: %s" path)
+ {:file path :reason (error-reason e)} e))
nil))]
:when loaded]
[(serialization/strip-labels loaded) {:content content :path path}]))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
index 9be7650780c..023c59013f1 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
@@ -92,7 +92,10 @@
bad-yaml}}
result (impl/import! (source.p/snapshot (test-helpers/create-mock-source :initial-files files)) task-id)]
(is (= :error (:status result))
- "Import should fail when a YAML file cannot be parsed, not silently skip it"))))
+ "Import should fail when a YAML file cannot be parsed, not silently skip it")
+ (testing "the error message names the offending file and the parse reason"
+ (is (str/includes? (:message result) "collections/main/test/bad_card.yaml"))
+ (is (str/includes? (:message result) "expected ',' or ']'"))))))
(deftest import!-handles-generic-errors-test
(testing "import! handles generic errors"
@@ -116,7 +119,19 @@
e (ex-info "Failed to load into database for Card abc123"
{:path "Card abc123"}
cause)]
- (is (str/includes? (impl/source-error-message e) "A referenced database does not exist on this instance")))))
+ (is (str/includes? (impl/source-error-message e) "A referenced database does not exist on this instance"))))
+ (testing "source-error-message lists each unreadable file with its parse reason (GHY-3887)"
+ (let [ingest-err (ex-info "Failed to parse file: collections/transforms/a.yaml"
+ {:file "collections/transforms/a.yaml"
+ :reason "found character '@' that cannot start any token. (Do not use @ for indentation) (line 1, column 1)"})
+ e (ex-info "Failed to read 1 file(s) during ingestion: collections/transforms/a.yaml"
+ {:ingest-errors [ingest-err]
+ :files ["collections/transforms/a.yaml"]}
+ ingest-err)
+ msg (impl/source-error-message e)]
+ (is (str/includes? msg "Failed to read 1 file(s)"))
+ (is (str/includes? msg "collections/transforms/a.yaml"))
+ (is (str/includes? msg "found character '@'")))))
;; We need to make sure the task-id we use to track the Remote Sync is not bound to a transactions because of the behavior of
;; update-sync-progress. So the follow two tests cannot use with-temp to create models