Skip to content

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

Report synchronous bigquery errors as client errors (#74684)

metamben00e329ce7e8e
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index bf0c9fa74c6..915a88d7d79 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -52,6 +52,7 @@
Field$Mode
FieldValue
FieldValueList
+ Job
JobInfo
QueryJobConfiguration
Schema
@@ -739,7 +740,12 @@
result-promise (promise)
request (build-bigquery-request sql parameters)
_ (driver.conn/track-connection-acquisition! database-details)
- job (.create client (JobInfo/of request) (u/varargs BigQuery$JobOption))
+ ;; Wrap exception to avoid responding with HTTP 500 and reporting "We're experiencing server issues"
+ ;; in the UI. (#71558)
+ ^Job job (try
+ (.create client (JobInfo/of request) (u/varargs BigQuery$JobOption))
+ (catch Throwable t
+ (handle-bigquery-exception t sql parameters)))
job-id (.getJobId job)
query-future (future
;; ensure the classloader is available within the future.
diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
index 6cf5bd26e57..c3f6d7e3261 100644
--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj
@@ -32,7 +32,7 @@
[metabase.warehouse-schema.models.field-values :as field-values]
[toucan2.core :as t2])
(:import
- (com.google.cloud.bigquery BigQuery TableResult)
+ (com.google.cloud.bigquery BigQuery BigQueryException TableResult)
(com.google.cloud.http HttpTransportOptions)))
(set! *warn-on-reflection* true)
@@ -739,6 +739,36 @@
:type :native
:native {:query "SELECT abc FROM 123;"}}))))))
+(deftest synchronous-bigquery-exception-is-invalid-query-test
+ (testing (str "BigQueryException thrown synchronously from .create (e.g. 'Too many query "
+ "parameters') is wrapped with :type :invalid-query so the FE renders the "
+ "actual error instead of 'We're experiencing server issues' (#71558)")
+ ;; Simulate BigQuery rejecting the job at submission time (the synchronous .create path),
+ ;; which is what happens when a query exceeds the 10000 parameter limit. Only `.create` is
+ ;; called on the client before the throw, so the reify can stop there.
+ (let [sync-error (BigQueryException.
+ 400
+ "Too many query parameters: 13444 exceeds limit of 10000.")
+ mock-client (reify BigQuery
+ (^com.google.cloud.bigquery.Job create
+ [_
+ ^com.google.cloud.bigquery.JobInfo _job-info
+ ^"[Lcom.google.cloud.bigquery.BigQuery$JobOption;" _opts]
+ (throw sync-error)))]
+ (with-redefs [bigquery/database-details->client (constantly mock-client)]
+ (let [ex (try
+ (#'bigquery/execute-bigquery (constantly nil) {} "SELECT 1" [] nil)
+ nil
+ (catch Throwable t t))]
+ (is (some? ex)
+ "expected execute-bigquery to throw")
+ (is (instance? clojure.lang.ExceptionInfo ex)
+ "execute-bigquery should wrap the raw BigQueryException in an ex-info")
+ (is (= :invalid-query (some-> ex ex-data :type))
+ "synchronous BigQueryException should be classified as :invalid-query")
+ (is (re-find #"Too many query parameters" (ex-message ex))
+ "the underlying BigQuery error message should be preserved"))))))
+
(deftest project-id-override-test
(mt/test-driver :bigquery-cloud-sdk
(testing "Querying a different project-id works"