Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 3 | # 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 Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 16 | |
| 17 | """Errors for the library. |
| 18 | |
| 19 | All exceptions defined by the library |
| 20 | should be defined in this file. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
| 25 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 26 | from oauth2client import util |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 27 | from oauth2client.anyjson import simplejson |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 28 | |
| 29 | |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 30 | class Error(Exception): |
| 31 | """Base error for this module.""" |
| 32 | pass |
| 33 | |
| 34 | |
| 35 | class HttpError(Error): |
| 36 | """HTTP data was invalid or unexpected.""" |
| 37 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 38 | @util.positional(3) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 39 | def __init__(self, resp, content, uri=None): |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 40 | self.resp = resp |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 41 | self.content = content |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 42 | self.uri = uri |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 43 | |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 44 | def _get_reason(self): |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 45 | """Calculate the reason for the error from the response content.""" |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 46 | reason = self.resp.reason |
| 47 | try: |
| 48 | data = simplejson.loads(self.content) |
| 49 | reason = data['error']['message'] |
| 50 | except (ValueError, KeyError): |
| 51 | pass |
Joe Gregorio | e7bbbb9 | 2013-02-20 15:37:24 -0500 | [diff] [blame] | 52 | if reason is None: |
| 53 | reason = '' |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 54 | return reason |
| 55 | |
| 56 | def __repr__(self): |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 57 | if self.uri: |
| 58 | return '<HttpError %s when requesting %s returned "%s">' % ( |
Joe Gregorio | 3fb9367 | 2012-07-25 11:31:11 -0400 | [diff] [blame] | 59 | self.resp.status, self.uri, self._get_reason().strip()) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 60 | else: |
| 61 | return '<HttpError %s "%s">' % (self.resp.status, self._get_reason()) |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 62 | |
| 63 | __str__ = __repr__ |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 64 | |
| 65 | |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 66 | class InvalidJsonError(Error): |
| 67 | """The JSON returned could not be parsed.""" |
| 68 | pass |
| 69 | |
| 70 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 71 | class UnknownFileType(Error): |
| 72 | """File type unknown or unexpected.""" |
| 73 | pass |
| 74 | |
| 75 | |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 76 | class UnknownLinkType(Error): |
| 77 | """Link type unknown or unexpected.""" |
| 78 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 79 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 80 | |
Joe Gregorio | dae2f55 | 2011-11-21 08:16:56 -0500 | [diff] [blame] | 81 | class UnknownApiNameOrVersion(Error): |
Joe Gregorio | 8b4df3f | 2011-11-18 15:44:48 -0500 | [diff] [blame] | 82 | """No API with that name and version exists.""" |
| 83 | pass |
| 84 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 85 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 86 | class UnacceptableMimeTypeError(Error): |
| 87 | """That is an unacceptable mimetype for this operation.""" |
| 88 | pass |
| 89 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 90 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 91 | class MediaUploadSizeError(Error): |
| 92 | """Media is larger than the method can accept.""" |
| 93 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 94 | |
| 95 | |
Joe Gregorio | baf0480 | 2013-03-01 12:27:06 -0500 | [diff] [blame] | 96 | class ResumableUploadError(HttpError): |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 97 | """Error occured during resumable upload.""" |
| 98 | pass |
| 99 | |
| 100 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 101 | class InvalidChunkSizeError(Error): |
| 102 | """The given chunksize is not valid.""" |
| 103 | pass |
| 104 | |
Joe Gregorio | 1a5e30e | 2013-06-25 15:35:47 -0400 | [diff] [blame^] | 105 | class InvalidNotificationError(Error): |
| 106 | """The channel Notification is invalid.""" |
| 107 | pass |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 108 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 109 | class BatchError(HttpError): |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 110 | """Error occured during batch operations.""" |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 111 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 112 | @util.positional(2) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 113 | def __init__(self, reason, resp=None, content=None): |
| 114 | self.resp = resp |
| 115 | self.content = content |
| 116 | self.reason = reason |
| 117 | |
| 118 | def __repr__(self): |
| 119 | return '<BatchError %s "%s">' % (self.resp.status, self.reason) |
| 120 | |
| 121 | __str__ = __repr__ |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 122 | |
| 123 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 124 | class UnexpectedMethodError(Error): |
| 125 | """Exception raised by RequestMockBuilder on unexpected calls.""" |
| 126 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 127 | @util.positional(1) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 128 | def __init__(self, methodId=None): |
| 129 | """Constructor for an UnexpectedMethodError.""" |
| 130 | super(UnexpectedMethodError, self).__init__( |
| 131 | 'Received unexpected call %s' % methodId) |
| 132 | |
| 133 | |
| 134 | class UnexpectedBodyError(Error): |
| 135 | """Exception raised by RequestMockBuilder on unexpected bodies.""" |
| 136 | |
| 137 | def __init__(self, expected, provided): |
| 138 | """Constructor for an UnexpectedMethodError.""" |
| 139 | super(UnexpectedBodyError, self).__init__( |
| 140 | 'Expected: [%s] - Provided: [%s]' % (expected, provided)) |