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 oauth2client import util
27
28
29 -class Error(Exception):
30 """Base error for this module."""
31 pass
32
35 """HTTP data was invalid or unexpected."""
36
37 @util.positional(3)
38 - def __init__(self, resp, content, uri=None):
39 self.resp = resp
40 if not isinstance(content, bytes):
41 raise TypeError("HTTP content should be bytes")
42 self.content = content
43 self.uri = uri
44
46 """Calculate the reason for the error from the response content."""
47 reason = self.resp.reason
48 try:
49 data = json.loads(self.content.decode('utf-8'))
50 reason = data['error']['message']
51 except (ValueError, KeyError):
52 pass
53 if reason is None:
54 reason = ''
55 return reason
56
58 if self.uri:
59 return '<HttpError %s when requesting %s returned "%s">' % (
60 self.resp.status, self.uri, self._get_reason().strip())
61 else:
62 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
63
64 __str__ = __repr__
65
68 """The JSON returned could not be parsed."""
69 pass
70
73 """File type unknown or unexpected."""
74 pass
75
78 """Link type unknown or unexpected."""
79 pass
80
83 """No API with that name and version exists."""
84 pass
85
88 """That is an unacceptable mimetype for this operation."""
89 pass
90
95
98 """Error occured during resumable upload."""
99 pass
100
103 """The given chunksize is not valid."""
104 pass
105
107 """The channel Notification is invalid."""
108 pass
109
111 """Error occured during batch operations."""
112
113 @util.positional(2)
114 - def __init__(self, reason, resp=None, content=None):
115 self.resp = resp
116 self.content = content
117 self.reason = reason
118
120 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
121
122 __str__ = __repr__
123
126 """Exception raised by RequestMockBuilder on unexpected calls."""
127
128 @util.positional(1)
130 """Constructor for an UnexpectedMethodError."""
131 super(UnexpectedMethodError, self).__init__(
132 'Received unexpected call %s' % methodId)
133
134
135 -class UnexpectedBodyError(Error):
136 """Exception raised by RequestMockBuilder on unexpected bodies."""
137
138 - def __init__(self, expected, provided):
139 """Constructor for an UnexpectedMethodError."""
140 super(UnexpectedBodyError, self).__init__(
141 'Expected: [%s] - Provided: [%s]' % (expected, provided))
142