blob: 6656bd1b63701f9cfbcaa64095be34a201e865a1 [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
Ali Afshar2dcc6522010-12-16 10:11:53 +010040 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050041 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050042
Ali Afshar2dcc6522010-12-16 10:11:53 +010043 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040044 """Calculate the reason for the error from the response content."""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040045 reason = self.resp.reason
46 try:
Craig Citro6ae34d72014-08-18 23:10:09 -070047 data = json.loads(self.content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040048 reason = data['error']['message']
49 except (ValueError, KeyError):
50 pass
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050051 if reason is None:
52 reason = ''
Ali Afshar2dcc6522010-12-16 10:11:53 +010053 return reason
54
55 def __repr__(self):
Joe Gregorio49396552011-03-08 10:39:00 -050056 if self.uri:
57 return '<HttpError %s when requesting %s returned "%s">' % (
Joe Gregorio3fb93672012-07-25 11:31:11 -040058 self.resp.status, self.uri, self._get_reason().strip())
Joe Gregorio49396552011-03-08 10:39:00 -050059 else:
60 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010061
62 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050063
64
Joe Gregorio49396552011-03-08 10:39:00 -050065class InvalidJsonError(Error):
66 """The JSON returned could not be parsed."""
67 pass
68
69
Joe Gregoriodc106fc2012-11-20 14:30:14 -050070class UnknownFileType(Error):
71 """File type unknown or unexpected."""
72 pass
73
74
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050075class UnknownLinkType(Error):
76 """Link type unknown or unexpected."""
77 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -040078
Joe Gregorio66f57522011-11-30 11:00:00 -050079
Joe Gregoriodae2f552011-11-21 08:16:56 -050080class UnknownApiNameOrVersion(Error):
Joe Gregorio8b4df3f2011-11-18 15:44:48 -050081 """No API with that name and version exists."""
82 pass
83
Joe Gregorioa388ce32011-09-09 17:19:13 -040084
Joe Gregoriofdf7c802011-06-30 12:33:38 -040085class UnacceptableMimeTypeError(Error):
86 """That is an unacceptable mimetype for this operation."""
87 pass
88
Joe Gregorioa388ce32011-09-09 17:19:13 -040089
Joe Gregoriofdf7c802011-06-30 12:33:38 -040090class MediaUploadSizeError(Error):
91 """Media is larger than the method can accept."""
92 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040093
94
Joe Gregoriobaf04802013-03-01 12:27:06 -050095class ResumableUploadError(HttpError):
Joe Gregoriod0bd3882011-11-22 09:49:47 -050096 """Error occured during resumable upload."""
97 pass
98
99
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400100class InvalidChunkSizeError(Error):
101 """The given chunksize is not valid."""
102 pass
103
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400104class InvalidNotificationError(Error):
105 """The channel Notification is invalid."""
106 pass
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400107
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500108class BatchError(HttpError):
Joe Gregorio66f57522011-11-30 11:00:00 -0500109 """Error occured during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500110
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400111 @util.positional(2)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500112 def __init__(self, reason, resp=None, content=None):
113 self.resp = resp
114 self.content = content
115 self.reason = reason
116
117 def __repr__(self):
118 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
119
120 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500121
122
Joe Gregorioa388ce32011-09-09 17:19:13 -0400123class UnexpectedMethodError(Error):
124 """Exception raised by RequestMockBuilder on unexpected calls."""
125
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400126 @util.positional(1)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400127 def __init__(self, methodId=None):
128 """Constructor for an UnexpectedMethodError."""
129 super(UnexpectedMethodError, self).__init__(
130 'Received unexpected call %s' % methodId)
131
132
133class 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))