The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Report SQL errors with parameters the same as without parameters (#74041)
metambend1a1c68e15be
diff --git a/src/metabase/driver/sql_jdbc/execute.clj b/src/metabase/driver/sql_jdbc/execute.cljindex 3c18f5b7fb9..1ef4933b8ff 100644--- a/src/metabase/driver/sql_jdbc/execute.clj+++ b/src/metabase/driver/sql_jdbc/execute.clj@@ -567,13 +567,16 @@(defn- prepared-statement*^PreparedStatement [driver conn sql params canceled-chan]- ;; sometimes preparing the statement fails, usually if the SQL syntax is invalid.+ ;; sometimes preparing the statement fails, usually if the SQL syntax is invalid. Match the+ ;; classification used in [[execute-reducible-query]] below: such errors should surface to the+ ;; user as :invalid-query (HTTP 4xx with the underlying database message), not :driver+ ;; (HTTP 5xx "We're experiencing server issues"). See #71637.(doto (try(prepared-statement driver conn sql params)(catch Throwable e(throw (ex-info (tru "Error preparing statement: {0}" (ex-message e)){:driver driver- :type driver-api/qp.error-type.driver+ :type driver-api/qp.error-type.invalid-query:sql (str/split-lines (driver/prettify-native-form driver sql)):params params}e))))diff --git a/test/metabase/driver/sql_jdbc/native_test.clj b/test/metabase/driver/sql_jdbc/native_test.cljindex 947717b810c..807bbf0fed6 100644--- a/test/metabase/driver/sql_jdbc/native_test.clj+++ b/test/metabase/driver/sql_jdbc/native_test.clj@@ -3,6 +3,7 @@(:require[clojure.test :refer :all][medley.core :as m]+ [metabase.lib.core :as lib][metabase.query-processor.test :as qp][metabase.test :as mt][metabase.util.malli.schema :as ms]))@@ -76,3 +77,24 @@{:native {:query "SELECT ZID FROM CHECKINS LIMIT 2"}:type :native:database (mt/id)}))))))++(deftest ^:parallel malformed-sql-error-type-test+ (testing "Malformed native SQL is classified as :invalid-query regardless of whether the query has parameters (#71637)"+ (testing "without parameters (Statement path)"+ (is (=? {:status :failed+ :error_type :invalid-query+ :error #"(?s)Column \"ASDF\" not found.*"}+ (qp/process-query+ (qp/userland-query+ (lib/native-query (mt/metadata-provider) "SELECT * FROM ORDERS GROUP BY ASDF"))))))+ (testing "with parameters (PreparedStatement path)"+ (let [query (-> (lib/native-query (mt/metadata-provider)+ "SELECT * FROM ORDERS WHERE CATEGORY = {{category}} GROUP BY ASDF")+ (assoc :parameters [{:type :text+ :name "category"+ :target [:variable [:template-tag "category"]]+ :value "Widget"}]))]+ (is (=? {:status :failed+ :error_type :invalid-query+ :error #"(?s)Column \"ASDF\" not found.*"}+ (qp/process-query (qp/userland-query query))))))))