Skip to content

[Fix] Add support for all error details types and fix potential unmarshalling error #912

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

Merged
merged 7 commits into from
Mar 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Documentation

### Internal Changes

* Refactor `DatabricksError` to expose different types of error details ([#912](https://github.com/databricks/databricks-sdk-py/pull/912)).
* Update Jobs ListJobs API to support paginated responses ([#896](https://github.com/databricks/databricks-sdk-py/pull/896))
* Update Jobs ListRuns API to support paginated responses ([#890](https://github.com/databricks/databricks-sdk-py/pull/890))
* Introduce automated tagging ([#888](https://github.com/databricks/databricks-sdk-py/pull/888))
Expand Down
46 changes: 34 additions & 12 deletions databricks/sdk/errors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import requests

from . import details as errdetails


# Deprecated.
class ErrorDetail:
def __init__(
self,
Expand All @@ -22,17 +25,18 @@ def __init__(

@classmethod
def from_dict(cls, d: Dict[str, any]) -> "ErrorDetail":
if "@type" in d:
d["type"] = d["@type"]
return cls(**d)
# Key "@type" is not a valid keyword argument name in Python. Rename
# it to "type" to avoid conflicts.
safe_args = {}
for k, v in d.items():
safe_args[k if k != "@type" else "type"] = v

return cls(**safe_args)


class DatabricksError(IOError):
"""Generic error from Databricks REST API"""

# Known ErrorDetail types
_error_info_type = "type.googleapis.com/google.rpc.ErrorInfo"

def __init__(
self,
message: str = None,
Expand All @@ -54,11 +58,11 @@ def __init__(
:param status: [Deprecated]
:param scimType: [Deprecated]
:param error: [Deprecated]
:param retry_after_secs:
:param retry_after_secs: [Deprecated]
:param details:
:param kwargs:
"""
# SCIM-specific parameters are deprecated
# SCIM-specific parameters are deprecated.
if detail:
warnings.warn(
"The 'detail' parameter of DatabricksError is deprecated and will be removed in a future version."
Expand All @@ -72,12 +76,18 @@ def __init__(
"The 'status' parameter of DatabricksError is deprecated and will be removed in a future version."
)

# API 1.2-specific parameters are deprecated
# API 1.2-specific parameters are deprecated.
if error:
warnings.warn(
"The 'error' parameter of DatabricksError is deprecated and will be removed in a future version."
)

# Retry-after is deprecated.
if retry_after_secs:
warnings.warn(
"The 'retry_after_secs' parameter of DatabricksError is deprecated and will be removed in a future version."
)

if detail:
# Handle SCIM error message details
# @see https://tools.ietf.org/html/rfc7644#section-3.7.3
Expand All @@ -88,20 +98,32 @@ def __init__(
# add more context from SCIM responses
message = f"{scimType} {message}".strip(" ")
error_code = f"SCIM_{status}"

super().__init__(message if message else error)
self.error_code = error_code
self.retry_after_secs = retry_after_secs
self.details = [ErrorDetail.from_dict(detail) for detail in details] if details else []
self._error_details = errdetails.parse_error_details(details)
self.kwargs = kwargs

# Deprecated.
self.details = []
if details:
for d in details:
if not isinstance(d, dict):
continue
self.details.append(ErrorDetail.from_dict(d))

def get_error_info(self) -> List[ErrorDetail]:
return self._get_details_by_type(DatabricksError._error_info_type)
return self._get_details_by_type(errdetails._ERROR_INFO_TYPE)

def _get_details_by_type(self, error_type) -> List[ErrorDetail]:
if self.details == None:
if self.details is None:
return []
return [detail for detail in self.details if detail.type == error_type]

def get_error_details(self) -> errdetails.ErrorDetails:
return self._error_details


@dataclass
class _ErrorOverride:
Expand Down
Loading
Loading