blob: 332327ec04ff8a2faf5137a208ff018b48cdbb10 [file] [log] [blame]
Craig Citro751b7fb2014-09-23 11:20:38 -07001# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregorio20a5aa92011-04-01 17:44:25 -04002#
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 Gregorio3ad5e9a2010-12-09 15:01:04 -050014
15"""Errors for the library.
16
17All exceptions defined by the library
18should be defined in this file.
19"""
INADA Naokie4ea1a92015-03-04 03:45:42 +090020from __future__ import absolute_import
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050021
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070022__author__ = "jcgregorio@google.com (Joe Gregorio)"
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050023
Craig Citro6ae34d72014-08-18 23:10:09 -070024import json
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050025
Helen Koikede13e3b2018-04-26 16:05:16 -030026from googleapiclient import _helpers as util
Ali Afshar2dcc6522010-12-16 10:11:53 +010027
28
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050029class Error(Exception):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070030 """Base error for this module."""
31
32 pass
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050033
34
35class HttpError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070036 """HTTP data was invalid or unexpected."""
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050037
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070038 @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 Marquardtdb2a7662021-03-17 16:02:04 -030046 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 Gregorio3ad5e9a2010-12-09 15:01:04 -050052
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070053 def _get_reason(self):
54 """Calculate the reason for the error from the response content."""
55 reason = self.resp.reason
56 try:
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -050057 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 Kim66bb32c2019-10-30 10:11:58 -070062 if isinstance(data, dict):
63 reason = data["error"]["message"]
vinay-googlea5d20812021-04-03 03:00:05 -070064 error_detail_keyword = next((kw for kw in ["detail", "details", "errors", "message"] if kw in data["error"]), "")
Muad Mohameda341c5a2020-11-08 20:30:02 +000065 if error_detail_keyword:
66 self.error_details = data["error"][error_detail_keyword]
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070067 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 Parthenioue6a1da32020-12-09 17:00:03 -050072 else:
73 self.error_details = data
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070074 except (ValueError, KeyError, TypeError):
75 pass
76 if reason is None:
77 reason = ""
78 return reason
Ali Afshar2dcc6522010-12-16 10:11:53 +010079
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070080 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 Afshar2dcc6522010-12-16 10:11:53 +010097
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070098 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050099
100
Joe Gregorio49396552011-03-08 10:39:00 -0500101class InvalidJsonError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700102 """The JSON returned could not be parsed."""
103
104 pass
Joe Gregorio49396552011-03-08 10:39:00 -0500105
106
Joe Gregoriodc106fc2012-11-20 14:30:14 -0500107class UnknownFileType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700108 """File type unknown or unexpected."""
109
110 pass
Joe Gregoriodc106fc2012-11-20 14:30:14 -0500111
112
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -0500113class UnknownLinkType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700114 """Link type unknown or unexpected."""
115
116 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400117
Joe Gregorio66f57522011-11-30 11:00:00 -0500118
Joe Gregoriodae2f552011-11-21 08:16:56 -0500119class UnknownApiNameOrVersion(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700120 """No API with that name and version exists."""
121
122 pass
Joe Gregorio8b4df3f2011-11-18 15:44:48 -0500123
Joe Gregorioa388ce32011-09-09 17:19:13 -0400124
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400125class UnacceptableMimeTypeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700126 """That is an unacceptable mimetype for this operation."""
127
128 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400129
Joe Gregorioa388ce32011-09-09 17:19:13 -0400130
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400131class MediaUploadSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700132 """Media is larger than the method can accept."""
133
134 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400135
136
Joe Gregoriobaf04802013-03-01 12:27:06 -0500137class ResumableUploadError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000138 """Error occurred during resumable upload."""
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700139
140 pass
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500141
142
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400143class InvalidChunkSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700144 """The given chunksize is not valid."""
145
146 pass
147
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400148
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400149class InvalidNotificationError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700150 """The channel Notification is invalid."""
151
152 pass
153
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400154
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500155class BatchError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000156 """Error occurred during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500157
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700158 @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 Gregorio5d1171b2012-01-05 10:48:24 -0500163
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700164 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 Gregorio5d1171b2012-01-05 10:48:24 -0500169
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700170 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500171
172
Joe Gregorioa388ce32011-09-09 17:19:13 -0400173class UnexpectedMethodError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700174 """Exception raised by RequestMockBuilder on unexpected calls."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400175
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700176 @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 Gregorioa388ce32011-09-09 17:19:13 -0400182
183
184class UnexpectedBodyError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700185 """Exception raised by RequestMockBuilder on unexpected bodies."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400186
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700187 def __init__(self, expected, provided):
188 """Constructor for an UnexpectedMethodError."""
189 super(UnexpectedBodyError, self).__init__(
190 "Expected: [%s] - Provided: [%s]" % (expected, provided)
191 )