Skip to content

Commit b668b6e

Browse files
committed
Improving the error message of dbutils.credentials.getServiceCredentialsProvider
This method is not supported by the SDK DBUtils.
1 parent 64a5550 commit b668b6e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

databricks/sdk/dbutils.py

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

279279

280+
def not_supported_method_err_msg(methodName):
281+
return f"Method '{methodName}' is not supported in the SDK version of DBUtils"
282+
283+
280284
class _OverrideProxyUtil:
281285

282286
@classmethod
283287
def new(cls, path: str):
288+
if path in cls.not_supported_override_paths:
289+
raise ValueError(cls.not_supported_override_paths[path])
290+
284291
if len(cls.__get_matching_overrides(path)) > 0:
285292
return _OverrideProxyUtil(path)
286293
return None
@@ -297,6 +304,16 @@ def __init__(self, name: str):
297304
"notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()": get_local_notebook_path,
298305
}
299306

307+
# These paths work the same as 'proxy_override_paths' but instead of using a local implementation we raise an exception.
308+
not_supported_override_paths = {
309+
# The object returned by 'credentials.getServiceCredentialProvider()' can't be serialized to JSON.
310+
# Without this override, the command would fail with an error 'TypeError: Object of type Session is not JSON serializable'.
311+
# We override it to show a better error message
312+
"credentials.getServiceCredentialsProvider": not_supported_method_err_msg(
313+
"credentials.getServiceCredentialsProvider"
314+
),
315+
}
316+
300317
@classmethod
301318
def __get_matching_overrides(cls, path: str):
302319
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
@@ -290,3 +290,13 @@ def test_dbutils_proxy_overrides(dbutils, mocker, restorable_env):
290290
return_value="test_cluster_id",
291291
)
292292
assert dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() == "test_source_file"
293+
294+
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")

0 commit comments

Comments
 (0)