Skip to content

Commit

Permalink
chore(bigquery): Add extra logging for BigQuery exceptions so we can …
Browse files Browse the repository at this point in the history
…have better insight on exceptions (#22024)
  • Loading branch information
Antonio-RiveroMartnez authored Nov 10, 2022
1 parent 736b534 commit 95b4c7b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
11 changes: 10 additions & 1 deletion superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@ def get_dbapi_exception_mapping(cls) -> Dict[Type[Exception], Type[Exception]]:
"""
return {}

@classmethod
def parse_error_exception(cls, exception: Exception) -> Exception:
"""
Each engine can implement and converge its own specific parser method
:return: An Exception with a parsed string off the original exception
"""
return exception

@classmethod
def get_dbapi_mapped_exception(cls, exception: Exception) -> Exception:
"""
Expand All @@ -443,7 +452,7 @@ def get_dbapi_mapped_exception(cls, exception: Exception) -> Exception:
"""
new_exception = cls.get_dbapi_exception_mapping().get(type(exception))
if not new_exception:
return exception
return cls.parse_error_exception(exception)
return new_exception(str(exception))

@classmethod
Expand Down
9 changes: 9 additions & 0 deletions superset/db_engine_specs/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,12 @@ def _get_fields(cls, cols: List[Dict[str, Any]]) -> List[Any]:
"author__name" and "author__email", respectively.
"""
return [column(c["name"]).label(c["name"].replace(".", "__")) for c in cols]

@classmethod
def parse_error_exception(cls, exception: Exception) -> Exception:
try:
return Exception(str(exception).splitlines()[0].rsplit(":")[1].strip())
except Exception: # pylint: disable=broad-except
# If for some reason we get an exception, for example, no new line
# We will return the original exception
return exception
41 changes: 41 additions & 0 deletions tests/unit_tests/db_engine_specs/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,44 @@ def test_mask_encrypted_extra_when_empty() -> None:
from superset.db_engine_specs.bigquery import BigQueryEngineSpec

assert BigQueryEngineSpec.mask_encrypted_extra(None) is None


def test_parse_error_message() -> None:
"""
Test that we parse a received message and just extract the useful information.
Example errors:
bigquery error: 400 Table \"case_detail_all_suites\" must be qualified with a dataset (e.g. dataset.table).
(job ID: ddf30b05-44e8-4fbf-aa29-40bfccaed886)
-----Query Job SQL Follows-----
| . | . | . |\n 1:select * from case_detail_all_suites\n 2:LIMIT 1001\n | . | . | . |
"""
from superset.db_engine_specs.bigquery import BigQueryEngineSpec

message = 'bigquery error: 400 Table "case_detail_all_suites" must be qualified with a dataset (e.g. dataset.table).\n\n(job ID: ddf30b05-44e8-4fbf-aa29-40bfccaed886)\n\n -----Query Job SQL Follows----- \n\n | . | . | . |\n 1:select * from case_detail_all_suites\n 2:LIMIT 1001\n | . | . | . |'
expected_result = '400 Table "case_detail_all_suites" must be qualified with a dataset (e.g. dataset.table).'
assert (
str(BigQueryEngineSpec.parse_error_exception(Exception(message)))
== expected_result
)


def test_parse_error_raises_exception() -> None:
"""
Test that we handle any exception we might get from calling the parse_error_exception method.
Example errors:
400 Syntax error: Expected "(" or keyword UNNEST but got "@" at [4:80]
bigquery error: 400 Table \"case_detail_all_suites\" must be qualified with a dataset (e.g. dataset.table).
"""
from superset.db_engine_specs.bigquery import BigQueryEngineSpec

message = 'bigquery error: 400 Table "case_detail_all_suites" must be qualified with a dataset (e.g. dataset.table).'
message_2 = "6"
expected_result = '400 Table "case_detail_all_suites" must be qualified with a dataset (e.g. dataset.table).'
assert (
str(BigQueryEngineSpec.parse_error_exception(Exception(message)))
== expected_result
)
assert str(BigQueryEngineSpec.parse_error_exception(Exception(message_2))) == "6"

0 comments on commit 95b4c7b

Please sign in to comment.