Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQuery: Hide error traceback in BigQuery cell magic #8808

Merged
merged 2 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion bigquery/google/cloud/bigquery/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,20 @@ def _cell_magic(line, query):
elif args.maximum_bytes_billed is not None:
value = int(args.maximum_bytes_billed)
job_config.maximum_bytes_billed = value
query_job = _run_query(client, query, job_config)

error = None
try:
query_job = _run_query(client, query, job_config)
except Exception as ex:
error = str(ex)

if not args.verbose:
display.clear_output()

if error:
print("\nERROR:\n", error)
plamut marked this conversation as resolved.
Show resolved Hide resolved
return

result = query_job.to_dataframe(bqstorage_client=bqstorage_client)
if args.destination_var:
IPython.get_ipython().push({args.destination_var: result})
Expand Down
27 changes: 27 additions & 0 deletions bigquery/tests/unit/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
except ImportError: # pragma: NO COVER
IPython = None

from google.api_core import exceptions
import google.auth.credentials

try:
Expand Down Expand Up @@ -815,3 +816,29 @@ def test_bigquery_magic_with_improperly_formatted_params():

with pytest.raises(SyntaxError):
ip.run_cell_magic("bigquery", "--params {17}", sql)


@pytest.mark.usefixtures("ipython_interactive")
def test_bigquery_magic_omits_tracebacks_from_error_message():
ip = IPython.get_ipython()
ip.extension_manager.load_extension("google.cloud.bigquery")

credentials_mock = mock.create_autospec(
google.auth.credentials.Credentials, instance=True
)
default_patch = mock.patch(
"google.auth.default", return_value=(credentials_mock, "general-project")
)

run_query_patch = mock.patch(
"google.cloud.bigquery.magics._run_query",
autospec=True,
side_effect=exceptions.BadRequest("Syntax error in SQL query"),
)

with run_query_patch, default_patch, io.capture_output() as captured_io:
ip.run_cell_magic("bigquery", "", "SELECT foo FROM WHERE LIMIT bar")

output = captured_io.stdout
assert "400 Syntax error in SQL query" in output
assert "Traceback (most recent call last)" not in output