Skip to content

Commit

Permalink
fix: add detailed message in job error (#1762)
Browse files Browse the repository at this point in the history
* fix: more detailed job error message

* lint

* fix mypy error

* remove import ignore

* Update google/cloud/bigquery/job/base.py

Co-authored-by: Chalmer Lowe <[email protected]>

* Update google/cloud/bigquery/job/base.py

Co-authored-by: Chalmer Lowe <[email protected]>

* variable name and unit test

---------

Co-authored-by: Chalmer Lowe <[email protected]>
Co-authored-by: Chalmer Lowe <[email protected]>
  • Loading branch information
3 people committed Jan 10, 2024
1 parent 575f7fc commit 08483fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
25 changes: 22 additions & 3 deletions google/cloud/bigquery/job/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
}


def _error_result_to_exception(error_result):
def _error_result_to_exception(error_result, errors=None):
"""Maps BigQuery error reasons to an exception.
The reasons and their matching HTTP status codes are documented on
Expand All @@ -66,6 +66,7 @@ def _error_result_to_exception(error_result):
Args:
error_result (Mapping[str, str]): The error result from BigQuery.
errors (Union[Iterable[str], None]): The detailed error messages.
Returns:
google.cloud.exceptions.GoogleAPICallError: The mapped exception.
Expand All @@ -74,8 +75,24 @@ def _error_result_to_exception(error_result):
status_code = _ERROR_REASON_TO_EXCEPTION.get(
reason, http.client.INTERNAL_SERVER_ERROR
)
# Manually create error message to preserve both error_result and errors.
# Can be removed once b/310544564 and b/318889899 are resolved.
concatenated_errors = ""
if errors:
concatenated_errors = "; "
for err in errors:
concatenated_errors += ", ".join(
[f"{key}: {value}" for key, value in err.items()]
)
concatenated_errors += "; "

# strips off the last unneeded semicolon and space
concatenated_errors = concatenated_errors[:-2]

error_message = error_result.get("message", "") + concatenated_errors

return exceptions.from_http_status(
status_code, error_result.get("message", ""), errors=[error_result]
status_code, error_message, errors=[error_result]
)


Expand Down Expand Up @@ -886,7 +903,9 @@ def _set_future_result(self):
return

if self.error_result is not None:
exception = _error_result_to_exception(self.error_result)
exception = _error_result_to_exception(
self.error_result, self.errors or ()
)
self.set_exception(exception)
else:
self.set_result(self)
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/job/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ def test_missing_reason(self):
exception = self._call_fut(error_result)
self.assertEqual(exception.code, http.client.INTERNAL_SERVER_ERROR)

def test_contatenate_errors(self):
# Added test for b/310544564 and b/318889899.
# Ensures that error messages from both error_result and errors are
# present in the exception raised.

error_result = {
"reason": "invalid1",
"message": "error message 1",
}
errors = [
{"reason": "invalid2", "message": "error message 2"},
{"reason": "invalid3", "message": "error message 3"},
]

exception = self._call_fut(error_result, errors)
self.assertEqual(
exception.message,
"error message 1; reason: invalid2, message: error message 2; "
"reason: invalid3, message: error message 3",
)


class Test_JobReference(unittest.TestCase):
JOB_ID = "job-id"
Expand Down

0 comments on commit 08483fb

Please sign in to comment.