-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathexceptions.py
237 lines (159 loc) · 8.31 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Firebase Exceptions module.
This module defines the base types for exceptions and the platform-wide error codes as outlined in
https://cloud.google.com/apis/design/errors.
:class:`FirebaseError` is the parent class of all exceptions raised by the Admin SDK. It contains
the ``code``, ``http_response`` and ``cause`` properties common to all Firebase exception types.
Each exception also carries a message that outlines what went wrong. This can be logged for
audit or debugging purposes.
When calling an Admin SDK API, developers can catch the parent ``FirebaseError`` and
inspect its ``code`` to implement fine-grained error handling. Alternatively, developers can
catch one or more subtypes of ``FirebaseError``. Under normal conditions, any given API can raise
only a small subset of the available exception subtypes. However, the SDK also exposes rare error
conditions like connection timeouts and other I/O errors as instances of ``FirebaseError``.
Therefore it is always a good idea to have a handler specified for ``FirebaseError``, after all the
subtype error handlers.
"""
#: Error code for ``InvalidArgumentError`` type.
INVALID_ARGUMENT = 'INVALID_ARGUMENT'
#: Error code for ``FailedPreconditionError`` type.
FAILED_PRECONDITION = 'FAILED_PRECONDITION'
#: Error code for ``OutOfRangeError`` type.
OUT_OF_RANGE = 'OUT_OF_RANGE'
#: Error code for ``UnauthenticatedError`` type.
UNAUTHENTICATED = 'UNAUTHENTICATED'
#: Error code for ``PermissionDeniedError`` type.
PERMISSION_DENIED = 'PERMISSION_DENIED'
#: Error code for ``NotFoundError`` type.
NOT_FOUND = 'NOT_FOUND'
#: Error code for ``ConflictError`` type.
CONFLICT = 'CONFLICT'
#: Error code for ``AbortedError`` type.
ABORTED = 'ABORTED'
#: Error code for ``AlreadyExistsError`` type.
ALREADY_EXISTS = 'ALREADY_EXISTS'
#: Error code for ``ResourceExhaustedError`` type.
RESOURCE_EXHAUSTED = 'RESOURCE_EXHAUSTED'
#: Error code for ``CancelledError`` type.
CANCELLED = 'CANCELLED'
#: Error code for ``DataLossError`` type.
DATA_LOSS = 'DATA_LOSS'
#: Error code for ``UnknownError`` type.
UNKNOWN = 'UNKNOWN'
#: Error code for ``InternalError`` type.
INTERNAL = 'INTERNAL'
#: Error code for ``UnavailableError`` type.
UNAVAILABLE = 'UNAVAILABLE'
#: Error code for ``DeadlineExceededError`` type.
DEADLINE_EXCEEDED = 'DEADLINE_EXCEEDED'
class FirebaseError(Exception):
"""Base class for all errors raised by the Admin SDK.
Args:
code: A string error code that represents the type of the exception. Possible error
codes are defined in https://cloud.google.com/apis/design/errors#handling_errors.
message: A human-readable error message string.
cause: The exception that caused this error (optional).
http_response: If this error was caused by an HTTP error response, this property is
set to the ``requests.Response`` object that represents the HTTP response (optional).
See https://docs.python-requests.org/en/master/api/#requests.Response for details of
this object.
"""
def __init__(self, code, message, cause=None, http_response=None):
Exception.__init__(self, message)
self._code = code
self._cause = cause
self._http_response = http_response
@property
def code(self):
return self._code
@property
def cause(self):
return self._cause
@property
def http_response(self):
return self._http_response
class InvalidArgumentError(FirebaseError):
"""Client specified an invalid argument."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, INVALID_ARGUMENT, message, cause, http_response)
class FailedPreconditionError(FirebaseError):
"""Request can not be executed in the current system state, such as deleting a non-empty
directory."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, FAILED_PRECONDITION, message, cause, http_response)
class OutOfRangeError(FirebaseError):
"""Client specified an invalid range."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, OUT_OF_RANGE, message, cause, http_response)
class UnauthenticatedError(FirebaseError):
"""Request not authenticated due to missing, invalid, or expired OAuth token."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, UNAUTHENTICATED, message, cause, http_response)
class PermissionDeniedError(FirebaseError):
"""Client does not have sufficient permission.
This can happen because the OAuth token does not have the right scopes, the client doesn't
have permission, or the API has not been enabled for the client project.
"""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, PERMISSION_DENIED, message, cause, http_response)
class NotFoundError(FirebaseError):
"""A specified resource is not found, or the request is rejected by undisclosed reasons, such
as whitelisting."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, NOT_FOUND, message, cause, http_response)
class ConflictError(FirebaseError):
"""Concurrency conflict, such as read-modify-write conflict."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, CONFLICT, message, cause, http_response)
class AbortedError(FirebaseError):
"""Concurrency conflict, such as read-modify-write conflict."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, ABORTED, message, cause, http_response)
class AlreadyExistsError(FirebaseError):
"""The resource that a client tried to create already exists."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, ALREADY_EXISTS, message, cause, http_response)
class ResourceExhaustedError(FirebaseError):
"""Either out of resource quota or reaching rate limiting."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, RESOURCE_EXHAUSTED, message, cause, http_response)
class CancelledError(FirebaseError):
"""Request cancelled by the client."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, CANCELLED, message, cause, http_response)
class DataLossError(FirebaseError):
"""Unrecoverable data loss or data corruption."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, DATA_LOSS, message, cause, http_response)
class UnknownError(FirebaseError):
"""Unknown server error."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, UNKNOWN, message, cause, http_response)
class InternalError(FirebaseError):
"""Internal server error."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, INTERNAL, message, cause, http_response)
class UnavailableError(FirebaseError):
"""Service unavailable. Typically the server is down."""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, UNAVAILABLE, message, cause, http_response)
class DeadlineExceededError(FirebaseError):
"""Request deadline exceeded.
This will happen only if the caller sets a deadline that is shorter than the method's
default deadline (i.e. requested deadline is not enough for the server to process the
request) and the request did not finish within the deadline.
"""
def __init__(self, message, cause=None, http_response=None):
FirebaseError.__init__(self, DEADLINE_EXCEEDED, message, cause, http_response)