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