Exemplo n.º 1
0
    def handle_rtdb_error(cls, error):
        """Converts an error encountered while calling RTDB into a FirebaseError."""
        if error.response is None:
            return _utils.handle_requests_error(error)

        message = cls._extract_error_message(error.response)
        return _utils.handle_requests_error(error, message=message)
Exemplo n.º 2
0
def handle_auth_backend_error(error):
    """Converts a requests error received from the Firebase Auth service into a FirebaseError."""
    if error.response is None:
        return _utils.handle_requests_error(error)

    code, custom_message = _parse_error_body(error.response)
    if not code:
        msg = 'Unexpected error response: {0}'.format(error.response.content.decode())
        return _utils.handle_requests_error(error, message=msg)

    exc_type = _CODE_TO_EXC_TYPE.get(code)
    msg = _build_error_message(code, exc_type, custom_message)
    if not exc_type:
        return _utils.handle_requests_error(error, message=msg)

    return exc_type(msg, cause=error, http_response=error.response)
Exemplo n.º 3
0
 def test_http_response_with_unknown_status(self):
     resp, error = self._create_response(status=501)
     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
Exemplo n.º 4
0
 def test_http_response(self):
     resp, error = self._create_response()
     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
Exemplo n.º 5
0
 def test_http_response_with_code(self):
     resp, error = self._create_response()
     firebase_error = _utils.handle_requests_error(
         error, code=exceptions.UNAVAILABLE)
     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
Exemplo n.º 6
0
 def test_http_response_with_message(self):
     resp, error = self._create_response()
     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
Exemplo n.º 7
0
 def test_requests_connection_error(self):
     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
Exemplo n.º 8
0
 def test_timeout_error(self):
     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
Exemplo n.º 9
0
 def test_unknown_transport_error(self):
     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
Exemplo n.º 10
0
 def delete_instance_id(self, instance_id):
     if not isinstance(instance_id, str) or not instance_id:
         raise ValueError('Instance ID must be a non-empty string.')
     path = 'project/{0}/instanceId/{1}'.format(self._project_id, instance_id)
     try:
         self._client.request('delete', path)
     except requests.exceptions.RequestException as error:
         msg = self._extract_message(instance_id, error)
         raise _utils.handle_requests_error(error, msg)
Exemplo n.º 11
0
    def _handle_iid_error(self, error):
        """Handles errors received from the Instance ID API."""
        if error.response is None:
            raise _utils.handle_requests_error(error)

        data = {}
        try:
            parsed_body = error.response.json()
            if isinstance(parsed_body, dict):
                data = parsed_body
        except ValueError:
            pass

        # IID error response format: {"error": "some error message"}
        msg = data.get('error')
        if not msg:
            msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format(
                error.response.status_code, error.response.content.decode())

        return _utils.handle_requests_error(error, msg)
Exemplo n.º 12
0
    def _handle_iid_error(self, error):
        """Handles errors received from the Instance ID API."""
        if error.response is None:
            raise _utils.handle_requests_error(error)

        data = {}
        try:
            parsed_body = error.response.json()
            if isinstance(parsed_body, dict):
                data = parsed_body
        except ValueError:
            pass

        # IID error response format: {"error": "ErrorCode"}
        code = data.get('error')
        msg = None
        if code:
            msg = 'Error while calling the IID service: {0}'.format(code)
        else:
            msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format(
                error.response.status_code, error.response.content.decode())

        return _utils.handle_requests_error(error, msg)