Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afad7bd

Browse files
committed
Merge branch 'exceptions'
New exception handeling for better error messages. See exceptions.py and graph_objs.py (most effected)
2 parents 7ff7957 + a5c2ddc commit afad7bd

File tree

5 files changed

+460
-211
lines changed

5 files changed

+460
-211
lines changed

plotly/exceptions.py

Lines changed: 164 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,191 @@
22
exceptions
33
==========
44
5-
A module that contains plotly's exception heirarchy.
5+
A module that contains plotly's exception hierarchy.
6+
7+
message (required!) (should be root message + caller message)
8+
info: (required!)
9+
path_to_error (required!)
10+
minimal_message (required!)
11+
12+
- minimal_message is set inside this module, should not be set elsewhere
13+
14+
- message is set inside this module, should not be set elsewhere
15+
616
717
"""
818

919

20+
## Base Plotly Error ##
21+
1022
class PlotlyError(Exception):
1123
pass
1224

13-
class PlotlyInstantiationError(PlotlyError):
14-
pass
1525

16-
class PlotlyInvalidKeyError(PlotlyError):
26+
## Graph Objects Errors ##
27+
28+
class PlotlyGraphObjectError(PlotlyError):
29+
def __init__(self, message='', path=None, notes=None, plain_message=''):
30+
self.message = message
31+
self.plain_message=plain_message
32+
if isinstance(path, list):
33+
self.path = path
34+
elif path is None:
35+
self.path = []
36+
else:
37+
self.path = [path]
38+
if isinstance(notes, list):
39+
self.notes = notes
40+
elif notes is None:
41+
self.notes = []
42+
else:
43+
self.notes = [notes]
44+
super(PlotlyGraphObjectError, self).__init__(message)
45+
self.prepare()
46+
47+
def add_note(self, note):
48+
if isinstance(note, list):
49+
self.notes += note
50+
else:
51+
self.notes += [note]
52+
53+
def add_to_error_path(self, path):
54+
if isinstance(path, list):
55+
self.path = path + self.path
56+
else:
57+
self.path = [path] + self.path
58+
59+
def prepare(self):
60+
message = self.message
61+
message += "\n\nPath To Error:\n["
62+
for iii, key in enumerate(self.path):
63+
message += repr(key)
64+
if iii < len(self.path) - 1:
65+
message += "]["
66+
message += "]"
67+
if len(self.notes):
68+
message += "\n\nAdditional Notes:\n{}".format("\n".join(self.notes))
69+
if len(self.args) > 1:
70+
self.args = (message, self.args[1:][0])
71+
else:
72+
self.args = message,
73+
74+
75+
class PlotlyDictKeyError(PlotlyGraphObjectError):
76+
def __init__(self, obj='', key='', **kwargs):
77+
message = (
78+
"Invalid key, '{key}', for class, '{obj_name}'.\n\nRun "
79+
"'help(plotly.graph_objs.{obj_name})' for more information."
80+
"".format(key=key, obj_name=obj.__class__.__name__)
81+
)
82+
plain_message="invalid key, '{}', in dictionary".format(key)
83+
super(PlotlyDictKeyError, self).__init__(message=message,
84+
path=[key],
85+
plain_message=plain_message,
86+
**kwargs)
87+
88+
89+
class PlotlyDictValueError(PlotlyGraphObjectError):
90+
def __init__(self, obj='', key='', value='', val_types='', **kwargs):
91+
message = (
92+
"Invalid value type, '{value_name}', associated with key, "
93+
"'{key}', for class, '{obj_name}'.\nValid types for this key "
94+
"are:\n '{val_types}'.\n\nRun 'help(plotly.graph_objs.{obj_name})' "
95+
"for more information.".format(key=key,
96+
value_name=value.__class__.__name__,
97+
val_types=val_types,
98+
obj_name=obj.__class__.__name__)
99+
)
100+
plain_message = ("invalid value associated with key, '{}', in "
101+
"dictionary".format(key))
102+
super(PlotlyDictValueError, self).__init__(message=message,
103+
plain_message=plain_message,
104+
path=[key],
105+
**kwargs)
106+
107+
108+
class PlotlyListEntryError(PlotlyGraphObjectError):
109+
def __init__(self, obj='', index='', entry='', **kwargs):
110+
message = (
111+
"The entry at index, '{}', is invalid in a '{}' object"
112+
"".format(index, obj.__class__.__name__)
113+
)
114+
plain_message = (
115+
"The entry at index, '{}', is invalid."
116+
"".format(index)
117+
)
118+
super(PlotlyListEntryError, self).__init__(message=message,
119+
plain_message=plain_message,
120+
path=[index],
121+
**kwargs)
122+
123+
124+
class PlotlyDataTypeError(PlotlyGraphObjectError):
125+
def __init__(self, obj='', index='', **kwargs):
126+
message = (
127+
"The entry at index, '{}', is invalid because it does not "
128+
"contain a valid 'type' key-value. This is required for valid "
129+
"'{}' lists.".format(index, obj.__class__.__name__)
130+
)
131+
plain_message = (
132+
"The entry at index, '{}', is invalid because it does not "
133+
"contain a valid 'type' key-value. This is required for "
134+
"valid data lists.".format(index))
135+
super(PlotlyDataTypeError, self).__init__(message=message,
136+
plain_message=plain_message,
137+
path=[index],
138+
**kwargs)
139+
140+
141+
## Local Config Errors ##
142+
143+
class PlotlyLocalError(PlotlyError):
17144
pass
18145

19146

20-
class PlotlyInvalidListItemError(PlotlyError):
147+
class PlotlyLocalCredentialsError(PlotlyLocalError):
148+
def __init__(self):
149+
message = ("\n"
150+
"Couldn't find a 'username', 'api-key' pair for you on your local "
151+
"machine. To sign in temporarily (until you stop running Python), "
152+
"run:\n"
153+
">>> import plotly.plotly as py\n"
154+
">>> py.sign_in('username', 'api_key')\n\n"
155+
"Even better, save your credentials permatently using the 'tools' "
156+
"module:\n"
157+
">>> import plotly.tools as tls\n"
158+
">>> tls.set_credentials_file(username='username', api_key='api-key')\n\n"
159+
"Note that 'username' should be a string containing YOUR Plotly "
160+
"username and 'api-key' should be a string containing YOUR Plotly "
161+
"api-key. To view your api-key:\n"
162+
"1. Visit our website: https://plot.ly/\n"
163+
"2. Sign-in using your Plotly username and password (different "
164+
"from api-key)\n"
165+
"3. Click the drop-down menu in the upper-right corner that says "
166+
"your username.\n"
167+
"4. Select 'settings' from this list.\n"
168+
"5. Under the 'profile' tab in the settings list, you will find "
169+
"your 'API Key'.\n")
170+
super(PlotlyLocalCredentialsError, self).__init__(message)
171+
172+
173+
## Server Errors ##
174+
175+
class PlotlyServerError(PlotlyError):
21176
pass
22177

23178

24-
class PlotlyConnectionError(PlotlyError):
179+
class PlotlyConnectionError(PlotlyServerError):
25180
pass
26181

27182

28-
class PlotlyCredentialError(PlotlyError):
183+
class PlotlyCredentialError(PlotlyServerError):
29184
pass
30185

31186

32-
class PlotlyAccountError(PlotlyError):
187+
class PlotlyAccountError(PlotlyServerError):
33188
pass
34189

35190

36-
class PlotlyRateLimitError(PlotlyError):
191+
class PlotlyRateLimitError(PlotlyServerError):
37192
pass

0 commit comments

Comments
 (0)