-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
67 lines (63 loc) · 2.47 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import requests
# Proxy for handling pyrebase/firebase errors
def handle_firebase_action(
action_function, exception_type, error_dict, *fn_args, **fn_kwargs
):
"""
Handler for pyrebase/firebase actions.
"""
error = None
result = None
try:
# print("Args", fn_args, fn_kwargs)
result = action_function(*fn_args, **fn_kwargs)
except requests.HTTPError as e:
# Pyrebase/4 is intercepting and raising an incorrectly initialized requests.HTTPError,
# so the response has to be extracted from builtin BaseException args rather than e.response
response_dict = json.loads(e.args[0].response.text.replace('"', '"'))
error_type = None
try:
error_type = response_dict["error"]["message"]
except:
try:
error_type = response_dict["error"]["errors"][0]["message"]
except:
pass
if e.args[0].response.status_code == 400:
if not error_dict:
error = exception_type(
f"Unknown {exception_type.__name__} while trying to {action_function.__name__} ({error_type})",
error_type,
e.args[0],
)
else:
if error_type in error_dict:
error = exception_type(
error_dict[error_type], error_type, e.args[0]
)
else:
for key in error_dict:
if key in error_type:
message = error_dict[key] + " " + error_type.split(" : ")[1]
error = exception_type(message, error_type, e.args[0])
if not error:
error = exception_type(
f"Unknown {exception_type.__name__} while trying to {action_function.__name__} ({error_type})",
error_type,
e.args[0],
)
else:
error = exception_type(
f"Storage network error, please try again later ({e.args[0].response.status_code}) ({error_type})",
error_type,
e.args[0],
)
print(e.args[0])
if not error:
raise
except requests.ConnectionError as e:
error = exception_type(
"Storage connection error, please try again later", f"SERVER_{e.args[0]}", e
)
return (result, error)