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