forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
192 lines (153 loc) · 6.69 KB
/
exceptions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
exceptions
==========
A module that contains plotly's exception hierarchy.
message (required!) (should be root message + caller message)
info: (required!)
path_to_error (required!)
minimal_message (required!)
- minimal_message is set inside this module, should not be set elsewhere
- message is set inside this module, should not be set elsewhere
"""
## Base Plotly Error ##
class PlotlyError(Exception):
pass
## Graph Objects Errors ##
class PlotlyGraphObjectError(PlotlyError):
def __init__(self, message='', path=None, notes=None, plain_message=''):
self.message = message
self.plain_message=plain_message
if isinstance(path, list):
self.path = path
elif path is None:
self.path = []
else:
self.path = [path]
if isinstance(notes, list):
self.notes = notes
elif notes is None:
self.notes = []
else:
self.notes = [notes]
super(PlotlyGraphObjectError, self).__init__(message)
self.prepare()
def add_note(self, note):
if isinstance(note, list):
self.notes += note
else:
self.notes += [note]
def add_to_error_path(self, path):
if isinstance(path, list):
self.path = path + self.path
else:
self.path = [path] + self.path
def prepare(self):
message = self.message
message += "\n\nPath To Error:\n["
for iii, key in enumerate(self.path):
message += repr(key)
if iii < len(self.path) - 1:
message += "]["
message += "]"
if len(self.notes):
message += "\n\nAdditional Notes:\n{}".format("\n".join(self.notes))
if len(self.args) > 1:
self.args = (message, self.args[1:][0])
else:
self.args = message,
class PlotlyDictKeyError(PlotlyGraphObjectError):
def __init__(self, obj='', key='', **kwargs):
message = (
"Invalid key, '{key}', for class, '{obj_name}'.\n\nRun "
"'help(plotly.graph_objs.{obj_name})' for more information."
"".format(key=key, obj_name=obj.__class__.__name__)
)
plain_message="invalid key, '{}', in dictionary".format(key)
super(PlotlyDictKeyError, self).__init__(message=message,
path=[key],
plain_message=plain_message,
**kwargs)
class PlotlyDictValueError(PlotlyGraphObjectError):
def __init__(self, obj='', key='', value='', val_types='', **kwargs):
message = (
"Invalid value type, '{value_name}', associated with key, "
"'{key}', for class, '{obj_name}'.\nValid types for this key "
"are:\n '{val_types}'.\n\nRun 'help(plotly.graph_objs.{obj_name})' "
"for more information.".format(key=key,
value_name=value.__class__.__name__,
val_types=val_types,
obj_name=obj.__class__.__name__)
)
plain_message = ("invalid value associated with key, '{}', in "
"dictionary".format(key))
super(PlotlyDictValueError, self).__init__(message=message,
plain_message=plain_message,
path=[key],
**kwargs)
class PlotlyListEntryError(PlotlyGraphObjectError):
def __init__(self, obj='', index='', entry='', **kwargs):
message = (
"The entry at index, '{}', is invalid in a '{}' object"
"".format(index, obj.__class__.__name__)
)
plain_message = (
"The entry at index, '{}', is invalid."
"".format(index)
)
super(PlotlyListEntryError, self).__init__(message=message,
plain_message=plain_message,
path=[index],
**kwargs)
class PlotlyDataTypeError(PlotlyGraphObjectError):
def __init__(self, obj='', index='', **kwargs):
message = (
"The entry at index, '{}', is invalid because it does not "
"contain a valid 'type' key-value. This is required for valid "
"'{}' lists.".format(index, obj.__class__.__name__)
)
plain_message = (
"The entry at index, '{}', is invalid because it does not "
"contain a valid 'type' key-value. This is required for "
"valid data lists.".format(index))
super(PlotlyDataTypeError, self).__init__(message=message,
plain_message=plain_message,
path=[index],
**kwargs)
## Local Config Errors ##
class PlotlyLocalError(PlotlyError):
pass
class PlotlyLocalCredentialsError(PlotlyLocalError):
def __init__(self):
message = ("\n"
"Couldn't find a 'username', 'api-key' pair for you on your local "
"machine. To sign in temporarily (until you stop running Python), "
"run:\n"
">>> import plotly.plotly as py\n"
">>> py.sign_in('username', 'api_key')\n\n"
"Even better, save your credentials permatently using the 'tools' "
"module:\n"
">>> import plotly.tools as tls\n"
">>> tls.set_credentials_file(username='username', api_key='api-key')\n\n"
"Note that 'username' should be a string containing YOUR Plotly "
"username and 'api-key' should be a string containing YOUR Plotly "
"api-key. To view your api-key:\n"
"1. Visit our website: https://plot.ly/\n"
"2. Sign-in using your Plotly username and password (different "
"from api-key)\n"
"3. Click the drop-down menu in the upper-right corner that says "
"your username.\n"
"4. Select 'settings' from this list.\n"
"5. Under the 'profile' tab in the settings list, you will find "
"your 'API Key'.\n")
super(PlotlyLocalCredentialsError, self).__init__(message)
## Server Errors ##
class PlotlyServerError(PlotlyError):
pass
class PlotlyConnectionError(PlotlyServerError):
pass
class PlotlyCredentialError(PlotlyServerError):
pass
class PlotlyAccountError(PlotlyServerError):
pass
class PlotlyRateLimitError(PlotlyServerError):
pass