|
| 1 | +# Copyright 2021 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 | +"""Internal utilities for interacting with Google API client.""" |
| 16 | + |
| 17 | +import io |
| 18 | +import socket |
| 19 | + |
| 20 | +import googleapiclient |
| 21 | +import httplib2 |
| 22 | +import requests |
| 23 | + |
| 24 | +from firebase_admin import exceptions |
| 25 | +from firebase_admin import _utils |
| 26 | + |
| 27 | + |
| 28 | +def handle_platform_error_from_googleapiclient(error, handle_func=None): |
| 29 | + """Constructs a ``FirebaseError`` from the given googleapiclient error. |
| 30 | +
|
| 31 | + This can be used to handle errors returned by Google Cloud Platform (GCP) APIs. |
| 32 | +
|
| 33 | + Args: |
| 34 | + error: An error raised by the googleapiclient while making an HTTP call to a GCP API. |
| 35 | + handle_func: A function that can be used to handle platform errors in a custom way. When |
| 36 | + specified, this function will be called with three arguments. It has the same |
| 37 | + signature as ```_handle_func_googleapiclient``, but may return ``None``. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + FirebaseError: A ``FirebaseError`` that can be raised to the user code. |
| 41 | + """ |
| 42 | + if not isinstance(error, googleapiclient.errors.HttpError): |
| 43 | + return handle_googleapiclient_error(error) |
| 44 | + |
| 45 | + content = error.content.decode() |
| 46 | + status_code = error.resp.status |
| 47 | + error_dict, message = _utils._parse_platform_error(content, status_code) # pylint: disable=protected-access |
| 48 | + http_response = _http_response_from_googleapiclient_error(error) |
| 49 | + exc = None |
| 50 | + if handle_func: |
| 51 | + exc = handle_func(error, message, error_dict, http_response) |
| 52 | + |
| 53 | + return exc if exc else _handle_func_googleapiclient(error, message, error_dict, http_response) |
| 54 | + |
| 55 | + |
| 56 | +def _handle_func_googleapiclient(error, message, error_dict, http_response): |
| 57 | + """Constructs a ``FirebaseError`` from the given GCP error. |
| 58 | +
|
| 59 | + Args: |
| 60 | + error: An error raised by the googleapiclient module while making an HTTP call. |
| 61 | + message: A message to be included in the resulting ``FirebaseError``. |
| 62 | + error_dict: Parsed GCP error response. |
| 63 | + http_response: A requests HTTP response object to associate with the exception. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + FirebaseError: A ``FirebaseError`` that can be raised to the user code or None. |
| 67 | + """ |
| 68 | + code = error_dict.get('status') |
| 69 | + return handle_googleapiclient_error(error, message, code, http_response) |
| 70 | + |
| 71 | + |
| 72 | +def handle_googleapiclient_error(error, message=None, code=None, http_response=None): |
| 73 | + """Constructs a ``FirebaseError`` from the given googleapiclient error. |
| 74 | +
|
| 75 | + This method is agnostic of the remote service that produced the error, whether it is a GCP |
| 76 | + service or otherwise. Therefore, this method does not attempt to parse the error response in |
| 77 | + any way. |
| 78 | +
|
| 79 | + Args: |
| 80 | + error: An error raised by the googleapiclient module while making an HTTP call. |
| 81 | + message: A message to be included in the resulting ``FirebaseError`` (optional). If not |
| 82 | + specified the string representation of the ``error`` argument is used as the message. |
| 83 | + code: A GCP error code that will be used to determine the resulting error type (optional). |
| 84 | + If not specified the HTTP status code on the error response is used to determine a |
| 85 | + suitable error code. |
| 86 | + http_response: A requests HTTP response object to associate with the exception (optional). |
| 87 | + If not specified, one will be created from the ``error``. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + FirebaseError: A ``FirebaseError`` that can be raised to the user code. |
| 91 | + """ |
| 92 | + if isinstance(error, socket.timeout) or ( |
| 93 | + isinstance(error, socket.error) and 'timed out' in str(error)): |
| 94 | + return exceptions.DeadlineExceededError( |
| 95 | + message='Timed out while making an API call: {0}'.format(error), |
| 96 | + cause=error) |
| 97 | + if isinstance(error, httplib2.ServerNotFoundError): |
| 98 | + return exceptions.UnavailableError( |
| 99 | + message='Failed to establish a connection: {0}'.format(error), |
| 100 | + cause=error) |
| 101 | + if not isinstance(error, googleapiclient.errors.HttpError): |
| 102 | + return exceptions.UnknownError( |
| 103 | + message='Unknown error while making a remote service call: {0}'.format(error), |
| 104 | + cause=error) |
| 105 | + |
| 106 | + if not code: |
| 107 | + code = _utils._http_status_to_error_code(error.resp.status) # pylint: disable=protected-access |
| 108 | + if not message: |
| 109 | + message = str(error) |
| 110 | + if not http_response: |
| 111 | + http_response = _http_response_from_googleapiclient_error(error) |
| 112 | + |
| 113 | + err_type = _utils._error_code_to_exception_type(code) # pylint: disable=protected-access |
| 114 | + return err_type(message=message, cause=error, http_response=http_response) |
| 115 | + |
| 116 | + |
| 117 | +def _http_response_from_googleapiclient_error(error): |
| 118 | + """Creates a requests HTTP Response object from the given googleapiclient error.""" |
| 119 | + resp = requests.models.Response() |
| 120 | + resp.raw = io.BytesIO(error.content) |
| 121 | + resp.status_code = error.resp.status |
| 122 | + return resp |
0 commit comments