FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adds error handling messages for the user when they should be adding … · HpcDataLab/docker-python@1f426ed · GitHub

Commit 1f426ed

Browse files
authored andcommitted
Adds error handling messages for the user when they should be adding … (Kaggle#539)
* Adds error handling messages for the user when they should be adding a BQ integration * Update copy.
1 parent 5d5d7a8 commit 1f426ed

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎patches/kaggle_gcp.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from google.auth import credentials
33
from google.auth.exceptions import RefreshError
44
from google.cloud import bigquery
5+
from google.cloud.exceptions import Forbidden
56
from google.cloud.bigquery._http import Connection
67
from kaggle_secrets import UserSecretsClient
78

@@ -39,6 +40,9 @@ def refresh(self, request):
3940
client = UserSecretsClient()
4041
self.token, self.expiry = client.get_bigquery_access_token()
4142
except Exception as e:
43+
if (not get_integrations().has_bigquery()):
44+
print(
45+
'Please ensure you have selected a BigQuery account in the Kernels Settings sidebar.')
4246
raise RefreshError('Unable to refresh access token.') from e
4347

4448

@@ -52,6 +56,16 @@ def __init__(self, client):
5256
self._EXTRA_HEADERS["X-KAGGLE-PROXY-DATA"] = os.getenv(
5357
"KAGGLE_DATA_PROXY_TOKEN")
5458

59+
def api_request(self, *args, **kwargs):
60+
"""Wrap Connection.api_request in order to handle errors gracefully.
61+
"""
62+
try:
63+
super().api_request(*args, **kwargs)
64+
except Forbidden as e:
65+
print("Permission denied using Kaggle's public BigQuery integration. "
66+
"Did you mean to select a BigQuery account in the Kernels Settings sidebar?")
67+
raise e
68+
5569

5670
class PublicBigqueryClient(bigquery.client.Client):
5771
"""A modified BigQuery client that routes requests using Kaggle's Data Proxy to provide free access to Public Datasets.

‎patches/sitecustomize.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
def monkeypatch_bq(bq_client, *args, **kwargs):
1616
specified_project = kwargs.get('project')
1717
specified_credentials = kwargs.get('credentials')
18-
kernel_integrations = get_integrations()
19-
if specified_project is None and specified_credentials is None and not kernel_integrations.has_bigquery():
18+
has_bigquery = get_integrations().has_bigquery()
19+
if specified_project is None and specified_credentials is None and not has_bigquery:
2020
print("Using Kaggle's public dataset BigQuery integration.")
2121
return PublicBigqueryClient(*args, **kwargs)
2222

2323
else:
2424
if specified_credentials is None:
2525
kwargs['credentials'] = KaggleKernelCredentials()
26+
if (not has_bigquery):
27+
print('Please ensure you have selected a BigQuery '
28+
'account in the Kernels Settings sidebar.')
2629
return bq_client(*args, **kwargs)
2730

2831
# Monkey patches BigQuery client creation to use proxy or user-connected GCP account.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL