Skip to content

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

Metabot: remove extraneous data from non-500 errors with an error code (#81201)

Andrei Simionescufa07f544d59d
diff --git a/src/metabase/server/middleware/exceptions.clj b/src/metabase/server/middleware/exceptions.clj
index 8c755555af1..b0083a7eb7c 100644
--- a/src/metabase/server/middleware/exceptions.clj
+++ b/src/metabase/server/middleware/exceptions.clj
@@ -43,6 +43,11 @@
(and status-code (:errors other-info))
other-info
+ ;; a machine-readable error code means the throw site authored this as an
+ ;; API response; return it as-is, without a stacktrace
+ (and status-code (not= status-code 500) (:error-code other-info))
+ (merge {:message (ex-message e)} other-info)
+
;; allow administrators to configure their instances to suppress stacktraces
;; returns 500 with a generic message
(server.settings/hide-stacktraces)
diff --git a/test/metabase/metabot/api/document_test.clj b/test/metabase/metabot/api/document_test.clj
index 0cce77229fa..49bf5ae0796 100644
--- a/test/metabase/metabot/api/document_test.clj
+++ b/test/metabase/metabot/api/document_test.clj
@@ -7,6 +7,7 @@
[metabase.metabot.self.openrouter :as openrouter]
[metabase.metabot.test-util :as mut]
[metabase.metabot.tools.sql.create :as create-sql-query-tools]
+ [metabase.metabot.usage :as metabot.usage]
[metabase.query-processor :as qp]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]))
@@ -34,6 +35,15 @@
:post 200 "metabot/document/generate-content"
{:instructions "Show me sales data"}))))))
+(deftest generate-content-free-limit-body-test
+ (testing "the 402 body is the message and error code alone, not a map carrying a stack trace"
+ (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly true)]
+ (is (= {:message "You've used all of your included AI service tokens. To keep using AI features, end your trial early and start your subscription, or add your own AI provider API key."
+ :error-code "metabase_ai_managed_locked"}
+ (mt/user-http-request :crowberto
+ :post 402 "metabot/document/generate-content"
+ {:instructions "Show me sales data"}))))))
+
(deftest generate-content-prometheus-test
(mt/with-temporary-setting-values [llm-providers llm.tu/default-connections
llm-metabot-provider test-provider]
diff --git a/test/metabase/server/middleware/exceptions_test.clj b/test/metabase/server/middleware/exceptions_test.clj
index 9fd51b262e4..d1df5b4098e 100644
--- a/test/metabase/server/middleware/exceptions_test.clj
+++ b/test/metabase/server/middleware/exceptions_test.clj
@@ -52,6 +52,35 @@
(is (contains? (:body response) :via)
"Response should contain :via key with exception chain")))))
+(deftest api-exception-response-error-code-test
+ (testing "a non-500 that declares an :error-code returns its structured body without a stacktrace, whatever hide-stacktraces says"
+ (doseq [hide? [false true]]
+ (mt/with-temporary-setting-values [server.settings/hide-stacktraces hide?]
+ (let [exception (ex-info "You are out of tokens."
+ {:status-code 402
+ :message "You are out of tokens."
+ :error-code "metabase_ai_managed_locked"})
+ response (mw.exceptions/api-exception-response exception nil)]
+ (is (= 402 (:status response)))
+ (is (= {:message "You are out of tokens."
+ :error-code "metabase_ai_managed_locked"}
+ (:body response))))
+ (let [exception (ex-info "Slack API error: ratelimited"
+ {:status-code 502
+ :error-code "ratelimited"})
+ response (mw.exceptions/api-exception-response exception nil)]
+ (is (= 502 (:status response)))
+ (is (= {:message "Slack API error: ratelimited"
+ :error-code "ratelimited"}
+ (:body response)))))))
+ (testing "a 500, or the legacy :error_code spelling, keeps the full stacktrace body"
+ (mt/with-temporary-setting-values [server.settings/hide-stacktraces false]
+ (doseq [data [{:status-code 500, :error-code "boom"}
+ {:status-code 404, :error_code "archived"}]]
+ (let [response (mw.exceptions/api-exception-response (ex-info "boom" data) nil)]
+ (is (= (:status-code data) (:status response)))
+ (is (contains? (:body response) :trace)))))))
+
(deftest api-exception-response-hides-stacktraces-returns-generic-message-test
(testing "When hide-stacktraces is true, exception response contains only a generic message"
(mt/with-temporary-setting-values [server.settings/hide-stacktraces true]