Skip to content

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

Keep declared errors intact through with-pipeline-errors (#85922)

Chris Truterb4d6b899ec65
diff --git a/src/metabase/metabot/tools/core.clj b/src/metabase/metabot/tools/core.clj
index 9056d079f3f..3aefae6347c 100644
--- a/src/metabase/metabot/tools/core.clj
+++ b/src/metabase/metabot/tools/core.clj
@@ -428,7 +428,7 @@
(try
(thunk)
(catch Throwable e
- (if (and (not (contains? (ex-data e) tools.error/error-key))
+ (if (and (not (tools.error/declared? e))
(contains? refusal-status-codes (:status-code (ex-data e))))
(tools.error/throw-recoverable! ::recoverable.common/not-found
(cond-> {} kind (assoc :kind kind) (some? id) (assoc :id id))
@@ -475,6 +475,11 @@
(let [{:keys [agent-error? status-code error humanized]} (ex-data e)
code (pipeline-error-code error)]
(cond
+ ;; Its payload and recovery steps are more specific than anything below would rebuild. A declared
+ ;; not-found carries a 404, and a declared recoverable carries `:agent-error?`, so both would match.
+ (tools.error/declared? e)
+ (throw e)
+
;; Lib exceptions can also carry `:agent-error?`, but their messages aren't written for the model.
(and agent-error? code)
(tools.error/throw-recoverable!
@@ -499,7 +504,8 @@
(with-pipeline-errors
(construct/execute-representations-query query))
- Three cases:
+ Four cases:
+ - a declared tool error passes through unchanged, so a nested `with-entity` keeps the entity it names;
- an `:agent-error?` exception whose ex-data carries an `:error` code declared in
`metabase.metabot.tools.recoverable.pipeline` becomes that error, with the pipeline's own
sentence as the message;
diff --git a/src/metabase/metabot/tools/error.clj b/src/metabase/metabot/tools/error.clj
index 71efeae710d..3ad9aeb4f1c 100644
--- a/src/metabase/metabot/tools/error.clj
+++ b/src/metabase/metabot/tools/error.clj
@@ -378,6 +378,11 @@
;;; ------------------------------------------ Classification ------------------------------------------------------
+(defn declared?
+ "Whether `e` already carries a ToolError under [[error-key]], so a converter should rethrow it as it is."
+ [e]
+ (contains? (ex-data e) error-key))
+
(defn classify
"The ToolError for `e`.
diff --git a/src/metabase/metabot/tools/legacy.clj b/src/metabase/metabot/tools/legacy.clj
index b6b2cf21f72..43aa48fa4d3 100644
--- a/src/metabase/metabot/tools/legacy.clj
+++ b/src/metabase/metabot/tools/legacy.clj
@@ -107,7 +107,7 @@
;; converter such as `with-pipeline-errors`. A declared recoverable carries `:agent-error?`
;; too, so without this it would be re-wrapped as a legacy agent error — losing its recovery
;; steps and re-attributing it to this tool.
- (contains? (ex-data e) tools.error/error-key)
+ (tools.error/declared? e)
(throw e)
terminal-error?
diff --git a/src/metabase/metabot/tools/save_entity.clj b/src/metabase/metabot/tools/save_entity.clj
index 3b9e15cde28..df96160b7b7 100644
--- a/src/metabase/metabot/tools/save_entity.clj
+++ b/src/metabase/metabot/tools/save_entity.clj
@@ -136,7 +136,7 @@
(catch clojure.lang.ExceptionInfo e
(let [data (ex-data e)
status-code (:status-code data)]
- (if (and (not (contains? data tools.error/error-key))
+ (if (and (not (tools.error/declared? e))
(int? status-code) (<= 400 status-code 499))
(save-rejected! {:message (ex-message e)})
(throw e))))))
diff --git a/test/metabase/metabot/tools/core_test.clj b/test/metabase/metabot/tools/core_test.clj
index cd69ad012d9..6d8433cf83d 100644
--- a/test/metabase/metabot/tools/core_test.clj
+++ b/test/metabase/metabot/tools/core_test.clj
@@ -384,6 +384,31 @@
:data {}}
(pipeline-failure "no" {:status-code status}))))))
+(deftest ^:parallel with-pipeline-errors-keeps-declared-errors-test
+ (testing "a declared not-found raised inside keeps the entity it names, despite its 404"
+ (is (=? {:class :recoverable
+ :code :metabase.metabot.tools.recoverable.common/not-found
+ :message "Table 5 was not found. It may not exist, or you may not have access to it."
+ :data {:kind :table :id 5}}
+ (tools.error/classify
+ (is (thrown? clojure.lang.ExceptionInfo
+ (tools/with-pipeline-errors
+ (tools/with-entity {:kind :table :id 5}
+ (throw (ex-info "nope" {:status-code 404}))))))))))
+ (doseq [[branch thunk] [["refusal"
+ #(tools/with-entity {:kind :table :id 5}
+ (throw (ex-info "nope" {:status-code 404})))]
+ ["agent-error"
+ #(tools.error/throw-recoverable!
+ :metabase.metabot.tools.recoverable.pipeline/unknown-table
+ {:message "No table found."})]]]
+ (testing (str "a declared error that would match the " branch " branch is rethrown as it is, not rebuilt")
+ (let [declared (try (thunk) (catch Throwable e e))]
+ (is (identical? declared
+ (try
+ (tools/with-pipeline-errors (throw declared))
+ (catch Throwable e e))))))))
+
(deftest ^:parallel with-pipeline-errors-does-not-relay-foreign-messages-test
(testing "Lib exceptions stay unrecoverable without a declared `:error` code and lose the `:agent-error?` flag."
(is (=? {:class :unrecoverable