Skip to content

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

Return client error when unable to acquire DB connection (#74088)

Riley Thompsonef79842d3899
diff --git a/src/metabase/driver/sql_jdbc/execute.clj b/src/metabase/driver/sql_jdbc/execute.clj
index 1ef4933b8ff..e496883651b 100644
--- a/src/metabase/driver/sql_jdbc/execute.clj
+++ b/src/metabase/driver/sql_jdbc/execute.clj
@@ -351,7 +351,15 @@
(binding [*connection-recursion-depth* (inc *connection-recursion-depth*)]
(if-let [conn (:connection db-or-id-or-spec)]
(f conn)
- (let [get-conn (^:once fn* [] (.getConnection (do-with-resolved-connection-data-source driver db-or-id-or-spec options)))]
+ (let [get-conn (^:once fn* []
+ (let [conn-data-source (do-with-resolved-connection-data-source driver db-or-id-or-spec options)]
+ (try
+ (.getConnection conn-data-source)
+ (catch Throwable e
+ (throw (ex-info (tru "Unable to connect to the database: {0}" (ex-message e))
+ {:type driver-api/qp.error-type.unable-to-acquire-connection
+ :driver driver}
+ e))))))]
(if (:keep-open? options)
(f (get-conn))
(with-open [conn ^Connection (get-conn)]
diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.clj
index 2e6e4b4adf3..91d91fb1c88 100644
--- a/src/metabase/driver_api/core.clj
+++ b/src/metabase/driver_api/core.clj
@@ -202,6 +202,7 @@
(p/import-def qp.error-type/invalid-query qp.error-type.invalid-query)
(p/import-def qp.error-type/missing-required-parameter qp.error-type.missing-required-parameter)
(p/import-def qp.error-type/qp qp.error-type.qp)
+(p/import-def qp.error-type/unable-to-acquire-connection qp.error-type.unable-to-acquire-connection)
(p/import-def qp.error-type/unsupported-feature qp.error-type.unsupported-feature)
(def schema.common.non-blank-string
diff --git a/src/metabase/query_processor/error_type.clj b/src/metabase/query_processor/error_type.clj
index 8ee728779e6..ca02647042d 100644
--- a/src/metabase/query_processor/error_type.clj
+++ b/src/metabase/query_processor/error_type.clj
@@ -109,6 +109,11 @@
:parent invalid-query
:show-in-embeds? true)
+(deferror unable-to-acquire-connection
+ "The query references a database that we are unable to acquire a connection to."
+ :parent client
+ :show-in-embeds? true)
+
;;;; ### Server-Side Errors
(deferror server
diff --git a/test/metabase/driver/sql_jdbc/execute_test.clj b/test/metabase/driver/sql_jdbc/execute_test.clj
index e8315d1d594..1e26ec3915a 100644
--- a/test/metabase/driver/sql_jdbc/execute_test.clj
+++ b/test/metabase/driver/sql_jdbc/execute_test.clj
@@ -5,8 +5,10 @@
[metabase.config.core :as config]
[metabase.driver :as driver]
[metabase.driver.connection :as driver.conn]
+ [metabase.driver.h2 :as h2]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.test :as mt]
+ [metabase.test.data.interface :as tx]
[metabase.util.malli.registry :as mr])
(:import
(java.sql Connection DatabaseMetaData)
@@ -214,3 +216,23 @@
(fn [_conn] nil)))
(is (pos? (mt/metric-value system :metabase-db-connection/write-op
{:connection-type "write-data"})))))))))))
+
+(deftest bad-connection-details-throw-client-error-test
+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})
+ #_{:clj-kondo/ignore [:discouraged-var]}
+ (mt/with-temp [:model/Database tmp-db {:details (tx/bad-connection-details driver/*driver*)
+ :engine driver/*driver*}]
+ ;; It's not straightforward to trigger a `.getConnection` error for some drivers (e.g. sqlite)
+ ;; so just mock the exception. Also need to mock this h2 method so that the query doesn't fail
+ ;; before it gets to `do-with-resolved-connection-data-source`.
+ (with-redefs [h2/check-read-only-statements (fn [_query] nil)
+ sql-jdbc.execute/do-with-resolved-connection-data-source
+ (fn [_driver _db-or-id-or-spec _options]
+ (reify javax.sql.DataSource
+ (getConnection [_]
+ (throw (java.sql.SQLException. "connection error")))))]
+ (let [query {:database (:id tmp-db)
+ :type :native
+ :native {:query "SELECT 1"}}
+ response (mt/user-http-request :crowberto :post 400 "dataset" query)]
+ (is (= "unable-to-acquire-connection" (:error_type response))))))))
diff --git a/test/metabase/test/data/interface.clj b/test/metabase/test/data/interface.clj
index ce18c05ec72..5ffccf83c4b 100644
--- a/test/metabase/test/data/interface.clj
+++ b/test/metabase/test/data/interface.clj
@@ -1154,32 +1154,29 @@
(doseq [driver [:h2 :sqlite]]
(defmethod bad-connection-details driver
[_driver]
- nil))
+ {:db (u.random/random-name)}))
-(doseq [driver [:bigquery-cloud-sdk]]
- (defmethod bad-connection-details driver
- [_driver]
- {:project-id (u.random/random-name)}))
+(defmethod bad-connection-details :bigquery-cloud-sdk
+ [_driver]
+ {:project-id (u.random/random-name)})
(doseq [driver [:redshift :snowflake :vertica :sparksql]]
(defmethod bad-connection-details driver
[_driver]
{:db (u.random/random-name)}))
-(doseq [driver [:oracle]]
- (defmethod bad-connection-details driver
- [_driver]
- {:service-name (u.random/random-name)}))
+(defmethod bad-connection-details :oracle
+ [_driver]
+ {:service-name (u.random/random-name)})
(doseq [driver [:presto-jdbc :databricks]]
(defmethod bad-connection-details driver
[_driver]
{:catalog (u.random/random-name)}))
-(doseq [driver [:athena]]
- (defmethod bad-connection-details driver
- [_driver]
- {:access_key (u.random/random-name)}))
+(defmethod bad-connection-details :athena
+ [_driver]
+ {:access_key (u.random/random-name)})
(doseq [driver [:postgres :mysql :snowflake :databricks :redshift :sqlite :vertica :athena :oracle]]
(defmethod driver/database-supports? [driver :test/arrays]