blob: 6a741f86ddc8443960466f8158b756053033c342 [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
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
Craig Citro6ae34d72014-08-18 23:10:09 -070024import json
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050025
Jon Wayne Parrott6755f612016-08-15 10:52:26 -070026# Oauth2client < 3 has the positional helper in 'util', >= 3 has it
27# in '_helpers'.
28try:
29 from oauth2client import util
30except ImportError:
31 from oauth2client import _helpers as util
Ali Afshar2dcc6522010-12-16 10:11:53 +010032
33
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050034class Error(Exception):
35 """Base error for this module."""
36 pass
37
38
39class HttpError(Error):
40 """HTTP data was invalid or unexpected."""
41
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040042 @util.positional(3)
Joe Gregorio49396552011-03-08 10:39:00 -050043 def __init__(self, resp, content, uri=None):
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050044 self.resp = resp
INADA Naoki09157612015-03-25 01:51:03 +090045 if not isinstance(content, bytes):
46 raise TypeError("HTTP content should be bytes")
Ali Afshar2dcc6522010-12-16 10:11:53 +010047 self.content = content
Joe Gregorio49396552011-03-08 10:39:00 -050048 self.uri = uri
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050049
Ali Afshar2dcc6522010-12-16 10:11:53 +010050 def _get_reason(self):
Joe Gregoriofdf7c802011-06-30 12:33:38 -040051 """Calculate the reason for the error from the response content."""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040052 reason = self.resp.reason
53 try:
INADA Naoki09157612015-03-25 01:51:03 +090054 data = json.loads(self.content.decode('utf-8'))
Thang Minh Vub7122b32016-10-15 01:57:15 +080055 if isinstance(data, dict):
56 reason = data['error']['message']
57 elif isinstance(data, list) and len(data) > 0:
58 first_error = data[0]
59 reason = first_error['error']['message']
60 except (ValueError, KeyError, TypeError):
Joe Gregorio20b54fb2012-07-26 09:59:35 -040061 pass
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050062 if reason is None:
63 reason = ''
Ali Afshar2dcc6522010-12-16 10:11:53 +010064 return reason
65
66 def __repr__(self):
Joe Gregorio49396552011-03-08 10:39:00 -050067 if self.uri:
68 return '<HttpError %s when requesting %s returned "%s">' % (
Joe Gregorio3fb93672012-07-25 11:31:11 -040069 self.resp.status, self.uri, self._get_reason().strip())
Joe Gregorio49396552011-03-08 10:39:00 -050070 else:
71 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason())
Ali Afshar2dcc6522010-12-16 10:11:53 +010072
73 __str__ = __repr__
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050074
75
Joe Gregorio49396552011-03-08 10:39:00 -050076class InvalidJsonError(Error):
77 """The JSON returned could not be parsed."""
78 pass
79
80
Joe Gregoriodc106fc2012-11-20 14:30:14 -050081class UnknownFileType(Error):
82 """File type unknown or unexpected."""
83 pass
84
85
Joe Gregorio3ad5e9a2010-12-09 15:01:04 -050086class UnknownLinkType(Error):
87 """Link type unknown or unexpected."""
88 pass
Joe Gregoriofdf7c802011-06-30 12:33:38 -040089
Joe Gregorio66f57522011-11-30 11:00:00 -050090
Joe Gregoriodae2f552011-11-21 08:16:56 -050091class UnknownApiNameOrVersion(Error):
Joe Gregorio8b4df3f2011-11-18 15:44:48 -050092 """No API with that name and version exists."""
93 pass
94
Joe Gregorioa388ce32011-09-09 17:19:13 -040095
Joe Gregoriofdf7c802011-06-30 12:33:38 -040096class UnacceptableMimeTypeError(Error):
97 """That is an unacceptable mimetype for this operation."""
98 pass
99
Joe Gregorioa388ce32011-09-09 17:19:13 -0400100
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400101class MediaUploadSizeError(Error):
102 """Media is larger than the method can accept."""
103 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400104
105
Joe Gregoriobaf04802013-03-01 12:27:06 -0500106class ResumableUploadError(HttpError):
Joe Gregoriod0bd3882011-11-22 09:49:47 -0500107 """Error occured during resumable upload."""
108 pass
109
110
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400111class InvalidChunkSizeError(Error):
112 """The given chunksize is not valid."""
113 pass
114
Joe Gregorio1a5e30e2013-06-25 15:35:47 -0400115class InvalidNotificationError(Error):
116 """The channel Notification is invalid."""
117 pass
Joe Gregorioc80ac9d2012-08-21 14:09:09 -0400118
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500119class BatchError(HttpError):
Joe Gregorio66f57522011-11-30 11:00:00 -0500120 """Error occured during batch operations."""
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500121
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400122 @util.positional(2)
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500123 def __init__(self, reason, resp=None, content=None):
124 self.resp = resp
125 self.content = content
126 self.reason = reason
127
128 def __repr__(self):
ittus5f00cad2016-10-15 10:32:40 +0800129 if getattr(self.resp, 'status', None) is None:
130 return '<BatchError "%s">' % (self.reason)
131 else:
Joe Gregorio5d1171b2012-01-05 10:48:24 -0500132 return '<BatchError %s "%s">' % (self.resp.status, self.reason)
133
134 __str__ = __repr__
Joe Gregorio66f57522011-11-30 11:00:00 -0500135
136
Joe Gregorioa388ce32011-09-09 17:19:13 -0400137class UnexpectedMethodError(Error):
138 """Exception raised by RequestMockBuilder on unexpected calls."""
139
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400140 @util.positional(1)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400141 def __init__(self, methodId=None):
142 """Constructor for an UnexpectedMethodError."""
143 super(UnexpectedMethodError, self).__init__(
144 'Received unexpected call %s' % methodId)
145
146
147class UnexpectedBodyError(Error):
148 """Exception raised by RequestMockBuilder on unexpected bodies."""
149
150 def __init__(self, expected, provided):
151 """Constructor for an UnexpectedMethodError."""
152 super(UnexpectedBodyError, self).__init__(
153 'Expected: [%s] - Provided: [%s]' % (expected, provided))