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

fix: add detailed message in job error #1762

Merged
merged 9 commits into from Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions google/cloud/bigquery/job/base.py
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
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