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 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 22 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 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 | |
Helen Koike | de13e3b | 2018-04-26 16:05:16 -0300 | [diff] [blame] | 26 | from googleapiclient import _helpers as util |
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): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 30 | """Base error for this module.""" |
| 31 | |
| 32 | pass |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class HttpError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 36 | """HTTP data was invalid or unexpected.""" |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 37 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 38 | @util.positional(3) |
| 39 | def __init__(self, resp, content, uri=None): |
| 40 | self.resp = resp |
| 41 | if not isinstance(content, bytes): |
| 42 | raise TypeError("HTTP content should be bytes") |
| 43 | self.content = content |
| 44 | self.uri = uri |
| 45 | self.error_details = "" |
William Marquardt | db2a766 | 2021-03-17 16:02:04 -0300 | [diff] [blame] | 46 | self._get_reason() |
| 47 | |
| 48 | @property |
| 49 | def status_code(self): |
| 50 | """Return the HTTP status code from the response content.""" |
| 51 | return self.resp.status |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 52 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 53 | def _get_reason(self): |
| 54 | """Calculate the reason for the error from the response content.""" |
| 55 | reason = self.resp.reason |
| 56 | try: |
Anthonios Partheniou | e6a1da3 | 2020-12-09 17:00:03 -0500 | [diff] [blame] | 57 | try: |
| 58 | data = json.loads(self.content.decode("utf-8")) |
| 59 | except json.JSONDecodeError: |
| 60 | # In case it is not json |
| 61 | data = self.content.decode("utf-8") |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 62 | if isinstance(data, dict): |
| 63 | reason = data["error"]["message"] |
vinay-google | a5d2081 | 2021-04-03 03:00:05 -0700 | [diff] [blame] | 64 | error_detail_keyword = next((kw for kw in ["detail", "details", "errors", "message"] if kw in data["error"]), "") |
Muad Mohamed | a341c5a | 2020-11-08 20:30:02 +0000 | [diff] [blame] | 65 | if error_detail_keyword: |
| 66 | self.error_details = data["error"][error_detail_keyword] |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 67 | elif isinstance(data, list) and len(data) > 0: |
| 68 | first_error = data[0] |
| 69 | reason = first_error["error"]["message"] |
| 70 | if "details" in first_error["error"]: |
| 71 | self.error_details = first_error["error"]["details"] |
Anthonios Partheniou | e6a1da3 | 2020-12-09 17:00:03 -0500 | [diff] [blame] | 72 | else: |
| 73 | self.error_details = data |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 74 | except (ValueError, KeyError, TypeError): |
| 75 | pass |
| 76 | if reason is None: |
| 77 | reason = "" |
| 78 | return reason |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 79 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 80 | def __repr__(self): |
| 81 | reason = self._get_reason() |
| 82 | if self.error_details: |
| 83 | return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % ( |
| 84 | self.resp.status, |
| 85 | self.uri, |
| 86 | reason.strip(), |
| 87 | self.error_details, |
| 88 | ) |
| 89 | elif self.uri: |
| 90 | return '<HttpError %s when requesting %s returned "%s">' % ( |
| 91 | self.resp.status, |
| 92 | self.uri, |
| 93 | self._get_reason().strip(), |
| 94 | ) |
| 95 | else: |
| 96 | return '<HttpError %s "%s">' % (self.resp.status, self._get_reason()) |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 97 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 98 | __str__ = __repr__ |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 99 | |
| 100 | |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 101 | class InvalidJsonError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 102 | """The JSON returned could not be parsed.""" |
| 103 | |
| 104 | pass |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 105 | |
| 106 | |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 107 | class UnknownFileType(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 108 | """File type unknown or unexpected.""" |
| 109 | |
| 110 | pass |
Joe Gregorio | dc106fc | 2012-11-20 14:30:14 -0500 | [diff] [blame] | 111 | |
| 112 | |
Joe Gregorio | 3ad5e9a | 2010-12-09 15:01:04 -0500 | [diff] [blame] | 113 | class UnknownLinkType(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 114 | """Link type unknown or unexpected.""" |
| 115 | |
| 116 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 117 | |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 118 | |
Joe Gregorio | dae2f55 | 2011-11-21 08:16:56 -0500 | [diff] [blame] | 119 | class UnknownApiNameOrVersion(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 120 | """No API with that name and version exists.""" |
| 121 | |
| 122 | pass |
Joe Gregorio | 8b4df3f | 2011-11-18 15:44:48 -0500 | [diff] [blame] | 123 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 124 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 125 | class UnacceptableMimeTypeError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 126 | """That is an unacceptable mimetype for this operation.""" |
| 127 | |
| 128 | pass |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 129 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 130 | |
Joe Gregorio | fdf7c80 | 2011-06-30 12:33:38 -0400 | [diff] [blame] | 131 | class MediaUploadSizeError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 132 | """Media is larger than the method can accept.""" |
| 133 | |
| 134 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 135 | |
| 136 | |
Joe Gregorio | baf0480 | 2013-03-01 12:27:06 -0500 | [diff] [blame] | 137 | class ResumableUploadError(HttpError): |
Tim Gates | 43fc0cf | 2020-04-21 08:03:25 +1000 | [diff] [blame] | 138 | """Error occurred during resumable upload.""" |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 139 | |
| 140 | pass |
Joe Gregorio | d0bd388 | 2011-11-22 09:49:47 -0500 | [diff] [blame] | 141 | |
| 142 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 143 | class InvalidChunkSizeError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 144 | """The given chunksize is not valid.""" |
| 145 | |
| 146 | pass |
| 147 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 148 | |
Joe Gregorio | 1a5e30e | 2013-06-25 15:35:47 -0400 | [diff] [blame] | 149 | class InvalidNotificationError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 150 | """The channel Notification is invalid.""" |
| 151 | |
| 152 | pass |
| 153 | |
Joe Gregorio | c80ac9d | 2012-08-21 14:09:09 -0400 | [diff] [blame] | 154 | |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 155 | class BatchError(HttpError): |
Tim Gates | 43fc0cf | 2020-04-21 08:03:25 +1000 | [diff] [blame] | 156 | """Error occurred during batch operations.""" |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 157 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 158 | @util.positional(2) |
| 159 | def __init__(self, reason, resp=None, content=None): |
| 160 | self.resp = resp |
| 161 | self.content = content |
| 162 | self.reason = reason |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 163 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 164 | def __repr__(self): |
| 165 | if getattr(self.resp, "status", None) is None: |
| 166 | return '<BatchError "%s">' % (self.reason) |
| 167 | else: |
| 168 | return '<BatchError %s "%s">' % (self.resp.status, self.reason) |
Joe Gregorio | 5d1171b | 2012-01-05 10:48:24 -0500 | [diff] [blame] | 169 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 170 | __str__ = __repr__ |
Joe Gregorio | 66f5752 | 2011-11-30 11:00:00 -0500 | [diff] [blame] | 171 | |
| 172 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 173 | class UnexpectedMethodError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 174 | """Exception raised by RequestMockBuilder on unexpected calls.""" |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 175 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 176 | @util.positional(1) |
| 177 | def __init__(self, methodId=None): |
| 178 | """Constructor for an UnexpectedMethodError.""" |
| 179 | super(UnexpectedMethodError, self).__init__( |
| 180 | "Received unexpected call %s" % methodId |
| 181 | ) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 182 | |
| 183 | |
| 184 | class UnexpectedBodyError(Error): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 185 | """Exception raised by RequestMockBuilder on unexpected bodies.""" |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 186 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 187 | def __init__(self, expected, provided): |
| 188 | """Constructor for an UnexpectedMethodError.""" |
| 189 | super(UnexpectedBodyError, self).__init__( |
| 190 | "Expected: [%s] - Provided: [%s]" % (expected, provided) |
| 191 | ) |