Skip to content

Commit 6fc0de5

Browse files
authored
BigQuery: Hide error traceback in BigQuery cell magic (#8808)
* Hide error traceback in BigQuery cell magic The traceback is an internal detail of the IPython magic, the users should only see the relevant error information. * Output BigQuery cell magic error message to stderr
1 parent fdcd121 commit 6fc0de5

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

bigquery/google/cloud/bigquery/magics.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
from __future__ import print_function
131131

132132
import ast
133+
import sys
133134
import time
134135
from concurrent import futures
135136

@@ -415,11 +416,20 @@ def _cell_magic(line, query):
415416
elif args.maximum_bytes_billed is not None:
416417
value = int(args.maximum_bytes_billed)
417418
job_config.maximum_bytes_billed = value
418-
query_job = _run_query(client, query, job_config)
419+
420+
error = None
421+
try:
422+
query_job = _run_query(client, query, job_config)
423+
except Exception as ex:
424+
error = str(ex)
419425

420426
if not args.verbose:
421427
display.clear_output()
422428

429+
if error:
430+
print("\nERROR:\n", error, file=sys.stderr)
431+
return
432+
423433
result = query_job.to_dataframe(bqstorage_client=bqstorage_client)
424434
if args.destination_var:
425435
IPython.get_ipython().push({args.destination_var: result})

bigquery/tests/unit/test_magics.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
except ImportError: # pragma: NO COVER
3333
IPython = None
3434

35+
from google.api_core import exceptions
3536
import google.auth.credentials
3637

3738
try:
@@ -862,3 +863,30 @@ def test_bigquery_magic_with_improperly_formatted_params():
862863

863864
with pytest.raises(SyntaxError):
864865
ip.run_cell_magic("bigquery", "--params {17}", sql)
866+
867+
868+
@pytest.mark.usefixtures("ipython_interactive")
869+
def test_bigquery_magic_omits_tracebacks_from_error_message():
870+
ip = IPython.get_ipython()
871+
ip.extension_manager.load_extension("google.cloud.bigquery")
872+
873+
credentials_mock = mock.create_autospec(
874+
google.auth.credentials.Credentials, instance=True
875+
)
876+
default_patch = mock.patch(
877+
"google.auth.default", return_value=(credentials_mock, "general-project")
878+
)
879+
880+
run_query_patch = mock.patch(
881+
"google.cloud.bigquery.magics._run_query",
882+
autospec=True,
883+
side_effect=exceptions.BadRequest("Syntax error in SQL query"),
884+
)
885+
886+
with run_query_patch, default_patch, io.capture_output() as captured_io:
887+
ip.run_cell_magic("bigquery", "", "SELECT foo FROM WHERE LIMIT bar")
888+
889+
output = captured_io.stderr
890+
assert "400 Syntax error in SQL query" in output
891+
assert "Traceback (most recent call last)" not in output
892+
assert "Syntax error" not in captured_io.stdout

0 commit comments

Comments
 (0)