44
55A module that contains plotly's exception hierarchy.
66
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-
16-
177"""
188import json
199
@@ -56,7 +46,7 @@ def __str__(self):
5646 return self .message
5747
5848
59- # Grid Errors #
49+ # Grid Errors
6050COLUMN_NOT_YET_UPLOADED_MESSAGE = (
6151 "Hm... it looks like your column '{column_name}' hasn't "
6252 "been uploaded to Plotly yet. You need to upload your "
@@ -72,126 +62,86 @@ def __str__(self):
7262 "the column \" {0}\" and try again."
7363)
7464
75- # Would Cause Server Errors
7665
7766class PlotlyEmptyDataError (PlotlyError ):
7867 pass
7968
8069
8170# Graph Objects Errors
8271class PlotlyGraphObjectError (PlotlyError ):
83- def __init__ (self , message = '' , path = None , notes = None , plain_message = '' ):
72+ def __init__ (self , message = '' , path = (), notes = ()):
73+ """
74+ General graph object error for validation failures.
75+
76+ :param (str|unicode) message: The error message.
77+ :param (iterable) path: A path pointing to the error.
78+ :param notes: Add additional notes, but keep default exception message.
79+
80+ """
8481 self .message = message
85- self .plain_message = plain_message
86- if isinstance (path , list ):
87- self .path = path
88- elif path is None :
89- self .path = []
90- else :
91- self .path = [path ]
92- if isinstance (notes , list ):
93- self .notes = notes
94- elif notes is None :
95- self .notes = []
96- else :
97- self .notes = [notes ]
82+ self .plain_message = message # for backwards compat
83+ self .path = list (path )
84+ self .notes = notes
9885 super (PlotlyGraphObjectError , self ).__init__ (message )
99- self .prepare ()
100-
101- def add_note (self , note ):
102- if isinstance (note , list ):
103- self .notes += note
104- else :
105- self .notes += [note ]
10686
107- def add_to_error_path (self , path ):
108- if isinstance (path , list ):
109- self .path = path + self .path
110- else :
111- self .path = [path ] + self .path
112-
113- def prepare (self ):
114- message = self .message
115- message += "\n \n Path To Error:\n ["
116- for iii , key in enumerate (self .path ):
117- message += repr (key )
118- if iii < len (self .path ) - 1 :
119- message += "]["
120- message += "]"
121- if len (self .notes ):
122- message += "\n \n Additional Notes:\n {0}" .format (
123- "\n " .join (self .notes ))
124- if len (self .args ) > 1 :
125- self .args = (message , self .args [1 :][0 ])
126- else :
127- self .args = message ,
87+ def __str__ (self ):
88+ """This is called by Python to present the error message."""
89+ format_dict = {
90+ 'message' : self .message ,
91+ 'path' : '[' + '][' .join (repr (k ) for k in self .path ) + ']' ,
92+ 'notes' : '\n ' .join (self .notes )
93+ }
94+ return ('{message}\n \n Path To Error: {path}\n \n {notes}'
95+ .format (** format_dict ))
12896
12997
13098class PlotlyDictKeyError (PlotlyGraphObjectError ):
131- def __init__ (self , obj = '' , key = '' , ** kwargs ):
132- message = (
133- "Invalid key, '{key}', for class, '{obj_name}'.\n \n Run "
134- "'help(plotly.graph_objs.{obj_name})' for more information."
135- "" .format (key = key , obj_name = obj .__class__ .__name__ )
99+ def __init__ (self , obj , path , notes = ()):
100+ """See PlotlyGraphObjectError.__init__ for param docs."""
101+ format_dict = {'attribute' : path [- 1 ], 'object_name' : obj ._name }
102+ message = ("'{attribute}' is not allowed in '{object_name}'"
103+ .format (** format_dict ))
104+ notes = [obj .help (return_help = True )] + list (notes )
105+ super (PlotlyDictKeyError , self ).__init__ (
106+ message = message , path = path , notes = notes
136107 )
137- plain_message = ("Invalid key, '{key}', found in '{obj}' object"
138- "" .format (key = key , obj = obj .__class__ .__name__ ))
139- super (PlotlyDictKeyError , self ).__init__ (message = message ,
140- path = [key ],
141- plain_message = plain_message ,
142- ** kwargs )
143108
144109
145110class PlotlyDictValueError (PlotlyGraphObjectError ):
146- def __init__ (self , obj = '' , key = '' , value = '' , val_types = '' , ** kwargs ):
147- message = (
148- "Invalid value type, '{value_name}', associated with key, "
149- "'{key}', for class, '{obj_name}'.\n Valid types for this key "
150- "are:\n '{val_types}'.\n \n Run 'help(plotly.graph_objs.{obj_name})' "
151- "for more information." .format (key = key ,
152- value_name = value .__class__ .__name__ ,
153- val_types = val_types ,
154- obj_name = obj .__class__ .__name__ )
111+ def __init__ (self , obj , path , notes = ()):
112+ """See PlotlyGraphObjectError.__init__ for param docs."""
113+ format_dict = {'attribute' : path [- 1 ], 'object_name' : obj ._name }
114+ message = ("'{attribute}' has invalid value inside '{object_name}'"
115+ .format (** format_dict ))
116+ notes = [obj .help (path [- 1 ], return_help = True )] + list (notes )
117+ super (PlotlyDictValueError , self ).__init__ (
118+ message = message , notes = notes , path = path
155119 )
156- plain_message = ("Invalid value found in '{obj}' associated with key, "
157- "'{key}'" .format (key = key , obj = obj .__class__ .__name__ ))
158- super (PlotlyDictValueError , self ).__init__ (message = message ,
159- plain_message = plain_message ,
160- path = [key ],
161- ** kwargs )
162120
163121
164122class PlotlyListEntryError (PlotlyGraphObjectError ):
165- def __init__ (self , obj = '' , index = '' , entry = '' , ** kwargs ):
166- message = (
167- "The entry at index, '{0}', is invalid in a '{1}' object"
168- "" . format ( index , obj . __class__ . __name__ )
169- )
170- plain_message = (
171- "Invalid entry found in '{obj}' object at index, '{index}'."
172- "" . format ( obj = obj . __class__ . __name__ , index = index )
123+ def __init__ (self , obj , path , notes = () ):
124+ """See PlotlyGraphObjectError.__init__ for param docs."""
125+ format_dict = { 'index' : path [ - 1 ], 'object_name' : obj . _name }
126+ message = ( "Invalid entry found in '{object_name}' at index, '{index}'"
127+ . format ( ** format_dict ) )
128+ notes = [ obj . help ( return_help = True )] + list ( notes )
129+ super ( PlotlyListEntryError , self ). __init__ (
130+ message = message , path = path , notes = notes
173131 )
174- super (PlotlyListEntryError , self ).__init__ (message = message ,
175- plain_message = plain_message ,
176- path = [index ],
177- ** kwargs )
178132
179133
180134class PlotlyDataTypeError (PlotlyGraphObjectError ):
181- def __init__ (self , obj = '' , index = '' , ** kwargs ):
182- message = (
183- "The entry at index, '{0}', is invalid because it does not "
184- "contain a valid 'type' key-value. This is required for valid "
185- "'{1}' lists." .format (index , obj .__class__ .__name__ )
135+ def __init__ (self , obj , path , notes = ()):
136+ """See PlotlyGraphObjectError.__init__ for param docs."""
137+ format_dict = {'index' : path [- 1 ], 'object_name' : obj ._name }
138+ message = ("Invalid entry found in '{object_name}' at index, '{index}'"
139+ .format (** format_dict ))
140+ note = "It's invalid because it does't contain a valid 'type' value."
141+ notes = [note ] + list (notes )
142+ super (PlotlyDataTypeError , self ).__init__ (
143+ message = message , path = path , notes = notes
186144 )
187- plain_message = (
188- "Invalid entry found in 'data' object at index, '{0}'. It "
189- "does not contain a valid 'type' key, required for 'data' "
190- "lists." .format (index ))
191- super (PlotlyDataTypeError , self ).__init__ (message = message ,
192- plain_message = plain_message ,
193- path = [index ],
194- ** kwargs )
195145
196146
197147# Local Config Errors
@@ -201,7 +151,8 @@ class PlotlyLocalError(PlotlyError):
201151
202152class PlotlyLocalCredentialsError (PlotlyLocalError ):
203153 def __init__ (self ):
204- message = ("\n "
154+ message = (
155+ "\n "
205156 "Couldn't find a 'username', 'api-key' pair for you on your local "
206157 "machine. To sign in temporarily (until you stop running Python), "
207158 "run:\n "
@@ -210,8 +161,10 @@ def __init__(self):
210161 "Even better, save your credentials permanently using the 'tools' "
211162 "module:\n "
212163 ">>> import plotly.tools as tls\n "
213- ">>> tls.set_credentials_file(username='username', api_key='api-key')\n \n "
214- "For more help, see https://plot.ly/python.\n " )
164+ ">>> tls.set_credentials_file(username='username', "
165+ "api_key='api-key')\n \n "
166+ "For more help, see https://plot.ly/python.\n "
167+ )
215168 super (PlotlyLocalCredentialsError , self ).__init__ (message )
216169
217170
0 commit comments