1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Errors for the library.
16
17 All exceptions defined by the library
18 should be defined in this file.
19 """
20 from __future__ import absolute_import
21
22 __author__ = "jcgregorio@google.com (Joe Gregorio)"
23
24 import json
25
26 from googleapiclient import _helpers as util
27
28
29 -class Error(Exception):
30 """Base error for this module."""
31
32 pass
33
36 """HTTP data was invalid or unexpected."""
37
38 @util.positional(3)
39 - def __init__(self, resp, content, uri=None):
40 self.resp = resp
41 if not isinstance(content, bytes):
42 raise TypeError("HTTP content should be bytes")
43 self.content = content
44 self.uri = uri
45 self.error_details = ""
46
48 """Calculate the reason for the error from the response content."""
49 reason = self.resp.reason
50 try:
51 data = json.loads(self.content.decode("utf-8"))
52 if isinstance(data, dict):
53 reason = data["error"]["message"]
54 if "details" in data["error"]:
55 self.error_details = data["error"]["details"]
56 elif "detail" in data["error"]:
57 self.error_details = data["error"]["detail"]
58 elif isinstance(data, list) and len(data) > 0:
59 first_error = data[0]
60 reason = first_error["error"]["message"]
61 if "details" in first_error["error"]:
62 self.error_details = first_error["error"]["details"]
63 except (ValueError, KeyError, TypeError):
64 pass
65 if reason is None:
66 reason = ""
67 return reason
68
70 reason = self._get_reason()
71 if self.error_details:
72 return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % (
73 self.resp.status,
74 self.uri,
75 reason.strip(),
76 self.error_details,
77 )
78 elif self.uri:
79 return '<HttpError %s when requesting %s returned "%s">' % (
80 self.resp.status,
81 self.uri,
82 self._get_reason().strip(),
83 )
84 else:
85 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
86
87 __str__ = __repr__
88
91 """The JSON returned could not be parsed."""
92
93 pass
94
97 """File type unknown or unexpected."""
98
99 pass
100
103 """Link type unknown or unexpected."""
104
105 pass
106
109 """No API with that name and version exists."""
110
111 pass
112
115 """That is an unacceptable mimetype for this operation."""
116
117 pass
118
124
127 """Error occurred during resumable upload."""
128
129 pass
130
133 """The given chunksize is not valid."""
134
135 pass
136
139 """The channel Notification is invalid."""
140
141 pass
142
145 """Error occurred during batch operations."""
146
147 @util.positional(2)
148 - def __init__(self, reason, resp=None, content=None):
149 self.resp = resp
150 self.content = content
151 self.reason = reason
152
154 if getattr(self.resp, "status", None) is None:
155 return '<BatchError "%s">' % (self.reason)
156 else:
157 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
158
159 __str__ = __repr__
160
163 """Exception raised by RequestMockBuilder on unexpected calls."""
164
165 @util.positional(1)
167 """Constructor for an UnexpectedMethodError."""
168 super(UnexpectedMethodError, self).__init__(
169 "Received unexpected call %s" % methodId
170 )
171
172
173 -class UnexpectedBodyError(Error):
174 """Exception raised by RequestMockBuilder on unexpected bodies."""
175
176 - def __init__(self, expected, provided):
177 """Constructor for an UnexpectedMethodError."""
178 super(UnexpectedBodyError, self).__init__(
179 "Expected: [%s] - Provided: [%s]" % (expected, provided)
180 )
181