blob: e31ffe47b8f3d33c92e1dadaa388dd29c7c014f3 [file] [log] [blame]
Craig Citro751b7fb2014-09-23 11:20:38 -07001# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio20a5aa92011-04-01 17:44:25 -04002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050014
15"""Errors for the library.
16
17All exceptions defined by the library
18should be defined in this file.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
Craig Citro6ae34d72014-08-18 23:10:09 -070023import json
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050024
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040025from oauth2client import util
Ali Afshar2dcc6522010-12-16 10:11:53 +010026
27
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050028class Error(Exception):
29 """Base error for this module."""
30 pass
31
32
33class HttpError(Error):
34 """HTTP data was invalid or unexpected."""
35
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040036 @util.positional(3)
Joe Gregorio49396552011-03-08 10:39:00 -050037 def __init__(self, resp, content, uri=None):
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050038 self.resp = resp
Ali Afshar2dcc6522010-12-16 10:11:53 +010039 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050040 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050041
Ali Afshar2dcc6522010-12-16 10:11:53 +010042 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040043 """Calculate the reason for the error from the response content."""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040044 reason = self.resp.reason
45 try:
Craig Citro6ae34d72014-08-18 23:10:09 -070046 data = json.loads(self.content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040047 reason = data['error']['message']
48 except (ValueError, KeyError):
49 pass
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050050 if reason is None:
51 reason = ''
Ali Afshar2dcc6522010-12-16 10:11:53 +010052 return reason
53
54 def __repr__(self):
Joe Gregorio49396552011-03-08 10:39:00 -050055 if self.uri:
56 return '<HttpError %s when requesting %s returned "%s">' % (
Joe Gregorio3fb93672012-07-25 11:31:11 -040057 self.resp.status, self.uri, self._get_reason().strip())
Joe Gregorio49396552011-03-08 10:39:00 -050058 else:
59 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010060
61 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050062
63
Joe Gregorio49396552011-03-08 10:39:00 -050064class InvalidJsonError(Error):
65 """The JSON returned could not be parsed."""
66 pass
67
68
Joe Gregoriodc106fc2012-11-20 14:30:14 -050069class UnknownFileType(Error):
70 """File type unknown or unexpected."""
71 pass
72
73
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050074class UnknownLinkType(Error):
75 """Link type unknown or unexpected."""
76 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -040077
Joe Gregorio66f57522011-11-30 11:00:00 -050078
Joe Gregoriodae2f552011-11-21 08:16:56 -050079class UnknownApiNameOrVersion(Error):
Joe Gregorio8b4df3f2011-11-18 15:44:48 -050080 """No API with that name and version exists."""
81 pass
82
Joe Gregorioa388ce32011-09-09 17:19:13 -040083
Joe Gregoriofdf7c802011-06-30 12:33:38 -040084class UnacceptableMimeTypeError(Error):
85 """That is an unacceptable mimetype for this operation."""
86 pass
87
Joe Gregorioa388ce32011-09-09 17:19:13 -040088
Joe Gregoriofdf7c802011-06-30 12:33:38 -040089class MediaUploadSizeError(Error):
90 """Media is larger than the method can accept."""
91 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040092
93
Joe Gregoriobaf04802013-03-01 12:27:06 -050094class ResumableUploadError(HttpError):
Joe Gregoriod0bd3882011-11-22 09:49:47 -050095 """Error occured during resumable upload."""
96 pass
97
98
Joe Gregorioc80ac9d2012-08-21 14:09:09 -040099class InvalidChunkSizeError(Error):
100 """The given chunksize is not valid."""
101 pass
102
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400103class InvalidNotificationError(Error):
104 """The channel Notification is invalid."""
105 pass
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400106
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500107class BatchError(HttpError):
Joe Gregorio66f57522011-11-30 11:00:00 -0500108 """Error occured during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500109
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400110 @util.positional(2)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500111 def __init__(self, reason, resp=None, content=None):
112 self.resp = resp
113 self.content = content
114 self.reason = reason
115
116 def __repr__(self):
117 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
118
119 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500120
121
Joe Gregorioa388ce32011-09-09 17:19:13 -0400122class UnexpectedMethodError(Error):
123 """Exception raised by RequestMockBuilder on unexpected calls."""
124
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400125 @util.positional(1)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400126 def __init__(self, methodId=None):
127 """Constructor for an UnexpectedMethodError."""
128 super(UnexpectedMethodError, self).__init__(
129 'Received unexpected call %s' % methodId)
130
131
132class UnexpectedBodyError(Error):
133 """Exception raised by RequestMockBuilder on unexpected bodies."""
134
135 def __init__(self, expected, provided):
136 """Constructor for an UnexpectedMethodError."""
137 super(UnexpectedBodyError, self).__init__(
138 'Expected: [%s] - Provided: [%s]' % (expected, provided))