-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconstants.py
87 lines (77 loc) · 3.03 KB
/
constants.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import inspect
import sys
from tabcmd.execution.localize import _
class Constants:
login_error = "401001"
invalid_credentials = "401002"
source_not_found = "404005"
forbidden = "403022"
resource_conflict_general = "409"
source_already_exists = "409006"
user_already_member_of_site = "409017"
class Errors:
@staticmethod
def is_expired_session(error):
if hasattr(error, "code"):
return error.code == Constants.invalid_credentials
@staticmethod
def is_resource_conflict(error):
if hasattr(error, "code"):
return error.code.startswith(Constants.resource_conflict_general)
@staticmethod
def is_login_error(error):
if hasattr(error, "code"):
return error.code == Constants.login_error
# https://gist.github.com/FredLoney/5454553
@staticmethod
def log_stack(logger):
if not logger:
print("logger not available: cannot show stack")
return
try:
"""The log header message formatter."""
HEADER_FMT = "Printing Call Stack at %s::%s"
"""The log stack message formatter."""
STACK_FMT = "%s, line %d in function %s."
stack = inspect.stack()
here = stack[0]
file, line, func = here[1:4]
start = 0
n_lines = 5
logger.debug(HEADER_FMT % (file, func))
for frame in stack[start + 1 : n_lines]:
file, line, func = frame[1:4]
logger.debug(STACK_FMT % (file, line, func))
except Exception as e:
logger.info("Error printing stack trace:", e)
@staticmethod
def exit_with_error(logger, message=None, exception=None):
try:
Errors.log_stack(logger)
if message and not exception:
logger.error(message)
if exception:
if message:
logger.debug("Error message: " + message)
Errors.check_common_error_codes_and_explain(logger, exception)
except Exception as exc:
print(sys.stderr, "Error during log call from exception - {} {}".format(exc.__class__, message))
try:
logger.info("Exiting...")
except Exception:
print(sys.stderr, "Exiting...")
sys.exit(1)
@staticmethod
def check_common_error_codes_and_explain(logger, exception):
# most errors contain as much info in the message as we can get from the code
# identify any that we can add useful detail for and include them here
if Errors.is_expired_session(exception):
# catch this one so we can attempt to refresh the session before telling them it failed
logger.error(_("session.errors.session_expired"))
# TODO: add session as an argument to this method
# and add the full command line as a field in Session?
# "session.session_expired_login"))
# session.renew_session()
return
else:
logger.error(exception)