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

feat: Adding reason, domain, metadata & error_details fields in Custom Exceptions for additional info #804

Merged
merged 5 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -281,11 +281,11 @@ def execute(self, sql, args=None):
self._do_execute_update, sql, args or None
)
except (AlreadyExists, FailedPrecondition, OutOfRange) as e:
raise IntegrityError(getattr(e, "details", e))
raise IntegrityError(getattr(e, "details", e)) from e
except InvalidArgument as e:
raise ProgrammingError(getattr(e, "details", e))
raise ProgrammingError(getattr(e, "details", e)) from e
except InternalServerError as e:
raise OperationalError(getattr(e, "details", e))
raise OperationalError(getattr(e, "details", e)) from e

@check_not_closed
def executemany(self, operation, seq_of_params):
Expand Down
62 changes: 61 additions & 1 deletion google/cloud/spanner_dbapi/exceptions.py
Expand Up @@ -14,6 +14,8 @@

"""Spanner DB API exceptions."""

from google.api_core.exceptions import GoogleAPICallError


class Warning(Exception):
"""Important DB API warning."""
Expand All @@ -27,7 +29,65 @@ class Error(Exception):
Does not include :class:`Warning`.
"""

pass
def _is_error_cause_instance_of_google_api_exception(self):
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
return isinstance(self.__cause__, GoogleAPICallError)

@property
def reason(self):
"""The reason of the error.
Reference:
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
gauravpurohit06 marked this conversation as resolved.
Show resolved Hide resolved
Returns:
Union[str, None]: An optional string containing reason of the error.
"""
return (
self.__cause__.reason
if self._is_error_cause_instance_of_google_api_exception()
else None
)

@property
def domain(self):
"""The logical grouping to which the "reason" belongs.
Reference:
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
Returns:
Union[str, None]: An optional string containing a logical grouping to which the "reason" belongs.
"""
return (
self.__cause__.domain
if self._is_error_cause_instance_of_google_api_exception()
else None
)

@property
def metadata(self):
"""Additional structured details about this error.
Reference:
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
Returns:
Union[Dict[str, str], None]: An optional object containing structured details about the error.
"""
return (
self.__cause__.metadata
if self._is_error_cause_instance_of_google_api_exception()
else None
)

@property
def details(self):
"""Information contained in google.rpc.status.details.
Reference:
https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
Returns:
Sequence[Any]: A list of structured objects from error_details.proto
"""
return (
self.__cause__.details
if self._is_error_cause_instance_of_google_api_exception()
else None
)


class InterfaceError(Error):
Expand Down