|
| 1 | +# Copyright 2019 Google Inc. |
| 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 | +"""Firebase Exceptions module. |
| 16 | +
|
| 17 | +This module defines the base types for exceptions and the platform-wide error codes as outlined in |
| 18 | +https://cloud.google.com/apis/design/errors. |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +INVALID_ARGUMENT = 'INVALID_ARGUMENT' |
| 23 | +FAILED_PRECONDITION = 'FAILED_PRECONDITION' |
| 24 | +OUT_OF_RANGE = 'OUT_OF_RANGE' |
| 25 | +UNAUTHENTICATED = 'UNAUTHENTICATED' |
| 26 | +PERMISSION_DENIED = 'PERMISSION_DENIED' |
| 27 | +NOT_FOUND = 'NOT_FOUND' |
| 28 | +CONFLICT = 'CONFLICT' |
| 29 | +ABORTED = 'ABORTED' |
| 30 | +ALREADY_EXISTS = 'ALREADY_EXISTS' |
| 31 | +RESOURCE_EXHAUSTED = 'RESOURCE_EXHAUSTED' |
| 32 | +CANCELLED = 'CANCELLED' |
| 33 | +DATA_LOSS = 'DATA_LOSS' |
| 34 | +UNKNOWN = 'UNKNOWN' |
| 35 | +INTERNAL = 'INTERNAL' |
| 36 | +UNAVAILABLE = 'UNAVAILABLE' |
| 37 | +DEADLINE_EXCEEDED = 'DEADLINE_EXCEEDED' |
| 38 | + |
| 39 | + |
| 40 | +class FirebaseError(Exception): |
| 41 | + """Base class for all errors raised by the Admin SDK.""" |
| 42 | + |
| 43 | + def __init__(self, code, message, cause=None, http_response=None): |
| 44 | + Exception.__init__(self, message) |
| 45 | + self._code = code |
| 46 | + self._cause = cause |
| 47 | + self._http_response = http_response |
| 48 | + |
| 49 | + @property |
| 50 | + def code(self): |
| 51 | + return self._code |
| 52 | + |
| 53 | + @property |
| 54 | + def cause(self): |
| 55 | + return self._cause |
| 56 | + |
| 57 | + @property |
| 58 | + def http_response(self): |
| 59 | + return self._http_response |
| 60 | + |
| 61 | + |
| 62 | +class InvalidArgumentError(FirebaseError): |
| 63 | + """Client specified an invalid argument.""" |
| 64 | + |
| 65 | + def __init__(self, message, cause=None, http_response=None): |
| 66 | + FirebaseError.__init__(self, INVALID_ARGUMENT, message, cause, http_response) |
| 67 | + |
| 68 | + |
| 69 | +class FailedPreconditionError(FirebaseError): |
| 70 | + """Request can not be executed in the current system state, such as deleting a non-empty |
| 71 | + directory.""" |
| 72 | + |
| 73 | + def __init__(self, message, cause=None, http_response=None): |
| 74 | + FirebaseError.__init__(self, FAILED_PRECONDITION, message, cause, http_response) |
| 75 | + |
| 76 | + |
| 77 | +class OutOfRangeError(FirebaseError): |
| 78 | + """Client specified an invalid range.""" |
| 79 | + |
| 80 | + def __init__(self, message, cause=None, http_response=None): |
| 81 | + FirebaseError.__init__(self, OUT_OF_RANGE, message, cause, http_response) |
| 82 | + |
| 83 | + |
| 84 | +class UnauthenticatedError(FirebaseError): |
| 85 | + """Request not authenticated due to missing, invalid, or expired OAuth token.""" |
| 86 | + |
| 87 | + def __init__(self, message, cause=None, http_response=None): |
| 88 | + FirebaseError.__init__(self, UNAUTHENTICATED, message, cause, http_response) |
| 89 | + |
| 90 | + |
| 91 | +class PermissionDeniedError(FirebaseError): |
| 92 | + """Client does not have sufficient permission. |
| 93 | +
|
| 94 | + This can happen because the OAuth token does not have the right scopes, the client doesn't |
| 95 | + have permission, or the API has not been enabled for the client project. |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__(self, message, cause=None, http_response=None): |
| 99 | + FirebaseError.__init__(self, PERMISSION_DENIED, message, cause, http_response) |
| 100 | + |
| 101 | + |
| 102 | +class NotFoundError(FirebaseError): |
| 103 | + """A specified resource is not found, or the request is rejected by undisclosed reasons, such |
| 104 | + as whitelisting.""" |
| 105 | + |
| 106 | + def __init__(self, message, cause=None, http_response=None): |
| 107 | + FirebaseError.__init__(self, NOT_FOUND, message, cause, http_response) |
| 108 | + |
| 109 | + |
| 110 | +class ConflictError(FirebaseError): |
| 111 | + """Concurrency conflict, such as read-modify-write conflict.""" |
| 112 | + |
| 113 | + def __init__(self, message, cause=None, http_response=None): |
| 114 | + FirebaseError.__init__(self, CONFLICT, message, cause, http_response) |
| 115 | + |
| 116 | + |
| 117 | +class AbortedError(FirebaseError): |
| 118 | + """Concurrency conflict, such as read-modify-write conflict.""" |
| 119 | + |
| 120 | + def __init__(self, message, cause=None, http_response=None): |
| 121 | + FirebaseError.__init__(self, ABORTED, message, cause, http_response) |
| 122 | + |
| 123 | + |
| 124 | +class AlreadyExistsError(FirebaseError): |
| 125 | + """The resource that a client tried to create already exists.""" |
| 126 | + |
| 127 | + def __init__(self, message, cause=None, http_response=None): |
| 128 | + FirebaseError.__init__(self, ALREADY_EXISTS, message, cause, http_response) |
| 129 | + |
| 130 | + |
| 131 | +class ResourceExhaustedError(FirebaseError): |
| 132 | + """Either out of resource quota or reaching rate limiting.""" |
| 133 | + |
| 134 | + def __init__(self, message, cause=None, http_response=None): |
| 135 | + FirebaseError.__init__(self, RESOURCE_EXHAUSTED, message, cause, http_response) |
| 136 | + |
| 137 | + |
| 138 | +class CancelledError(FirebaseError): |
| 139 | + """Request cancelled by the client.""" |
| 140 | + |
| 141 | + def __init__(self, message, cause=None, http_response=None): |
| 142 | + FirebaseError.__init__(self, CANCELLED, message, cause, http_response) |
| 143 | + |
| 144 | + |
| 145 | +class DataLossError(FirebaseError): |
| 146 | + """Unrecoverable data loss or data corruption.""" |
| 147 | + |
| 148 | + def __init__(self, message, cause=None, http_response=None): |
| 149 | + FirebaseError.__init__(self, DATA_LOSS, message, cause, http_response) |
| 150 | + |
| 151 | + |
| 152 | +class UnknownError(FirebaseError): |
| 153 | + """Unknown server error.""" |
| 154 | + |
| 155 | + def __init__(self, message, cause=None, http_response=None): |
| 156 | + FirebaseError.__init__(self, UNKNOWN, message, cause, http_response) |
| 157 | + |
| 158 | + |
| 159 | +class InternalError(FirebaseError): |
| 160 | + """Internal server error.""" |
| 161 | + |
| 162 | + def __init__(self, message, cause=None, http_response=None): |
| 163 | + FirebaseError.__init__(self, INTERNAL, message, cause, http_response) |
| 164 | + |
| 165 | + |
| 166 | +class UnavailableError(FirebaseError): |
| 167 | + """Service unavailable. Typically the server is down.""" |
| 168 | + |
| 169 | + def __init__(self, message, cause=None, http_response=None): |
| 170 | + FirebaseError.__init__(self, UNAVAILABLE, message, cause, http_response) |
| 171 | + |
| 172 | + |
| 173 | +class DeadlineExceededError(FirebaseError): |
| 174 | + """Request deadline exceeded. |
| 175 | +
|
| 176 | + This will happen only if the caller sets a deadline that is shorter than the method's |
| 177 | + default deadline (i.e. requested deadline is not enough for the server to process the |
| 178 | + request) and the request did not finish within the deadline. |
| 179 | + """ |
| 180 | + |
| 181 | + def __init__(self, message, cause=None, http_response=None): |
| 182 | + FirebaseError.__init__(self, DEADLINE_EXCEEDED, message, cause, http_response) |
0 commit comments