1# Copyright 2014 Google Inc. All Rights Reserved. 2# 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. 14 15"""Errors for the library. 16 17All exceptions defined by the library 18should be defined in this file. 19""" 20from __future__ import absolute_import 21 22__author__ = 'jcgregorio@google.com (Joe Gregorio)' 23 24import json 25 26from oauth2client import util 27 28 29class Error(Exception): 30 """Base error for this module.""" 31 pass 32 33 34class HttpError(Error): 35 """HTTP data was invalid or unexpected.""" 36 37 @util.positional(3) 38 def __init__(self, resp, content, uri=None): 39 self.resp = resp 40 self.content = content 41 self.uri = uri 42 43 def _get_reason(self): 44 """Calculate the reason for the error from the response content.""" 45 reason = self.resp.reason 46 try: 47 data = json.loads(self.content) 48 reason = data['error']['message'] 49 except (ValueError, KeyError): 50 pass 51 if reason is None: 52 reason = '' 53 return reason 54 55 def __repr__(self): 56 if self.uri: 57 return '<HttpError %s when requesting %s returned "%s">' % ( 58 self.resp.status, self.uri, self._get_reason().strip()) 59 else: 60 return '<HttpError %s "%s">' % (self.resp.status, self._get_reason()) 61 62 __str__ = __repr__ 63 64 65class InvalidJsonError(Error): 66 """The JSON returned could not be parsed.""" 67 pass 68 69 70class UnknownFileType(Error): 71 """File type unknown or unexpected.""" 72 pass 73 74 75class UnknownLinkType(Error): 76 """Link type unknown or unexpected.""" 77 pass 78 79 80class UnknownApiNameOrVersion(Error): 81 """No API with that name and version exists.""" 82 pass 83 84 85class UnacceptableMimeTypeError(Error): 86 """That is an unacceptable mimetype for this operation.""" 87 pass 88 89 90class MediaUploadSizeError(Error): 91 """Media is larger than the method can accept.""" 92 pass 93 94 95class ResumableUploadError(HttpError): 96 """Error occured during resumable upload.""" 97 pass 98 99 100class InvalidChunkSizeError(Error): 101 """The given chunksize is not valid.""" 102 pass 103 104class InvalidNotificationError(Error): 105 """The channel Notification is invalid.""" 106 pass 107 108class BatchError(HttpError): 109 """Error occured during batch operations.""" 110 111 @util.positional(2) 112 def __init__(self, reason, resp=None, content=None): 113 self.resp = resp 114 self.content = content 115 self.reason = reason 116 117 def __repr__(self): 118 return '<BatchError %s "%s">' % (self.resp.status, self.reason) 119 120 __str__ = __repr__ 121 122 123class UnexpectedMethodError(Error): 124 """Exception raised by RequestMockBuilder on unexpected calls.""" 125 126 @util.positional(1) 127 def __init__(self, methodId=None): 128 """Constructor for an UnexpectedMethodError.""" 129 super(UnexpectedMethodError, self).__init__( 130 'Received unexpected call %s' % methodId) 131 132 133class UnexpectedBodyError(Error): 134 """Exception raised by RequestMockBuilder on unexpected bodies.""" 135 136 def __init__(self, expected, provided): 137 """Constructor for an UnexpectedMethodError.""" 138 super(UnexpectedBodyError, self).__init__( 139 'Expected: [%s] - Provided: [%s]' % (expected, provided)) 140