-
Couldn't load subscription status.
- Fork 344
Introduced the exceptions module #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,22 @@ | |
|
|
||
| """Internal utilities common to all modules.""" | ||
|
|
||
| import requests | ||
|
|
||
| import firebase_admin | ||
| from firebase_admin import exceptions | ||
|
|
||
|
|
||
| _STATUS_TO_EXCEPTION_TYPE = { | ||
| 400: exceptions.InvalidArgumentError, | ||
| 401: exceptions.UnautenticatedError, | ||
| 403: exceptions.PermissionDeniedError, | ||
| 404: exceptions.NotFoundError, | ||
| 409: exceptions.ConflictError, | ||
| 429: exceptions.ResourceExhaustedError, | ||
| 500: exceptions.InternalError, | ||
| 503: exceptions.UnavailableError, | ||
| } | ||
|
|
||
|
|
||
| def _get_initialized_app(app): | ||
|
|
@@ -33,3 +48,25 @@ def _get_initialized_app(app): | |
| def get_app_service(app, name, initializer): | ||
| app = _get_initialized_app(app) | ||
| return app._get_service(name, initializer) # pylint: disable=protected-access | ||
|
|
||
| def handle_requests_error(error, message=None, status=None): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document this method, especially what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| """Constructs a FirebaseError from the given requests error.""" | ||
| if isinstance(error, requests.exceptions.Timeout): | ||
| return exceptions.DeadlineExceededError( | ||
| message='Timed out while making an API call: {0}'.format(error), | ||
| cause=error) | ||
| elif isinstance(error, requests.exceptions.ConnectionError): | ||
| return exceptions.UnavailableError( | ||
| message='Failed to establish a connection: {0}'.format(error), | ||
| cause=error) | ||
| elif error.response is None: | ||
| return exceptions.UnknownError( | ||
| message='Unknown error while making a remote service call: {0}'.format(error), | ||
| cause=error) | ||
|
|
||
| if not status: | ||
| status = error.response.status_code | ||
| if not message: | ||
| message = str(error) | ||
| err_type = _STATUS_TO_EXCEPTION_TYPE.get(status, exceptions.UnknownError) | ||
| return err_type(message=message, cause=error, http_response=error.response) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Copyright 20190 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. | ||
| """ | ||
|
|
||
|
|
||
| INVALID_ARGUMENT = 'INVALID_ARGUMENT' | ||
| FAILED_PRECONDITION = 'FAILED_PRECONDITION' | ||
| OUT_OF_RANGE = 'OUT_OF_RANGE' | ||
| UNAUTHENTICATED = 'UNAUTHENTICATED' | ||
| PERMISSION_DENIED = 'PERMISSION_DENIED' | ||
| NOT_FOUND = 'NOT_FOUND' | ||
| CONFLICT = 'CONFLICT' | ||
| ABORTED = 'ABORTED' | ||
| ALREADY_EXISTS = 'ALREADY_EXISTS' | ||
| RESOURCE_EXHAUSTED = 'RESOURCE_EXHAUSTED' | ||
| CANCELLED = 'CANCELLED' | ||
| DATA_LOSS = 'DATA_LOSS' | ||
| UNKNOWN = 'UNKNOWN' | ||
| INTERNAL = 'INTERNAL' | ||
| UNAVAILABLE = 'UNAVAILABLE' | ||
| DEADLINE_EXCEEDED = 'DEADLINE_EXCEEDED' | ||
|
|
||
|
|
||
| class FirebaseError(Exception): | ||
| """Base class for all errors raised by the Admin SDK.""" | ||
|
|
||
| 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want doc strings on all these types? We could reuse the comments from the status proto file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Took documentation from the GCP docs. |
||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, INVALID_ARGUMENT, message, cause, http_response) | ||
|
|
||
|
|
||
| class FailedPreconditionError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, FAILED_PRECONDITION, message, cause, http_response) | ||
|
|
||
|
|
||
| class OutOfRangeError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, OUT_OF_RANGE, message, cause, http_response) | ||
|
|
||
|
|
||
| class UnautenticatedError(FirebaseError): | ||
|
||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, UNAUTHENTICATED, message, cause, http_response) | ||
|
|
||
|
|
||
| class PermissionDeniedError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, PERMISSION_DENIED, message, cause, http_response) | ||
|
|
||
|
|
||
| class NotFoundError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, NOT_FOUND, message, cause, http_response) | ||
|
|
||
|
|
||
| class ConflictError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, CONFLICT, message, cause, http_response) | ||
|
|
||
|
|
||
| class AbortedError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, ABORTED, message, cause, http_response) | ||
|
|
||
|
|
||
| class AlreadyExistsError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, ALREADY_EXISTS, message, cause, http_response) | ||
|
|
||
|
|
||
| class ResourceExhaustedError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, RESOURCE_EXHAUSTED, message, cause, http_response) | ||
|
|
||
|
|
||
| class CancelledError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, CANCELLED, message, cause, http_response) | ||
|
|
||
|
|
||
| class DataLossError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, DATA_LOSS, message, cause, http_response) | ||
|
|
||
|
|
||
| class UnknownError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, UNKNOWN, message, cause, http_response) | ||
|
|
||
|
|
||
| class InternalError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, INTERNAL, message, cause, http_response) | ||
|
|
||
|
|
||
| class UnavailableError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, UNAVAILABLE, message, cause, http_response) | ||
|
|
||
|
|
||
| class DeadlineExceededError(FirebaseError): | ||
|
|
||
| def __init__(self, message, cause=None, http_response=None): | ||
| FirebaseError.__init__(self, DEADLINE_EXCEEDED, message, cause, http_response) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # 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. | ||
|
|
||
|
|
||
| import requests | ||
| from requests import models | ||
|
|
||
| from firebase_admin import exceptions | ||
| from firebase_admin import _utils | ||
|
|
||
|
|
||
| def test_timeout_error(): | ||
| error = requests.exceptions.Timeout('Test error') | ||
| firebase_error = _utils.handle_requests_error(error) | ||
| assert isinstance(firebase_error, exceptions.DeadlineExceededError) | ||
| assert str(firebase_error) == 'Timed out while making an API call: Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is None | ||
|
|
||
| def test_connection_error(): | ||
| error = requests.exceptions.ConnectionError('Test error') | ||
| firebase_error = _utils.handle_requests_error(error) | ||
| assert isinstance(firebase_error, exceptions.UnavailableError) | ||
| assert str(firebase_error) == 'Failed to establish a connection: Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is None | ||
|
|
||
| def test_unknown_transport_error(): | ||
| error = requests.exceptions.RequestException('Test error') | ||
| firebase_error = _utils.handle_requests_error(error) | ||
| assert isinstance(firebase_error, exceptions.UnknownError) | ||
| assert str(firebase_error) == 'Unknown error while making a remote service call: Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is None | ||
|
|
||
| def test_http_response(): | ||
| resp = models.Response() | ||
| resp.status_code = 500 | ||
| error = requests.exceptions.RequestException('Test error', response=resp) | ||
| firebase_error = _utils.handle_requests_error(error) | ||
| assert isinstance(firebase_error, exceptions.InternalError) | ||
| assert str(firebase_error) == 'Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is resp | ||
|
|
||
| def test_http_response_with_unknown_status(): | ||
| resp = models.Response() | ||
| resp.status_code = 501 | ||
| error = requests.exceptions.RequestException('Test error', response=resp) | ||
| firebase_error = _utils.handle_requests_error(error) | ||
| assert isinstance(firebase_error, exceptions.UnknownError) | ||
| assert str(firebase_error) == 'Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is resp | ||
|
|
||
| def test_http_response_with_message(): | ||
| resp = models.Response() | ||
| resp.status_code = 500 | ||
| error = requests.exceptions.RequestException('Test error', response=resp) | ||
| firebase_error = _utils.handle_requests_error(error, message='Explicit error message') | ||
| assert isinstance(firebase_error, exceptions.InternalError) | ||
| assert str(firebase_error) == 'Explicit error message' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is resp | ||
|
|
||
| def test_http_response_with_status(): | ||
| resp = models.Response() | ||
| resp.status_code = 500 | ||
| error = requests.exceptions.RequestException('Test error', response=resp) | ||
| firebase_error = _utils.handle_requests_error(error, status=503) | ||
| assert isinstance(firebase_error, exceptions.UnavailableError) | ||
| assert str(firebase_error) == 'Test error' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is resp | ||
|
|
||
| def test_http_response_with_message_and_status(): | ||
| resp = models.Response() | ||
| resp.status_code = 500 | ||
| error = requests.exceptions.RequestException('Test error', response=resp) | ||
| firebase_error = _utils.handle_requests_error( | ||
| error, message='Explicit error message', status=503) | ||
| assert isinstance(firebase_error, exceptions.UnavailableError) | ||
| assert str(firebase_error) == 'Explicit error message' | ||
| assert firebase_error.cause is error | ||
| assert firebase_error.http_response is resp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UnauthenticatedError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done