1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Errors for the library.
18
19 All exceptions defined by the library
20 should be defined in this file.
21 """
22
23 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
25
26 from oauth2client import util
27 from oauth2client.anyjson import simplejson
28
29
30 -class Error(Exception):
31 """Base error for this module."""
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 self.content = content
42 self.uri = uri
43
45 """Calculate the reason for the error from the response content."""
46 reason = self.resp.reason
47 try:
48 data = simplejson.loads(self.content)
49 reason = data['error']['message']
50 except (ValueError, KeyError):
51 pass
52 return reason
53
55 if self.uri:
56 return '<HttpError %s when requesting %s returned "%s">' % (
57 self.resp.status, self.uri, self._get_reason().strip())
58 else:
59 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
60
61 __str__ = __repr__
62
65 """The JSON returned could not be parsed."""
66 pass
67
70 """File type unknown or unexpected."""
71 pass
72
75 """Link type unknown or unexpected."""
76 pass
77
80 """No API with that name and version exists."""
81 pass
82
85 """That is an unacceptable mimetype for this operation."""
86 pass
87
92
95 """Error occured during resumable upload."""
96 pass
97
100 """The given chunksize is not valid."""
101 pass
102
105 """Error occured during batch operations."""
106
107 @util.positional(2)
108 - def __init__(self, reason, resp=None, content=None):
109 self.resp = resp
110 self.content = content
111 self.reason = reason
112
114 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
115
116 __str__ = __repr__
117
120 """Exception raised by RequestMockBuilder on unexpected calls."""
121
122 @util.positional(1)
124 """Constructor for an UnexpectedMethodError."""
125 super(UnexpectedMethodError, self).__init__(
126 'Received unexpected call %s' % methodId)
127
128
129 -class UnexpectedBodyError(Error):
130 """Exception raised by RequestMockBuilder on unexpected bodies."""
131
132 - def __init__(self, expected, provided):
133 """Constructor for an UnexpectedMethodError."""
134 super(UnexpectedBodyError, self).__init__(
135 'Expected: [%s] - Provided: [%s]' % (expected, provided))
136