blob: 7163645ef70c5c62271e9d74f35523693131562c [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 = ""
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050046
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070047 def _get_reason(self):
48 """Calculate the reason for the error from the response content."""
49 reason = self.resp.reason
50 try:
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -050051 try:
52 data = json.loads(self.content.decode("utf-8"))
53 except json.JSONDecodeError:
54 # In case it is not json
55 data = self.content.decode("utf-8")
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070056 if isinstance(data, dict):
57 reason = data["error"]["message"]
Muad Mohameda341c5a2020-11-08 20:30:02 +000058 error_detail_keyword = next((kw for kw in ["detail", "details", "message"] if kw in data["error"]), "")
59 if error_detail_keyword:
60 self.error_details = data["error"][error_detail_keyword]
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070061 elif isinstance(data, list) and len(data) > 0:
62 first_error = data[0]
63 reason = first_error["error"]["message"]
64 if "details" in first_error["error"]:
65 self.error_details = first_error["error"]["details"]
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -050066 else:
67 self.error_details = data
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070068 except (ValueError, KeyError, TypeError):
69 pass
70 if reason is None:
71 reason = ""
72 return reason
Ali Afshar2dcc6522010-12-16 10:11:53 +010073
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070074 def __repr__(self):
75 reason = self._get_reason()
76 if self.error_details:
77 return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % (
78 self.resp.status,
79 self.uri,
80 reason.strip(),
81 self.error_details,
82 )
83 elif self.uri:
84 return '<HttpError %s when requesting %s returned "%s">' % (
85 self.resp.status,
86 self.uri,
87 self._get_reason().strip(),
88 )
89 else:
90 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010091
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070092 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050093
94
Joe Gregorio49396552011-03-08 10:39:00 -050095class InvalidJsonError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070096 """The JSON returned could not be parsed."""
97
98 pass
Joe Gregorio49396552011-03-08 10:39:00 -050099
100
Joe Gregoriodc106fc2012-11-20 14:30:14 -0500101class UnknownFileType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700102 """File type unknown or unexpected."""
103
104 pass
Joe Gregoriodc106fc2012-11-20 14:30:14 -0500105
106
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -0500107class UnknownLinkType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700108 """Link type unknown or unexpected."""
109
110 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400111
Joe Gregorio66f57522011-11-30 11:00:00 -0500112
Joe Gregoriodae2f552011-11-21 08:16:56 -0500113class UnknownApiNameOrVersion(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700114 """No API with that name and version exists."""
115
116 pass
Joe Gregorio8b4df3f2011-11-18 15:44:48 -0500117
Joe Gregorioa388ce32011-09-09 17:19:13 -0400118
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400119class UnacceptableMimeTypeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700120 """That is an unacceptable mimetype for this operation."""
121
122 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400123
Joe Gregorioa388ce32011-09-09 17:19:13 -0400124
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400125class MediaUploadSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700126 """Media is larger than the method can accept."""
127
128 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400129
130
Joe Gregoriobaf04802013-03-01 12:27:06 -0500131class ResumableUploadError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000132 """Error occurred during resumable upload."""
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700133
134 pass
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500135
136
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400137class InvalidChunkSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700138 """The given chunksize is not valid."""
139
140 pass
141
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400142
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400143class InvalidNotificationError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700144 """The channel Notification is invalid."""
145
146 pass
147
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400148
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500149class BatchError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000150 """Error occurred during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500151
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700152 @util.positional(2)
153 def __init__(self, reason, resp=None, content=None):
154 self.resp = resp
155 self.content = content
156 self.reason = reason
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500157
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700158 def __repr__(self):
159 if getattr(self.resp, "status", None) is None:
160 return '<BatchError "%s">' % (self.reason)
161 else:
162 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500163
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700164 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500165
166
Joe Gregorioa388ce32011-09-09 17:19:13 -0400167class UnexpectedMethodError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700168 """Exception raised by RequestMockBuilder on unexpected calls."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400169
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700170 @util.positional(1)
171 def __init__(self, methodId=None):
172 """Constructor for an UnexpectedMethodError."""
173 super(UnexpectedMethodError, self).__init__(
174 "Received unexpected call %s" % methodId
175 )
Joe Gregorioa388ce32011-09-09 17:19:13 -0400176
177
178class UnexpectedBodyError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700179 """Exception raised by RequestMockBuilder on unexpected bodies."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400180
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700181 def __init__(self, expected, provided):
182 """Constructor for an UnexpectedMethodError."""
183 super(UnexpectedBodyError, self).__init__(
184 "Expected: [%s] - Provided: [%s]" % (expected, provided)
185 )