blob: c9e884e58e15401e89408ce65a54e645152ca6e2 [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"]
Muad Mohameda341c5a2020-11-08 20:30:02 +000054 error_detail_keyword = next((kw for kw in ["detail", "details", "message"] if kw in data["error"]), "")
55 if error_detail_keyword:
56 self.error_details = data["error"][error_detail_keyword]
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070057 elif isinstance(data, list) and len(data) > 0:
58 first_error = data[0]
59 reason = first_error["error"]["message"]
60 if "details" in first_error["error"]:
61 self.error_details = first_error["error"]["details"]
62 except (ValueError, KeyError, TypeError):
63 pass
64 if reason is None:
65 reason = ""
66 return reason
Ali Afshar2dcc6522010-12-16 10:11:53 +010067
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070068 def __repr__(self):
69 reason = self._get_reason()
70 if self.error_details:
71 return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % (
72 self.resp.status,
73 self.uri,
74 reason.strip(),
75 self.error_details,
76 )
77 elif self.uri:
78 return '<HttpError %s when requesting %s returned "%s">' % (
79 self.resp.status,
80 self.uri,
81 self._get_reason().strip(),
82 )
83 else:
84 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010085
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070086 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050087
88
Joe Gregorio49396552011-03-08 10:39:00 -050089class InvalidJsonError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070090 """The JSON returned could not be parsed."""
91
92 pass
Joe Gregorio49396552011-03-08 10:39:00 -050093
94
Joe Gregoriodc106fc2012-11-20 14:30:14 -050095class UnknownFileType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070096 """File type unknown or unexpected."""
97
98 pass
Joe Gregoriodc106fc2012-11-20 14:30:14 -050099
100
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -0500101class UnknownLinkType(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700102 """Link type unknown or unexpected."""
103
104 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400105
Joe Gregorio66f57522011-11-30 11:00:00 -0500106
Joe Gregoriodae2f552011-11-21 08:16:56 -0500107class UnknownApiNameOrVersion(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700108 """No API with that name and version exists."""
109
110 pass
Joe Gregorio8b4df3f2011-11-18 15:44:48 -0500111
Joe Gregorioa388ce32011-09-09 17:19:13 -0400112
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400113class UnacceptableMimeTypeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700114 """That is an unacceptable mimetype for this operation."""
115
116 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400117
Joe Gregorioa388ce32011-09-09 17:19:13 -0400118
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400119class MediaUploadSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700120 """Media is larger than the method can accept."""
121
122 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400123
124
Joe Gregoriobaf04802013-03-01 12:27:06 -0500125class ResumableUploadError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000126 """Error occurred during resumable upload."""
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700127
128 pass
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500129
130
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400131class InvalidChunkSizeError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700132 """The given chunksize is not valid."""
133
134 pass
135
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400136
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400137class InvalidNotificationError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700138 """The channel Notification is invalid."""
139
140 pass
141
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400142
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500143class BatchError(HttpError):
Tim Gates43fc0cf2020-04-21 08:03:25 +1000144 """Error occurred during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500145
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700146 @util.positional(2)
147 def __init__(self, reason, resp=None, content=None):
148 self.resp = resp
149 self.content = content
150 self.reason = reason
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500151
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700152 def __repr__(self):
153 if getattr(self.resp, "status", None) is None:
154 return '<BatchError "%s">' % (self.reason)
155 else:
156 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500157
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700158 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500159
160
Joe Gregorioa388ce32011-09-09 17:19:13 -0400161class UnexpectedMethodError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700162 """Exception raised by RequestMockBuilder on unexpected calls."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400163
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700164 @util.positional(1)
165 def __init__(self, methodId=None):
166 """Constructor for an UnexpectedMethodError."""
167 super(UnexpectedMethodError, self).__init__(
168 "Received unexpected call %s" % methodId
169 )
Joe Gregorioa388ce32011-09-09 17:19:13 -0400170
171
172class UnexpectedBodyError(Error):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700173 """Exception raised by RequestMockBuilder on unexpected bodies."""
Joe Gregorioa388ce32011-09-09 17:19:13 -0400174
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700175 def __init__(self, expected, provided):
176 """Constructor for an UnexpectedMethodError."""
177 super(UnexpectedBodyError, self).__init__(
178 "Expected: [%s] - Provided: [%s]" % (expected, provided)
179 )