Skip to content
Open
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
16 changes: 15 additions & 1 deletion databricks/sdk/errors/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
from .private_link import (_get_private_link_validation_error,
_is_private_link_redirect)

class _CatchAllErrorDeserializer:
"""
A catch-all deserializer that sets the entire response body as the error message.
"""

def deserialize_error(self, response: requests.Response, content: bytes) -> dict:
logging.warning('Unable to parse error with specific deserializers, using catch-all deserializer.')
return {
'message': content.decode('utf-8', errors='replace'),
'status_code': response.status_code,
'url': response.url
}

# A list of _ErrorDeserializers that are tried in order to parse an API error from a response body. Most errors should
# be parsable by the _StandardErrorDeserializer, but additional parsers can be added here for specific error formats.
# The order of the parsers is not important, as the set of errors that can be parsed by each parser should be disjoint.
Expand All @@ -21,6 +34,7 @@
_StandardErrorDeserializer(),
_StringErrorDeserializer(),
_HtmlErrorDeserializer(),
_CatchAllErrorDeserializer(),
]

# A list of _ErrorCustomizers that are applied to the error arguments after they are parsed. Customizers can modify the
Expand All @@ -38,7 +52,7 @@ def _unknown_error(response: requests.Response) -> str:
return (
'This is likely a bug in the Databricks SDK for Python or the underlying '
'API. Please report this issue with the following debugging information to the SDK issue tracker at '
f'https://github.com/databricks/databricks-sdk-go/issues. Request log:```{request_log}```')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, nice catch!

f'https://github.com/databricks/databricks-sdk-py/issues. Request log:```{request_log}```')


class _Parser:
Expand Down
20 changes: 11 additions & 9 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def make_private_link_response() -> requests.Response:
subclass_test_cases = [(fake_valid_response('GET', x[0], x[1], 'nope'), x[2], 'nope')
for x in base_subclass_test_cases]

UNABLE_TO_PARSE_RESPONSE_ERROR = (
'unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. '
'Please report this issue with the following debugging information to the SDK issue tracker at '
'https://github.com/databricks/databricks-sdk-py/issues. Request log:```GET /api/2.0/service\n'
'< {status_code} {reason}\n< {response_body}```')


@pytest.mark.parametrize(
'response, expected_error, expected_message', subclass_test_cases +
Expand Down Expand Up @@ -112,11 +118,8 @@ def make_private_link_response() -> requests.Response:
(fake_response('GET', 400, '<pre>Worker environment not ready</pre>'), errors.BadRequest,
'Worker environment not ready'),
(fake_response('GET', 400, 'this is not a real response'), errors.BadRequest,
('unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. '
'Please report this issue with the following debugging information to the SDK issue tracker at '
'https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n'
'< 400 Bad Request\n'
'< this is not a real response```')),
UNABLE_TO_PARSE_RESPONSE_ERROR.format(
status_code=400, reason='Bad Request', response_body='this is not a real response')),
(fake_response(
'GET', 404,
json.dumps({
Expand All @@ -125,11 +128,10 @@ def make_private_link_response() -> requests.Response:
'schemas': ['urn:ietf:params:scim:api:messages:2.0:Error']
})), errors.NotFound, 'None Group with id 1234 is not found'),
(fake_response('GET', 404, json.dumps("This is JSON but not a dictionary")), errors.NotFound,
'unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< "This is JSON but not a dictionary"```'
),
UNABLE_TO_PARSE_RESPONSE_ERROR.format(
status_code=404, reason='Not Found', response_body='"This is JSON but not a dictionary"')),
(fake_raw_response('GET', 404, b'\x80'), errors.NotFound,
'unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/service\n< 404 Not Found\n< �```'
)])
UNABLE_TO_PARSE_RESPONSE_ERROR.format(status_code=404, reason='Not Found', response_body='�'))])
def test_get_api_error(response, expected_error, expected_message):
parser = errors._Parser()
with pytest.raises(errors.DatabricksError) as e:
Expand Down
Loading