blob: 2f7b112f0d83ccf987d1f03c045cca1f4e16cf95 [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:
51 data = json.loads(self.content.decode("utf-8"))
52 if isinstance(data, dict):
53 reason = data["error"]["message"]
54 if "details" in data["error"]:
55 self.error_details = data["error"]["details"]
56 elif "detail" in data["error"]:
57 self.error_details = data["error"]["detail"]
58 elif isinstance(data, list) and len(data) > 0:
59 first_error = data[0]
60 reason = first_error["error"]["message"]
61 if "details" in first_error["error"]:
62 self.error_details = first_error["error"]["details"]
63 except (ValueError, KeyError, TypeError):
64 pass
65 if reason is None:
66 reason = ""
67 return reason
Ali Afshar2dcc6522010-12-16 10:11:53 +010068
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070069 def __repr__(self):
70 reason = self._get_reason()
71 if self.error_details:
72 return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % (
73 self.resp.status,
74 self.uri,
75 reason.strip(),
76 self.error_details,
77 )
78 elif self.uri:
79 return '<HttpError %s when requesting %s returned "%s">' % (
80 self.resp.status,
81 self.uri,
82 self._get_reason().strip(),
83 )
84 else:
85 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010086
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070087 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050088
89
Joe Gregorio49396552011-03-08 10:39:00 -050090class InvalidJsonError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070091 """The JSON returned could not be parsed."""
92
93 pass
Joe Gregorio49396552011-03-08 10:39:00 -050094
95
Joe Gregoriodc106fc2012-11-20 14:30:14 -050096class UnknownFileType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070097 """File type unknown or unexpected."""
98
99 pass
Joe Gregoriodc106fc2012-11-20 14:30:14 -0500100
101
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -0500102class UnknownLinkType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700103 """Link type unknown or unexpected."""
104
105 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400106
Joe Gregorio66f57522011-11-30 11:00:00 -0500107
Joe Gregoriodae2f552011-11-21 08:16:56 -0500108class UnknownApiNameOrVersion(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700109 """No API with that name and version exists."""
110
111 pass
Joe Gregorio8b4df3f2011-11-18 15:44:48 -0500112
Joe Gregorioa388ce32011-09-09 17:19:13 -0400113
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400114class UnacceptableMimeTypeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700115 """That is an unacceptable mimetype for this operation."""
116
117 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400118
Joe Gregorioa388ce32011-09-09 17:19:13 -0400119
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400120class MediaUploadSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700121 """Media is larger than the method can accept."""
122
123 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400124
125
Joe Gregoriobaf04802013-03-01 12:27:06 -0500126class ResumableUploadError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000127 """Error occurred during resumable upload."""
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700128
129 pass
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500130
131
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400132class InvalidChunkSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700133 """The given chunksize is not valid."""
134
135 pass
136
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400137
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400138class InvalidNotificationError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700139 """The channel Notification is invalid."""
140
141 pass
142
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400143
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500144class BatchError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000145 """Error occurred during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500146
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700147 @util.positional(2)
148 def __init__(self, reason, resp=None, content=None):
149 self.resp = resp
150 self.content = content
151 self.reason = reason
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500152
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700153 def __repr__(self):
154 if getattr(self.resp, "status", None) is None:
155 return '<BatchError "%s">' % (self.reason)
156 else:
157 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500158
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700159 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500160
161
Joe Gregorioa388ce32011-09-09 17:19:13 -0400162class UnexpectedMethodError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700163 """Exception raised by RequestMockBuilder on unexpected calls."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400164
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700165 @util.positional(1)
166 def __init__(self, methodId=None):
167 """Constructor for an UnexpectedMethodError."""
168 super(UnexpectedMethodError, self).__init__(
169 "Received unexpected call %s" % methodId
170 )
Joe Gregorioa388ce32011-09-09 17:19:13 -0400171
172
173class UnexpectedBodyError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700174 """Exception raised by RequestMockBuilder on unexpected bodies."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400175
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700176 def __init__(self, expected, provided):
177 """Constructor for an UnexpectedMethodError."""
178 super(UnexpectedBodyError, self).__init__(
179 "Expected: [%s] - Provided: [%s]" % (expected, provided)
180 )