Skip to content

Commit 9348464

Browse files
committed
Improve the error message when User Default Credentials are not provided
1 parent 9228492 commit 9348464

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

google/cloud/dataproc_spark_connect/session.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
PermissionDenied,
4040
)
4141
from google.api_core.future.polling import POLLING_PREDICATE
42+
from google.auth.exceptions import DefaultCredentialsError
4243
from google.cloud.dataproc_spark_connect.client import DataprocChannelBuilder
4344
from google.cloud.dataproc_spark_connect.exceptions import DataprocSparkConnectException
4445
from google.cloud.dataproc_spark_connect.pypi_artifacts import PyPiArtifacts
@@ -456,6 +457,15 @@ def create_session_pbar():
456457
raise DataprocSparkConnectException(
457458
f"Error while creating Dataproc Session: {e.message}"
458459
)
460+
except DefaultCredentialsError as e:
461+
stop_create_session_pbar_event.set()
462+
if create_session_pbar_thread.is_alive():
463+
create_session_pbar_thread.join()
464+
DataprocSparkSession._active_s8s_session_id = None
465+
DataprocSparkSession._active_session_uses_custom_id = False
466+
raise DataprocSparkConnectException(
467+
f"Error while creating Dataproc Session: {e}"
468+
)
459469
except Exception as e:
460470
stop_create_session_pbar_event.set()
461471
if create_session_pbar_thread.is_alive():

tests/unit/test_session.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,16 @@ def test_create_session_without_location(self):
14871487
except DataprocSparkConnectException as e:
14881488
self.assertIn("location is not set", str(e))
14891489

1490+
def test_create_session_without_application_default_credentials(self):
1491+
"""Tests that an exception is raised when application default credentials is not provided."""
1492+
os.environ.clear()
1493+
try:
1494+
DataprocSparkSession.builder.location("test-region").projectId(
1495+
"test-project"
1496+
).getOrCreate()
1497+
except DataprocSparkConnectException as e:
1498+
self.assertIn("Your default credentials were not found", str(e))
1499+
14901500

14911501
class DataprocSparkConnectClientTest(unittest.TestCase):
14921502

0 commit comments

Comments
 (0)