1#!/usr/bin/env python
2"""Exceptions for generated client libraries."""
3
4
5class Error(Exception):
6
7    """Base class for all exceptions."""
8
9
10class TypecheckError(Error, TypeError):
11
12    """An object of an incorrect type is provided."""
13
14
15class NotFoundError(Error):
16
17    """A specified resource could not be found."""
18
19
20class UserError(Error):
21
22    """Base class for errors related to user input."""
23
24
25class InvalidDataError(Error):
26
27    """Base class for any invalid data error."""
28
29
30class CommunicationError(Error):
31
32    """Any communication error talking to an API server."""
33
34
35class 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
62class InvalidUserInputError(InvalidDataError):
63
64    """User-provided input is invalid."""
65
66
67class InvalidDataFromServerError(InvalidDataError, CommunicationError):
68
69    """Data received from the server is malformed."""
70
71
72class BatchError(Error):
73
74    """Error generated while constructing a batch request."""
75
76
77class ConfigurationError(Error):
78
79    """Base class for configuration errors."""
80
81
82class GeneratedClientError(Error):
83
84    """The generated client configuration is invalid."""
85
86
87class ConfigurationValueError(UserError):
88
89    """Some part of the user-specified client configuration is invalid."""
90
91
92class ResourceUnavailableError(Error):
93
94    """User requested an unavailable resource."""
95
96
97class CredentialsError(Error):
98
99    """Errors related to invalid credentials."""
100
101
102class TransferError(CommunicationError):
103
104    """Errors related to transfers."""
105
106
107class TransferRetryError(TransferError):
108
109    """Retryable errors related to transfers."""
110
111
112class TransferInvalidError(TransferError):
113
114    """The given transfer is invalid."""
115
116
117class RequestError(CommunicationError):
118
119    """The request was not successful."""
120
121
122class 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
136class BadStatusCodeError(HttpError):
137
138    """The request completed but returned a bad status code."""
139
140
141class NotYetImplementedError(GeneratedClientError):
142
143    """This functionality is not yet implemented."""
144
145
146class StreamExhausted(Error):
147
148    """Attempted to read more bytes from a stream than were available."""
149