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 self.content = content
41 self.uri = uri
42
44 """Calculate the reason for the error from the response content."""
45 reason = self.resp.reason
46 try:
47 data = json.loads(self.content)
48 reason = data['error']['message']
49 except (ValueError, KeyError):
50 pass
51 if reason is None:
52 reason = ''
53 return reason
54
56 if self.uri:
57 return '<HttpError %s when requesting %s returned "%s">' % (
58 self.resp.status, self.uri, self._get_reason().strip())
59 else:
60 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
61
62 __str__ = __repr__
63
66 """The JSON returned could not be parsed."""
67 pass
68
71 """File type unknown or unexpected."""
72 pass
73
76 """Link type unknown or unexpected."""
77 pass
78
81 """No API with that name and version exists."""
82 pass
83
86 """That is an unacceptable mimetype for this operation."""
87 pass
88
93
96 """Error occured during resumable upload."""
97 pass
98
101 """The given chunksize is not valid."""
102 pass
103
105 """The channel Notification is invalid."""
106 pass
107
109 """Error occured during batch operations."""
110
111 @util.positional(2)
112 - def __init__(self, reason, resp=None, content=None):
113 self.resp = resp
114 self.content = content
115 self.reason = reason
116
118 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
119
120 __str__ = __repr__
121
124 """Exception raised by RequestMockBuilder on unexpected calls."""
125
126 @util.positional(1)
128 """Constructor for an UnexpectedMethodError."""
129 super(UnexpectedMethodError, self).__init__(
130 'Received unexpected call %s' % methodId)
131
132
133 -class UnexpectedBodyError(Error):
134 """Exception raised by RequestMockBuilder on unexpected bodies."""
135
136 - def __init__(self, expected, provided):
137 """Constructor for an UnexpectedMethodError."""
138 super(UnexpectedBodyError, self).__init__(
139 'Expected: [%s] - Provided: [%s]' % (expected, provided))
140