|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Exceptions for generated client libraries.""" |
| 3 | + |
| 4 | + |
| 5 | +class Error(Exception): |
| 6 | + |
| 7 | + """Base class for all exceptions.""" |
| 8 | + |
| 9 | + |
| 10 | +class TypecheckError(Error, TypeError): |
| 11 | + |
| 12 | + """An object of an incorrect type is provided.""" |
| 13 | + |
| 14 | + |
| 15 | +class NotFoundError(Error): |
| 16 | + |
| 17 | + """A specified resource could not be found.""" |
| 18 | + |
| 19 | + |
| 20 | +class UserError(Error): |
| 21 | + |
| 22 | + """Base class for errors related to user input.""" |
| 23 | + |
| 24 | + |
| 25 | +class InvalidDataError(Error): |
| 26 | + |
| 27 | + """Base class for any invalid data error.""" |
| 28 | + |
| 29 | + |
| 30 | +class CommunicationError(Error): |
| 31 | + |
| 32 | + """Any communication error talking to an API server.""" |
| 33 | + |
| 34 | + |
| 35 | +class HttpError(CommunicationError): |
| 36 | + |
| 37 | + """Error making a request. Soon to be HttpError.""" |
| 38 | + |
| 39 | + def __init__(self, response, content, url): |
| 40 | + super(HttpError, self).__init__() |
| 41 | + self.response = response |
| 42 | + self.content = content |
| 43 | + self.url = url |
| 44 | + |
| 45 | + def __str__(self): |
| 46 | + content = self.content.decode('ascii', 'replace') |
| 47 | + return 'HttpError accessing <%s>: response: <%s>, content <%s>' % ( |
| 48 | + self.url, self.response, content) |
| 49 | + |
| 50 | + @property |
| 51 | + def status_code(self): |
| 52 | + # TODO(craigcitro): Turn this into something better than a |
| 53 | + # KeyError if there is no status. |
| 54 | + return int(self.response['status']) |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def FromResponse(cls, http_response): |
| 58 | + return cls(http_response.info, http_response.content, |
| 59 | + http_response.request_url) |
| 60 | + |
| 61 | + |
| 62 | +class InvalidUserInputError(InvalidDataError): |
| 63 | + |
| 64 | + """User-provided input is invalid.""" |
| 65 | + |
| 66 | + |
| 67 | +class InvalidDataFromServerError(InvalidDataError, CommunicationError): |
| 68 | + |
| 69 | + """Data received from the server is malformed.""" |
| 70 | + |
| 71 | + |
| 72 | +class BatchError(Error): |
| 73 | + |
| 74 | + """Error generated while constructing a batch request.""" |
| 75 | + |
| 76 | + |
| 77 | +class ConfigurationError(Error): |
| 78 | + |
| 79 | + """Base class for configuration errors.""" |
| 80 | + |
| 81 | + |
| 82 | +class GeneratedClientError(Error): |
| 83 | + |
| 84 | + """The generated client configuration is invalid.""" |
| 85 | + |
| 86 | + |
| 87 | +class ConfigurationValueError(UserError): |
| 88 | + |
| 89 | + """Some part of the user-specified client configuration is invalid.""" |
| 90 | + |
| 91 | + |
| 92 | +class ResourceUnavailableError(Error): |
| 93 | + |
| 94 | + """User requested an unavailable resource.""" |
| 95 | + |
| 96 | + |
| 97 | +class CredentialsError(Error): |
| 98 | + |
| 99 | + """Errors related to invalid credentials.""" |
| 100 | + |
| 101 | + |
| 102 | +class TransferError(CommunicationError): |
| 103 | + |
| 104 | + """Errors related to transfers.""" |
| 105 | + |
| 106 | + |
| 107 | +class TransferRetryError(TransferError): |
| 108 | + |
| 109 | + """Retryable errors related to transfers.""" |
| 110 | + |
| 111 | + |
| 112 | +class TransferInvalidError(TransferError): |
| 113 | + |
| 114 | + """The given transfer is invalid.""" |
| 115 | + |
| 116 | + |
| 117 | +class RequestError(CommunicationError): |
| 118 | + |
| 119 | + """The request was not successful.""" |
| 120 | + |
| 121 | + |
| 122 | +class RetryAfterError(HttpError): |
| 123 | + |
| 124 | + """The response contained a retry-after header.""" |
| 125 | + |
| 126 | + def __init__(self, response, content, url, retry_after): |
| 127 | + super(RetryAfterError, self).__init__(response, content, url) |
| 128 | + self.retry_after = int(retry_after) |
| 129 | + |
| 130 | + @classmethod |
| 131 | + def FromResponse(cls, http_response): |
| 132 | + return cls(http_response.info, http_response.content, |
| 133 | + http_response.request_url, http_response.retry_after) |
| 134 | + |
| 135 | + |
| 136 | +class BadStatusCodeError(HttpError): |
| 137 | + |
| 138 | + """The request completed but returned a bad status code.""" |
| 139 | + |
| 140 | + |
| 141 | +class NotYetImplementedError(GeneratedClientError): |
| 142 | + |
| 143 | + """This functionality is not yet implemented.""" |
| 144 | + |
| 145 | + |
| 146 | +class StreamExhausted(Error): |
| 147 | + |
| 148 | + """Attempted to read more bytes from a stream than were available.""" |
0 commit comments