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