Skip to content

Commit 0bc2d75

Browse files
Improving the error message of dbutils.credentials.getServiceCredentialsProvider (#1063)
## What changes are proposed in this pull request? - Improving the error message of dbutils.credentials.getServiceCredentialsProvider - This method is not supported by the SDK DBUtils. We do something similar in the [databricks-dbutils-scala](https://github.com/databricks/databricks-dbutils-scala/blob/main/databricks-dbutils-scala/src/main/scala/com/databricks/sdk/scala/dbutils/SdkDBUtilsImpl.scala#L38) library. ## How is this tested? - Unit tests. - I manually tested it and confirm the correct message is shown. --------- Signed-off-by: Felipe <[email protected]>
1 parent 59de7cf commit 0bc2d75

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features and Improvements
66

77
### Bug Fixes
8+
- Improving the error message that is shown when the unsupported `dbutils.credentials.getServiceCredentialsProvider` method is used. This method can only be used inside of a notebook.
89

910
### Documentation
1011

databricks/sdk/dbutils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,17 @@ def get_local_notebook_path():
281281
return value
282282

283283

284+
def not_supported_method_err_msg(methodName):
285+
return f"Method '{methodName}' is not supported in the SDK version of DBUtils"
286+
287+
284288
class _OverrideProxyUtil:
285289

286290
@classmethod
287291
def new(cls, path: str):
292+
if path in cls.not_supported_override_paths:
293+
raise ValueError(cls.not_supported_override_paths[path])
294+
288295
if len(cls.__get_matching_overrides(path)) > 0:
289296
return _OverrideProxyUtil(path)
290297
return None
@@ -301,6 +308,16 @@ def __init__(self, name: str):
301308
"notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()": get_local_notebook_path,
302309
}
303310

311+
# These paths work the same as 'proxy_override_paths' but instead of using a local implementation we raise an exception.
312+
not_supported_override_paths = {
313+
# The object returned by 'credentials.getServiceCredentialProvider()' can't be serialized to JSON.
314+
# Without this override, the command would fail with an error 'TypeError: Object of type Session is not JSON serializable'.
315+
# We override it to show a better error message
316+
"credentials.getServiceCredentialsProvider": not_supported_method_err_msg(
317+
"credentials.getServiceCredentialsProvider"
318+
),
319+
}
320+
304321
@classmethod
305322
def __get_matching_overrides(cls, path: str):
306323
return [x for x in cls.proxy_override_paths.keys() if x.startswith(path)]

tests/test_dbutils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ def test_dbutils_proxy_overrides(dbutils, mocker, restorable_env):
292292
assert dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() == "test_source_file"
293293

294294

295+
@raises("Method 'credentials.getServiceCredentialsProvider' is not supported in the SDK version of DBUtils")
296+
def test_dbutils_credentials_get_service_credential_provider(config, mocker):
297+
from databricks.sdk.dbutils import RemoteDbUtils
298+
299+
config.cluster_id = "test_cluster_id"
300+
dbutils = RemoteDbUtils(config)
301+
302+
dbutils.credentials.getServiceCredentialsProvider("creds")
303+
304+
295305
def test_dbutils_adds_user_agent(config):
296306
from databricks.sdk.dbutils import RemoteDbUtils
297307

0 commit comments

Comments
 (0)