1#!/usr/bin/env python 2# 3# Copyright 2015 Google Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Exceptions for generated client libraries.""" 18 19 20class Error(Exception): 21 22 """Base class for all exceptions.""" 23 24 25class TypecheckError(Error, TypeError): 26 27 """An object of an incorrect type is provided.""" 28 29 30class NotFoundError(Error): 31 32 """A specified resource could not be found.""" 33 34 35class UserError(Error): 36 37 """Base class for errors related to user input.""" 38 39 40class InvalidDataError(Error): 41 42 """Base class for any invalid data error.""" 43 44 45class CommunicationError(Error): 46 47 """Any communication error talking to an API server.""" 48 49 50class HttpError(CommunicationError): 51 52 """Error making a request. Soon to be HttpError.""" 53 54 def __init__(self, response, content, url, 55 method_config=None, request=None): 56 super(HttpError, self).__init__() 57 self.response = response 58 self.content = content 59 self.url = url 60 self.method_config = method_config 61 self.request = request 62 63 def __str__(self): 64 content = self.content 65 if isinstance(content, bytes): 66 content = self.content.decode('ascii', 'replace') 67 return 'HttpError accessing <%s>: response: <%s>, content <%s>' % ( 68 self.url, self.response, content) 69 70 @property 71 def status_code(self): 72 # TODO(craigcitro): Turn this into something better than a 73 # KeyError if there is no status. 74 return int(self.response['status']) 75 76 @classmethod 77 def FromResponse(cls, http_response): 78 return cls(http_response.info, http_response.content, 79 http_response.request_url) 80 81 82class InvalidUserInputError(InvalidDataError): 83 84 """User-provided input is invalid.""" 85 86 87class InvalidDataFromServerError(InvalidDataError, CommunicationError): 88 89 """Data received from the server is malformed.""" 90 91 92class BatchError(Error): 93 94 """Error generated while constructing a batch request.""" 95 96 97class ConfigurationError(Error): 98 99 """Base class for configuration errors.""" 100 101 102class GeneratedClientError(Error): 103 104 """The generated client configuration is invalid.""" 105 106 107class ConfigurationValueError(UserError): 108 109 """Some part of the user-specified client configuration is invalid.""" 110 111 112class ResourceUnavailableError(Error): 113 114 """User requested an unavailable resource.""" 115 116 117class CredentialsError(Error): 118 119 """Errors related to invalid credentials.""" 120 121 122class TransferError(CommunicationError): 123 124 """Errors related to transfers.""" 125 126 127class TransferRetryError(TransferError): 128 129 """Retryable errors related to transfers.""" 130 131 132class TransferInvalidError(TransferError): 133 134 """The given transfer is invalid.""" 135 136 137class RequestError(CommunicationError): 138 139 """The request was not successful.""" 140 141 142class RetryAfterError(HttpError): 143 144 """The response contained a retry-after header.""" 145 146 def __init__(self, response, content, url, retry_after): 147 super(RetryAfterError, self).__init__(response, content, url) 148 self.retry_after = int(retry_after) 149 150 @classmethod 151 def FromResponse(cls, http_response): 152 return cls(http_response.info, http_response.content, 153 http_response.request_url, http_response.retry_after) 154 155 156class BadStatusCodeError(HttpError): 157 158 """The request completed but returned a bad status code.""" 159 160 161class NotYetImplementedError(GeneratedClientError): 162 163 """This functionality is not yet implemented.""" 164 165 166class StreamExhausted(Error): 167 168 """Attempted to read more bytes from a stream than were available.""" 169