blob: 2e60515a10a7e9ee463172359ea9fdd51f2bc5ba [file] [log] [blame]
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -05001#!/usr/bin/python2.4
2#
Joe Gregorio20a5aa92011-04-01 17:44:25 -04003# Copyright (C) 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050016
17"""Errors for the library.
18
19All exceptions defined by the library
20should be defined in this file.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
25
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040026from oauth2client import util
Joe Gregorio549230c2012-01-11 10:38:05 -050027from oauth2client.anyjson import simplejson
Ali Afshar2dcc6522010-12-16 10:11:53 +010028
29
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050030class Error(Exception):
31 """Base error for this module."""
32 pass
33
34
35class HttpError(Error):
36 """HTTP data was invalid or unexpected."""
37
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040038 @util.positional(3)
Joe Gregorio49396552011-03-08 10:39:00 -050039 def __init__(self, resp, content, uri=None):
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050040 self.resp = resp
Ali Afshar2dcc6522010-12-16 10:11:53 +010041 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050042 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050043
Ali Afshar2dcc6522010-12-16 10:11:53 +010044 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040045 """Calculate the reason for the error from the response content."""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040046 reason = self.resp.reason
47 try:
48 data = simplejson.loads(self.content)
49 reason = data['error']['message']
50 except (ValueError, KeyError):
51 pass
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 Gregoriod0bd3882011-11-22 09:49:47 -050094class ResumableUploadError(Error):
95 """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
103
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500104class BatchError(HttpError):
Joe Gregorio66f57522011-11-30 11:00:00 -0500105 """Error occured during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500106
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400107 @util.positional(2)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500108 def __init__(self, reason, resp=None, content=None):
109 self.resp = resp
110 self.content = content
111 self.reason = reason
112
113 def __repr__(self):
114 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
115
116 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500117
118
Joe Gregorioa388ce32011-09-09 17:19:13 -0400119class UnexpectedMethodError(Error):
120 """Exception raised by RequestMockBuilder on unexpected calls."""
121
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400122 @util.positional(1)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400123 def __init__(self, methodId=None):
124 """Constructor for an UnexpectedMethodError."""
125 super(UnexpectedMethodError, self).__init__(
126 'Received unexpected call %s' % methodId)
127
128
129class 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))