Skip to content

Commit f5160fb

Browse files
committed
added context to error
1 parent a343f9e commit f5160fb

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,15 +4012,22 @@ def insert_rows_json(
40124012
path = "%s/insertAll" % table.path
40134013
# We can always retry, because every row has an insert ID.
40144014
span_attributes = {"path": path}
4015-
response = self._call_api(
4016-
retry,
4017-
span_name="BigQuery.insertRowsJson",
4018-
span_attributes=span_attributes,
4019-
method="POST",
4020-
path=path,
4021-
data=data,
4022-
timeout=timeout,
4023-
)
4015+
try:
4016+
response = self._call_api(
4017+
retry,
4018+
span_name="BigQuery.insertRowsJson",
4019+
span_attributes=span_attributes,
4020+
method="POST",
4021+
path=path,
4022+
data=data,
4023+
timeout=timeout,
4024+
)
4025+
except requests.exceptions.SSLError as exc:
4026+
msg = (
4027+
"An SSL/Connection error occurred while streaming rows. This "
4028+
"could be due to an invalid request (e.g., invalid table schema)."
4029+
)
4030+
raise requests.exceptions.SSLError(msg) from exc
40244031
errors = []
40254032

40264033
for error in response.get("insertErrors", ()):

packages/google-cloud-bigquery/tests/unit/test_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6753,6 +6753,38 @@ def test_insert_rows_w_wrong_arg(self):
67536753
with self.assertRaises(TypeError):
67546754
client.insert_rows_json(table, ROW)
67556755

6756+
def test_insert_rows_json_w_ssl_error(self):
6757+
from google.cloud.bigquery.dataset import DatasetReference
6758+
from google.cloud.bigquery.schema import SchemaField
6759+
from google.cloud.bigquery.table import Table
6760+
import requests.exceptions
6761+
6762+
PROJECT = "PROJECT"
6763+
DS_ID = "DS_ID"
6764+
TABLE_ID = "TABLE_ID"
6765+
ROWS = [{"full_name": "Bhettye Rhubble", "age": "27", "joined": None}]
6766+
6767+
creds = _make_credentials()
6768+
client = self._make_one(project=PROJECT, credentials=creds, _http=object())
6769+
conn = client._connection = make_connection({})
6770+
6771+
# Make the connection raise an SSLError
6772+
conn.api_request.side_effect = requests.exceptions.SSLError("EOF occurred")
6773+
6774+
table_ref = DatasetReference(PROJECT, DS_ID).table(TABLE_ID)
6775+
schema = [
6776+
SchemaField("full_name", "STRING", mode="REQUIRED"),
6777+
SchemaField("age", "INTEGER", mode="REQUIRED"),
6778+
SchemaField("joined", "TIMESTAMP", mode="NULLABLE"),
6779+
]
6780+
table = Table(table_ref, schema=schema)
6781+
6782+
with self.assertRaises(requests.exceptions.SSLError) as context:
6783+
client.insert_rows_json(table, ROWS)
6784+
6785+
self.assertIn("invalid table schema", str(context.exception))
6786+
self.assertIn("SSL/Connection error occurred", str(context.exception))
6787+
67566788
def test_list_partitions(self):
67576789
from google.cloud.bigquery.table import Table
67586790

0 commit comments

Comments
 (0)