forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
174 lines (131 loc) · 5.41 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
"""
exceptions
==========
A module that contains plotly's exception hierarchy.
"""
from __future__ import absolute_import
from plotly.api.utils import to_native_utf8_string
# Base Plotly Error
class PlotlyError(Exception):
pass
class InputError(PlotlyError):
pass
class PlotlyRequestError(PlotlyError):
"""General API error. Raised for *all* failed requests."""
def __init__(self, message, status_code, content):
self.message = to_native_utf8_string(message)
self.status_code = status_code
self.content = content
def __str__(self):
return self.message
# Grid Errors
COLUMN_NOT_YET_UPLOADED_MESSAGE = (
"Hm... it looks like your column '{column_name}' hasn't "
"been uploaded to Plotly yet. You need to upload your "
"column to Plotly before you can assign it to '{reference}'.\n"
"To upload, try `plotly.plotly.grid_objs.upload` or "
"`plotly.plotly.grid_objs.append_column`.\n"
"Questions? chris@plot.ly"
)
NON_UNIQUE_COLUMN_MESSAGE = (
"Yikes, plotly grids currently "
"can't have duplicate column names. Rename "
"the column \"{0}\" and try again."
)
class PlotlyEmptyDataError(PlotlyError):
pass
# Graph Objects Errors
class PlotlyGraphObjectError(PlotlyError):
def __init__(self, message='', path=(), notes=()):
"""
General graph object error for validation failures.
:param (str|unicode) message: The error message.
:param (iterable) path: A path pointing to the error.
:param notes: Add additional notes, but keep default exception message.
"""
self.message = message
self.plain_message = message # for backwards compat
self.path = list(path)
self.notes = notes
super(PlotlyGraphObjectError, self).__init__(message)
def __str__(self):
"""This is called by Python to present the error message."""
format_dict = {
'message': self.message,
'path': '[' + ']['.join(repr(k) for k in self.path) + ']',
'notes': '\n'.join(self.notes)
}
return ('{message}\n\nPath To Error: {path}\n\n{notes}'
.format(**format_dict))
class PlotlyDictKeyError(PlotlyGraphObjectError):
def __init__(self, obj, path, notes=()):
"""See PlotlyGraphObjectError.__init__ for param docs."""
format_dict = {'attribute': path[-1], 'object_name': obj._name}
message = ("'{attribute}' is not allowed in '{object_name}'"
.format(**format_dict))
notes = [obj.help(return_help=True)] + list(notes)
super(PlotlyDictKeyError, self).__init__(
message=message, path=path, notes=notes
)
class PlotlyDictValueError(PlotlyGraphObjectError):
def __init__(self, obj, path, notes=()):
"""See PlotlyGraphObjectError.__init__ for param docs."""
format_dict = {'attribute': path[-1], 'object_name': obj._name}
message = ("'{attribute}' has invalid value inside '{object_name}'"
.format(**format_dict))
notes = [obj.help(path[-1], return_help=True)] + list(notes)
super(PlotlyDictValueError, self).__init__(
message=message, notes=notes, path=path
)
class PlotlyListEntryError(PlotlyGraphObjectError):
def __init__(self, obj, path, notes=()):
"""See PlotlyGraphObjectError.__init__ for param docs."""
format_dict = {'index': path[-1], 'object_name': obj._name}
message = ("Invalid entry found in '{object_name}' at index, '{index}'"
.format(**format_dict))
notes = [obj.help(return_help=True)] + list(notes)
super(PlotlyListEntryError, self).__init__(
message=message, path=path, notes=notes
)
class PlotlyDataTypeError(PlotlyGraphObjectError):
def __init__(self, obj, path, notes=()):
"""See PlotlyGraphObjectError.__init__ for param docs."""
format_dict = {'index': path[-1], 'object_name': obj._name}
message = ("Invalid entry found in '{object_name}' at index, '{index}'"
.format(**format_dict))
note = "It's invalid because it doesn't contain a valid 'type' value."
notes = [note] + list(notes)
super(PlotlyDataTypeError, self).__init__(
message=message, path=path, notes=notes
)
# 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 permanently using the 'tools' "
"module:\n"
">>> import plotly.tools as tls\n"
">>> tls.set_credentials_file(username='username', "
"api_key='api-key')\n\n"
"For more help, see https://plot.ly/python.\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