| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 # Copyright 2014 Google Inc. All Rights Reserved. |
| 2 # | 2 # |
| 3 # Copyright (C) 2010 Google Inc. | |
| 4 # | |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 8 # | 6 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # | 8 # |
| 11 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. | 13 # limitations under the License. |
| 16 | 14 |
| 17 """Errors for the library. | 15 """Errors for the library. |
| 18 | 16 |
| 19 All exceptions defined by the library | 17 All exceptions defined by the library |
| 20 should be defined in this file. | 18 should be defined in this file. |
| 21 """ | 19 """ |
| 20 from __future__ import absolute_import |
| 22 | 21 |
| 23 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | 22 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | 23 |
| 24 import json |
| 25 | 25 |
| 26 from oauth2client.anyjson import simplejson | 26 from oauth2client import util |
| 27 | 27 |
| 28 | 28 |
| 29 class Error(Exception): | 29 class Error(Exception): |
| 30 """Base error for this module.""" | 30 """Base error for this module.""" |
| 31 pass | 31 pass |
| 32 | 32 |
| 33 | 33 |
| 34 class HttpError(Error): | 34 class HttpError(Error): |
| 35 """HTTP data was invalid or unexpected.""" | 35 """HTTP data was invalid or unexpected.""" |
| 36 | 36 |
| 37 @util.positional(3) |
| 37 def __init__(self, resp, content, uri=None): | 38 def __init__(self, resp, content, uri=None): |
| 38 self.resp = resp | 39 self.resp = resp |
| 40 if not isinstance(content, bytes): |
| 41 raise TypeError("HTTP content should be bytes") |
| 39 self.content = content | 42 self.content = content |
| 40 self.uri = uri | 43 self.uri = uri |
| 41 | 44 |
| 42 def _get_reason(self): | 45 def _get_reason(self): |
| 43 """Calculate the reason for the error from the response content.""" | 46 """Calculate the reason for the error from the response content.""" |
| 44 if self.resp.get('content-type', '').startswith('application/json'): | 47 reason = self.resp.reason |
| 45 try: | 48 try: |
| 46 data = simplejson.loads(self.content) | 49 data = json.loads(self.content.decode('utf-8')) |
| 47 reason = data['error']['message'] | 50 reason = data['error']['message'] |
| 48 except (ValueError, KeyError): | 51 except (ValueError, KeyError): |
| 49 reason = self.content | 52 pass |
| 50 else: | 53 if reason is None: |
| 51 reason = self.resp.reason | 54 reason = '' |
| 52 return reason | 55 return reason |
| 53 | 56 |
| 54 def __repr__(self): | 57 def __repr__(self): |
| 55 if self.uri: | 58 if self.uri: |
| 56 return '<HttpError %s when requesting %s returned "%s">' % ( | 59 return '<HttpError %s when requesting %s returned "%s">' % ( |
| 57 self.resp.status, self.uri, self._get_reason()) | 60 self.resp.status, self.uri, self._get_reason().strip()) |
| 58 else: | 61 else: |
| 59 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason()) | 62 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason()) |
| 60 | 63 |
| 61 __str__ = __repr__ | 64 __str__ = __repr__ |
| 62 | 65 |
| 63 | 66 |
| 64 class InvalidJsonError(Error): | 67 class InvalidJsonError(Error): |
| 65 """The JSON returned could not be parsed.""" | 68 """The JSON returned could not be parsed.""" |
| 66 pass | 69 pass |
| 67 | 70 |
| 68 | 71 |
| 72 class UnknownFileType(Error): |
| 73 """File type unknown or unexpected.""" |
| 74 pass |
| 75 |
| 76 |
| 69 class UnknownLinkType(Error): | 77 class UnknownLinkType(Error): |
| 70 """Link type unknown or unexpected.""" | 78 """Link type unknown or unexpected.""" |
| 71 pass | 79 pass |
| 72 | 80 |
| 73 | 81 |
| 74 class UnknownApiNameOrVersion(Error): | 82 class UnknownApiNameOrVersion(Error): |
| 75 """No API with that name and version exists.""" | 83 """No API with that name and version exists.""" |
| 76 pass | 84 pass |
| 77 | 85 |
| 78 | 86 |
| 79 class UnacceptableMimeTypeError(Error): | 87 class UnacceptableMimeTypeError(Error): |
| 80 """That is an unacceptable mimetype for this operation.""" | 88 """That is an unacceptable mimetype for this operation.""" |
| 81 pass | 89 pass |
| 82 | 90 |
| 83 | 91 |
| 84 class MediaUploadSizeError(Error): | 92 class MediaUploadSizeError(Error): |
| 85 """Media is larger than the method can accept.""" | 93 """Media is larger than the method can accept.""" |
| 86 pass | 94 pass |
| 87 | 95 |
| 88 | 96 |
| 89 class ResumableUploadError(Error): | 97 class ResumableUploadError(HttpError): |
| 90 """Error occured during resumable upload.""" | 98 """Error occured during resumable upload.""" |
| 91 pass | 99 pass |
| 92 | 100 |
| 93 | 101 |
| 102 class InvalidChunkSizeError(Error): |
| 103 """The given chunksize is not valid.""" |
| 104 pass |
| 105 |
| 106 class InvalidNotificationError(Error): |
| 107 """The channel Notification is invalid.""" |
| 108 pass |
| 109 |
| 94 class BatchError(HttpError): | 110 class BatchError(HttpError): |
| 95 """Error occured during batch operations.""" | 111 """Error occured during batch operations.""" |
| 96 | 112 |
| 113 @util.positional(2) |
| 97 def __init__(self, reason, resp=None, content=None): | 114 def __init__(self, reason, resp=None, content=None): |
| 98 self.resp = resp | 115 self.resp = resp |
| 99 self.content = content | 116 self.content = content |
| 100 self.reason = reason | 117 self.reason = reason |
| 101 | 118 |
| 102 def __repr__(self): | 119 def __repr__(self): |
| 103 return '<BatchError %s "%s">' % (self.resp.status, self.reason) | 120 return '<BatchError %s "%s">' % (self.resp.status, self.reason) |
| 104 | 121 |
| 105 __str__ = __repr__ | 122 __str__ = __repr__ |
| 106 | 123 |
| 107 | 124 |
| 108 class UnexpectedMethodError(Error): | 125 class UnexpectedMethodError(Error): |
| 109 """Exception raised by RequestMockBuilder on unexpected calls.""" | 126 """Exception raised by RequestMockBuilder on unexpected calls.""" |
| 110 | 127 |
| 128 @util.positional(1) |
| 111 def __init__(self, methodId=None): | 129 def __init__(self, methodId=None): |
| 112 """Constructor for an UnexpectedMethodError.""" | 130 """Constructor for an UnexpectedMethodError.""" |
| 113 super(UnexpectedMethodError, self).__init__( | 131 super(UnexpectedMethodError, self).__init__( |
| 114 'Received unexpected call %s' % methodId) | 132 'Received unexpected call %s' % methodId) |
| 115 | 133 |
| 116 | 134 |
| 117 class UnexpectedBodyError(Error): | 135 class UnexpectedBodyError(Error): |
| 118 """Exception raised by RequestMockBuilder on unexpected bodies.""" | 136 """Exception raised by RequestMockBuilder on unexpected bodies.""" |
| 119 | 137 |
| 120 def __init__(self, expected, provided): | 138 def __init__(self, expected, provided): |
| 121 """Constructor for an UnexpectedMethodError.""" | 139 """Constructor for an UnexpectedMethodError.""" |
| 122 super(UnexpectedBodyError, self).__init__( | 140 super(UnexpectedBodyError, self).__init__( |
| 123 'Expected: [%s] - Provided: [%s]' % (expected, provided)) | 141 'Expected: [%s] - Provided: [%s]' % (expected, provided)) |
| OLD | NEW |