blob: 3d44de7006254ed3663e21b527d2eec9f79ee656 [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"""
INADA Naokie4ea1a92015-03-04 03:45:42 +090020from __future__ import absolute_import
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050021
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
Craig Citro6ae34d72014-08-18 23:10:09 -070024import json
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050025
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040026from oauth2client import util
Ali Afshar2dcc6522010-12-16 10:11:53 +010027
28
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050029class Error(Exception):
30 """Base error for this module."""
31 pass
32
33
34class HttpError(Error):
35 """HTTP data was invalid or unexpected."""
36
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040037 @util.positional(3)
Joe Gregorio49396552011-03-08 10:39:00 -050038 def __init__(self, resp, content, uri=None):
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050039 self.resp = resp
INADA Naoki09157612015-03-25 01:51:03 +090040 if not isinstance(content, bytes):
41 raise TypeError("HTTP content should be bytes")
Ali Afshar2dcc6522010-12-16 10:11:53 +010042 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050043 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050044
Ali Afshar2dcc6522010-12-16 10:11:53 +010045 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040046 """Calculate the reason for the error from the response content."""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040047 reason = self.resp.reason
48 try:
INADA Naoki09157612015-03-25 01:51:03 +090049 data = json.loads(self.content.decode('utf-8'))
Joe Gregorio20b54fb2012-07-26 09:59:35 -040050 reason = data['error']['message']
51 except (ValueError, KeyError):
52 pass
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050053 if reason is None:
54 reason = ''
Ali Afshar2dcc6522010-12-16 10:11:53 +010055 return reason
56
57 def __repr__(self):
Joe Gregorio49396552011-03-08 10:39:00 -050058 if self.uri:
59 return '<HttpError %s when requesting %s returned "%s">' % (
Joe Gregorio3fb93672012-07-25 11:31:11 -040060 self.resp.status, self.uri, self._get_reason().strip())
Joe Gregorio49396552011-03-08 10:39:00 -050061 else:
62 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010063
64 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050065
66
Joe Gregorio49396552011-03-08 10:39:00 -050067class InvalidJsonError(Error):
68 """The JSON returned could not be parsed."""
69 pass
70
71
Joe Gregoriodc106fc2012-11-20 14:30:14 -050072class UnknownFileType(Error):
73 """File type unknown or unexpected."""
74 pass
75
76
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050077class UnknownLinkType(Error):
78 """Link type unknown or unexpected."""
79 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -040080
Joe Gregorio66f57522011-11-30 11:00:00 -050081
Joe Gregoriodae2f552011-11-21 08:16:56 -050082class UnknownApiNameOrVersion(Error):
Joe Gregorio8b4df3f2011-11-18 15:44:48 -050083 """No API with that name and version exists."""
84 pass
85
Joe Gregorioa388ce32011-09-09 17:19:13 -040086
Joe Gregoriofdf7c802011-06-30 12:33:38 -040087class UnacceptableMimeTypeError(Error):
88 """That is an unacceptable mimetype for this operation."""
89 pass
90
Joe Gregorioa388ce32011-09-09 17:19:13 -040091
Joe Gregoriofdf7c802011-06-30 12:33:38 -040092class MediaUploadSizeError(Error):
93 """Media is larger than the method can accept."""
94 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040095
96
Joe Gregoriobaf04802013-03-01 12:27:06 -050097class ResumableUploadError(HttpError):
Joe Gregoriod0bd3882011-11-22 09:49:47 -050098 """Error occured during resumable upload."""
99 pass
100
101
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400102class InvalidChunkSizeError(Error):
103 """The given chunksize is not valid."""
104 pass
105
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400106class InvalidNotificationError(Error):
107 """The channel Notification is invalid."""
108 pass
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400109
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500110class BatchError(HttpError):
Joe Gregorio66f57522011-11-30 11:00:00 -0500111 """Error occured during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500112
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400113 @util.positional(2)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500114 def __init__(self, reason, resp=None, content=None):
115 self.resp = resp
116 self.content = content
117 self.reason = reason
118
119 def __repr__(self):
120 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
121
122 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500123
124
Joe Gregorioa388ce32011-09-09 17:19:13 -0400125class UnexpectedMethodError(Error):
126 """Exception raised by RequestMockBuilder on unexpected calls."""
127
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400128 @util.positional(1)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400129 def __init__(self, methodId=None):
130 """Constructor for an UnexpectedMethodError."""
131 super(UnexpectedMethodError, self).__init__(
132 'Received unexpected call %s' % methodId)
133
134
135class 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))