5
5
import logging
6
6
import os
7
7
from os .path import expanduser
8
+ import sys
8
9
9
10
import typing as t
10
11
32
33
hookimpl = pluggy .HookimplMarker ('abnosql.table' )
33
34
34
35
try :
35
- from google .api_core import exceptions as gex # type: ignore
36
+ from google .api_core .exceptions import ClientError # type: ignore
37
+ from google .auth .exceptions import GoogleAuthError # type: ignore
36
38
from google .cloud import firestore # type: ignore
37
39
except ImportError :
38
40
MISSING_DEPS = True
@@ -53,20 +55,23 @@ def decorator(func):
53
55
def wrapper (* args , ** kwargs ):
54
56
try :
55
57
return func (* args , ** kwargs )
56
- except gex . ClientError as e :
58
+ except ClientError as e :
57
59
if raise_not_found and e .code in [404 ]:
58
60
raise ex .NotFoundException () from None
59
61
raise ex .ValidationException (detail = e ) from None
62
+ except GoogleAuthError as e :
63
+ raise ex .ConfigException (detail = e ) from None
60
64
except ex .NoSQLException :
61
65
raise
62
66
except Exception as e :
63
- raise ex .PluginException (detail = e )
67
+ raise ex .PluginException (detail = e ) from None
64
68
return wrapper
65
69
return decorator
66
70
67
71
68
72
class Table (TableBase ):
69
73
74
+ @firestore_ex_handler ()
70
75
def __init__ (
71
76
self , pm : PM , name : str , config : t .Optional [dict ] = None
72
77
) -> None :
@@ -97,14 +102,19 @@ def _get_client(self):
97
102
if val is not None :
98
103
kwargs [attr ] = val
99
104
break
100
- cred_file = os .path .join (
101
- expanduser ('~' ),
102
- '.config' ,
103
- 'gcloud' ,
104
- 'application_default_credentials.json'
105
- )
106
- if os .path .isfile (cred_file ):
107
- os .environ ['GOOGLE_APPLICATION_CREDENTIALS' ] = cred_file
105
+ cred_file = os .path .join (* (
106
+ (
107
+ tuple (os .environ .get ('APPDATA' , '' ))
108
+ if sys .platform == 'win32'
109
+ else (expanduser ('~' ), '.config' )
110
+ ) + (
111
+ 'gcloud' ,
112
+ 'application_default_credentials.json'
113
+ )
114
+ ))
115
+ gac = 'GOOGLE_APPLICATION_CREDENTIALS'
116
+ if gac not in os .environ and os .path .isfile (cred_file ):
117
+ os .environ [gac ] = cred_file
108
118
return firestore .Client (** kwargs )
109
119
110
120
def _docid (self , ** kwargs ):
0 commit comments