U
    hR                     @   s  U d Z ddlmZ ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
Z
ddlmZ dd	 Zz<ddlZzdd
lmZ W n ek
r   e  dZY nX W n ek
r   dZY nX i Zeeef ed< i Zeeef ed< i Zedk	rejD ]Zeeejd < qG dd deZG dd deZG dd deZG dd deZG dd deedZ G dd de Z!G dd de!Z"G dd de!Z#G dd de!Z$G d d! d!e!Z%G d"d# d#e Z&G d$d% d%e&Z'G d&d' d'e'Z(G d(d) d)e'Z)G d*d+ d+e'Z*G d,d- d-e&Z+G d.d/ d/e+Z,G d0d1 d1e&Z-G d2d3 d3e-Z.G d4d5 d5e&Z/G d6d7 d7e&Z0G d8d9 d9e&Z1G d:d; d;e1Z2G d<d= d=e1Z3G d>d? d?e&Z4G d@dA dAe&Z5G dBdC dCe&Z6G dDdE dEe&Z7G dFdG dGe7Z8G dHdI dIe&Z9G dJdK dKe Z:G dLdM dMe:Z;G dNdO dOe:Z<G dPdQ dQe:Z=G dRdS dSe:Z>G dTdU dUe:Z?G dVdW dWe:Z@G dXdY dYe:ZAG dZd[ d[eAZBG d\d] d]eCZDd^d_ ZEd`da ZFdbdc ZGdseHeHee dddedfZIdgdh ZJdidj ZKdkdl ZLdmdn ZMdodp ZNdqdr ZOdS )tzExceptions raised by Google API core & clients.

This module provides base classes for all errors raised by libraries based
on :mod:`google.api_core`, including both HTTP and gRPC clients.
    )absolute_import)unicode_literalsN)OptionalDict)Union)error_details_pb2c                   C   s   t dt d S )NzCPlease install grpcio-status to obtain helpful grpc error messages.)warningswarnImportWarning r   r   >/tmp/pip-unpacked-wheel-eraazoov/google/api_core/exceptions.py$_warn_could_not_import_grpcio_status    s    r   )
rpc_status_HTTP_CODE_TO_EXCEPTION_GRPC_CODE_TO_EXCEPTIONc                   @   s   e Zd ZdZdS )GoogleAPIErrorz;Base class for all exceptions raised by Google API Clients.N__name__
__module____qualname____doc__r   r   r   r   r   @   s   r   c                   @   s   e Zd ZdZdS )DuplicateCredentialArgsz,Raised when multiple credentials are passed.Nr   r   r   r   r   r   F   s   r   c                       s4   e Zd ZdZ fddZedd Zdd Z  ZS )
RetryErrorzRaised when a function has exhausted all of its available retries.

    Args:
        message (str): The exception message.
        cause (Exception): The last exception raised when retrying the
            function.
    c                    s    t t| | || _|| _d S N)superr   __init__message_cause)selfr   cause	__class__r   r   r   U   s    zRetryError.__init__c                 C   s   | j S )z5The last exception raised when retrying the function.)r   r   r   r   r   r   Z   s    zRetryError.causec                 C   s   d | j| jS )Nz{}, last exception: {})formatr   r   r"   r   r   r   __str___   s    zRetryError.__str__)	r   r   r   r   r   propertyr   r$   __classcell__r   r   r    r   r   L   s
   
r   c                   @   s   e Zd ZdZdd ZdS )_GoogleAPICallErrorMetaz8Metaclass for registering GoogleAPICallError subclasses.c                 C   sD   t | |||}|jd k	r(t|j| |jd k	r@t|j| |S r   )type__new__coder   
setdefaultgrpc_status_coder   )Zmcsnamebases
class_dictclsr   r   r   r)   f   s    

z_GoogleAPICallErrorMeta.__new__N)r   r   r   r   r)   r   r   r   r   r'   c   s   r'   c                       s   e Zd ZU dZdZeedf ed< dZd fdd	Z	dd Z
ed	d
 Zedd Zedd Zedd Zedd Zedd Z  ZS )GoogleAPICallErrora  Base class for exceptions raised by calling API methods.

    Args:
        message (str): The exception message.
        errors (Sequence[Any]): An optional list of error details.
        details (Sequence[Any]): An optional list of objects defined in google.rpc.error_details.
        response (Union[requests.Request, grpc.Call]): The response or
            gRPC call metadata.
        error_info (Union[error_details_pb2.ErrorInfo, None]): An optional object containing error info
            (google.rpc.error_details.ErrorInfo).
    Nr*   r   c                    s2   t t| | || _|| _|| _|| _|| _d S r   )r   r1   r   r   _errors_details	_response_error_info)r   r   errorsdetailsresponse
error_infor    r   r   r      s    zGoogleAPICallError.__init__c                 C   sV   d | j| j}| jr&d || j}n,| jrRdd | jD }|rRd |d|}|S )Nz{} {}c                 S   s2   g | ]*}t |d rt |dr|j d|j qS )r*   r   z: )hasattrr*   r   ).0errorr   r   r   
<listcomp>   s   
 
z.GoogleAPICallError.__str__.<locals>.<listcomp>
)r#   r*   r   r7   r6   join)r   	error_msgr6   r   r   r   r$      s    zGoogleAPICallError.__str__c                 C   s   | j r| j jS dS )zThe reason of the error.

        Reference:
            https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112

        Returns:
            Union[str, None]: An optional string containing reason of the error.
        N)r5   reasonr"   r   r   r   rA      s    
zGoogleAPICallError.reasonc                 C   s   | j r| j jS dS )a5  The logical grouping to which the "reason" belongs.

        Reference:
            https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112

        Returns:
            Union[str, None]: An optional string containing a logical grouping to which the "reason" belongs.
        N)r5   domainr"   r   r   r   rB      s    
zGoogleAPICallError.domainc                 C   s   | j r| j jS dS )a.  Additional structured details about this error.

        Reference:
            https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112

        Returns:
            Union[Dict[str, str], None]: An optional object containing structured details about the error.
        N)r5   metadatar"   r   r   r   rC      s    
zGoogleAPICallError.metadatac                 C   s
   t | jS )zuDetailed error information.

        Returns:
            Sequence[Any]: A list of additional error details.
        )listr2   r"   r   r   r   r6      s    zGoogleAPICallError.errorsc                 C   s
   t | jS )al  Information contained in google.rpc.status.details.

        Reference:
            https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto
            https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto

        Returns:
            Sequence[Any]: A list of structured objects from error_details.proto
        )rD   r3   r"   r   r   r   r7      s    zGoogleAPICallError.detailsc                 C   s   | j S )zYOptional[Union[requests.Request, grpc.Call]]: The response or
        gRPC call metadata.)r4   r"   r   r   r   r8      s    zGoogleAPICallError.response)r   r   NN)r   r   r   r   r*   r   int__annotations__r,   r   r$   r%   rA   rB   rC   r6   r7   r8   r&   r   r   r    r   r1   o   s"   
		




r1   )	metaclassc                   @   s   e Zd ZdZdS )Redirectionz8Base class for for all redirection (HTTP 3xx) responses.Nr   r   r   r   r   rH      s   rH   c                   @   s   e Zd ZdZejjZdS )MovedPermanentlyz7Exception mapping a ``301 Moved Permanently`` response.N)r   r   r   r   httpclientMOVED_PERMANENTLYr*   r   r   r   r   rI      s   rI   c                   @   s   e Zd ZdZejjZdS )NotModifiedz2Exception mapping a ``304 Not Modified`` response.N)r   r   r   r   rJ   rK   NOT_MODIFIEDr*   r   r   r   r   rM      s   rM   c                   @   s   e Zd ZdZejjZdS )TemporaryRedirectz8Exception mapping a ``307 Temporary Redirect`` response.N)r   r   r   r   rJ   rK   TEMPORARY_REDIRECTr*   r   r   r   r   rO      s   rO   c                   @   s   e Zd ZdZdZdS )ResumeIncompletezException mapping a ``308 Resume Incomplete`` response.

    .. note:: :attr:`http.client.PERMANENT_REDIRECT` is ``308``, but Google
        APIs differ in their use of this status code.
    i4  N)r   r   r   r   r*   r   r   r   r   rQ      s   rQ   c                   @   s   e Zd ZdZdS )ClientErrorz5Base class for all client error (HTTP 4xx) responses.Nr   r   r   r   r   rR     s   rR   c                   @   s   e Zd ZdZejjZdS )
BadRequestz1Exception mapping a ``400 Bad Request`` response.N)r   r   r   r   rJ   rK   BAD_REQUESTr*   r   r   r   r   rS     s   rS   c                   @   s$   e Zd ZdZedk	rejjndZdS )InvalidArgumentzCException mapping a :attr:`grpc.StatusCode.INVALID_ARGUMENT` error.N)r   r   r   r   grpc
StatusCodeZINVALID_ARGUMENTr,   r   r   r   r   rU     s   rU   c                   @   s$   e Zd ZdZedk	rejjndZdS )FailedPreconditionzJException mapping a :attr:`grpc.StatusCode.FAILED_PRECONDITION`
    error.N)r   r   r   r   rV   rW   ZFAILED_PRECONDITIONr,   r   r   r   r   rX     s   rX   c                   @   s$   e Zd ZdZedk	rejjndZdS )
OutOfRangez?Exception mapping a :attr:`grpc.StatusCode.OUT_OF_RANGE` error.N)r   r   r   r   rV   rW   ZOUT_OF_RANGEr,   r   r   r   r   rY     s   rY   c                   @   s   e Zd ZdZejjZdS )Unauthorizedz2Exception mapping a ``401 Unauthorized`` response.N)r   r   r   r   rJ   rK   UNAUTHORIZEDr*   r   r   r   r   rZ   $  s   rZ   c                   @   s$   e Zd ZdZedk	rejjndZdS )UnauthenticatedzBException mapping a :attr:`grpc.StatusCode.UNAUTHENTICATED` error.N)r   r   r   r   rV   rW   ZUNAUTHENTICATEDr,   r   r   r   r   r\   *  s   r\   c                   @   s   e Zd ZdZejjZdS )	Forbiddenz/Exception mapping a ``403 Forbidden`` response.N)r   r   r   r   rJ   rK   	FORBIDDENr*   r   r   r   r   r]   0  s   r]   c                   @   s$   e Zd ZdZedk	rejjndZdS )PermissionDeniedzDException mapping a :attr:`grpc.StatusCode.PERMISSION_DENIED` error.N)r   r   r   r   rV   rW   ZPERMISSION_DENIEDr,   r   r   r   r   r_   6  s   r_   c                   @   s,   e Zd ZdZejjZedk	r$ej	jndZ
dS )NotFoundz`Exception mapping a ``404 Not Found`` response or a
    :attr:`grpc.StatusCode.NOT_FOUND` error.N)r   r   r   r   rJ   rK   	NOT_FOUNDr*   rV   rW   r,   r   r   r   r   r`   <  s   r`   c                   @   s   e Zd ZdZejjZdS )MethodNotAllowedz8Exception mapping a ``405 Method Not Allowed`` response.N)r   r   r   r   rJ   rK   METHOD_NOT_ALLOWEDr*   r   r   r   r   rb   D  s   rb   c                   @   s   e Zd ZdZejjZdS )Conflictz.Exception mapping a ``409 Conflict`` response.N)r   r   r   r   rJ   rK   CONFLICTr*   r   r   r   r   rd   J  s   rd   c                   @   s$   e Zd ZdZedk	rejjndZdS )AlreadyExistszAException mapping a :attr:`grpc.StatusCode.ALREADY_EXISTS` error.N)r   r   r   r   rV   rW   ZALREADY_EXISTSr,   r   r   r   r   rf   P  s   rf   c                   @   s$   e Zd ZdZedk	rejjndZdS )Abortedz:Exception mapping a :attr:`grpc.StatusCode.ABORTED` error.N)r   r   r   r   rV   rW   ZABORTEDr,   r   r   r   r   rg   V  s   rg   c                   @   s   e Zd ZdZejjZdS )LengthRequiredz5Exception mapping a ``411 Length Required`` response.N)r   r   r   r   rJ   rK   LENGTH_REQUIREDr*   r   r   r   r   rh   \  s   rh   c                   @   s   e Zd ZdZejjZdS )PreconditionFailedz9Exception mapping a ``412 Precondition Failed`` response.N)r   r   r   r   rJ   rK   PRECONDITION_FAILEDr*   r   r   r   r   rj   b  s   rj   c                   @   s   e Zd ZdZejjZdS )RequestRangeNotSatisfiablezCException mapping a ``416 Request Range Not Satisfiable`` response.N)r   r   r   r   rJ   rK   REQUESTED_RANGE_NOT_SATISFIABLEr*   r   r   r   r   rl   h  s   rl   c                   @   s   e Zd ZdZejjZdS )TooManyRequestsz7Exception mapping a ``429 Too Many Requests`` response.N)r   r   r   r   rJ   rK   TOO_MANY_REQUESTSr*   r   r   r   r   rn   n  s   rn   c                   @   s$   e Zd ZdZedk	rejjndZdS )ResourceExhaustedzEException mapping a :attr:`grpc.StatusCode.RESOURCE_EXHAUSTED` error.N)r   r   r   r   rV   rW   ZRESOURCE_EXHAUSTEDr,   r   r   r   r   rp   t  s   rp   c                   @   s(   e Zd ZdZdZedk	r ejjndZdS )	Cancelledz<Exception mapping a :attr:`grpc.StatusCode.CANCELLED` error.i  N)	r   r   r   r   r*   rV   rW   Z	CANCELLEDr,   r   r   r   r   rq   z  s   rq   c                   @   s   e Zd ZdZdS )ServerErrorzBase for 5xx responses.Nr   r   r   r   r   rr     s   rr   c                   @   s,   e Zd ZdZejjZedk	r$ej	j
ndZdS )InternalServerErrorzlException mapping a ``500 Internal Server Error`` response. or a
    :attr:`grpc.StatusCode.INTERNAL` error.N)r   r   r   r   rJ   rK   INTERNAL_SERVER_ERRORr*   rV   rW   ZINTERNALr,   r   r   r   r   rs     s   rs   c                   @   s$   e Zd ZdZedk	rejjndZdS )Unknownz:Exception mapping a :attr:`grpc.StatusCode.UNKNOWN` error.N)r   r   r   r   rV   rW   UNKNOWNr,   r   r   r   r   ru     s   ru   c                   @   s$   e Zd ZdZedk	rejjndZdS )DataLossz<Exception mapping a :attr:`grpc.StatusCode.DATA_LOSS` error.N)r   r   r   r   rV   rW   Z	DATA_LOSSr,   r   r   r   r   rw     s   rw   c                   @   s,   e Zd ZdZejjZedk	r$ej	j
ndZdS )MethodNotImplementedzjException mapping a ``501 Not Implemented`` response or a
    :attr:`grpc.StatusCode.UNIMPLEMENTED` error.N)r   r   r   r   rJ   rK   NOT_IMPLEMENTEDr*   rV   rW   ZUNIMPLEMENTEDr,   r   r   r   r   rx     s   rx   c                   @   s   e Zd ZdZejjZdS )
BadGatewayz1Exception mapping a ``502 Bad Gateway`` response.N)r   r   r   r   rJ   rK   BAD_GATEWAYr*   r   r   r   r   rz     s   rz   c                   @   s,   e Zd ZdZejjZedk	r$ej	j
ndZdS )ServiceUnavailablezlException mapping a ``503 Service Unavailable`` response or a
    :attr:`grpc.StatusCode.UNAVAILABLE` error.N)r   r   r   r   rJ   rK   SERVICE_UNAVAILABLEr*   rV   rW   UNAVAILABLEr,   r   r   r   r   r|     s   r|   c                   @   s   e Zd ZdZejjZdS )GatewayTimeoutz5Exception mapping a ``504 Gateway Timeout`` response.N)r   r   r   r   rJ   rK   GATEWAY_TIMEOUTr*   r   r   r   r   r     s   r   c                   @   s$   e Zd ZdZedk	rejjndZdS )DeadlineExceededzDException mapping a :attr:`grpc.StatusCode.DEADLINE_EXCEEDED` error.N)r   r   r   r   rV   rW   ZDEADLINE_EXCEEDEDr,   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )"AsyncRestUnsupportedParameterErrorzPRaised when an unsupported parameter is configured against async rest transport.Nr   r   r   r   r   r     s   r   c                 C   s   t | tS )zReturn the exception class for a specific HTTP status code.

    Args:
        status_code (int): The HTTP status code.

    Returns:
        :func:`type`: the appropriate subclass of :class:`GoogleAPICallError`.
    )r   getr1   status_coder   r   r   exception_class_for_http_status  s    	r   c                 K   s(   t | }||f|}|jdkr$| |_|S )a  Create a :class:`GoogleAPICallError` from an HTTP status code.

    Args:
        status_code (int): The HTTP status code.
        message (str): The exception message.
        kwargs: Additional arguments passed to the :class:`GoogleAPICallError`
            constructor.

    Returns:
        GoogleAPICallError: An instance of the appropriate subclass of
            :class:`GoogleAPICallError`.
    N)r   r*   r   r   kwargsZerror_classr<   r   r   r   from_http_status  s
    
r   c                 C   s$   |r|  nd }dj||| d}|S )Nz{method} {url}: {error})methodurlr<   )upperr#   )r<   r   r   r   r   r   r   _format_rest_error_message  s    r   )r   r   payloadc                 C   s   |si n|}| di  dd}| di  dd}| di  dd}ttdd |}|rf|d	 nd
}t|||}	t| j|	||| |d}
|
S )a  Create a :class:`GoogleAPICallError` from a google auth rest response.

    Args:
        response Union[google.auth.transport.Response, google.auth.aio.transport.Response]: The HTTP response.
        method Optional(str): The HTTP request method.
        url Optional(str): The HTTP request url.
        payload Optional(dict): The HTTP response payload. If not passed in, it is read from response for a response type of google.auth.transport.Response.

    Returns:
        GoogleAPICallError: An instance of the appropriate subclass of
            :class:`GoogleAPICallError`, with the message and errors populated
            from the response.
    r<   r   unknown errorr6   r   r7   c                 S   s   |  dddkS )Nz@type z(type.googleapis.com/google.rpc.ErrorInfo)r   Zdetailr   r   r   <lambda>
  s   
z,format_http_response_error.<locals>.<lambda>r   Nr6   r7   r8   r9   )r   rD   filterr   r   r   )r8   r   r   r   error_messager6   r7   Zerror_info_listr9   r   	exceptionr   r   r   format_http_response_error  s*    r   c                 C   sJ   z|   }W n& tk
r2   dd| jp(dii}Y nX t| | jj| jj|S )aS  Create a :class:`GoogleAPICallError` from a :class:`requests.Response`.

    Args:
        response (requests.Response): The HTTP response.

    Returns:
        GoogleAPICallError: An instance of the appropriate subclass of
            :class:`GoogleAPICallError`, with the message and errors populated
            from the response.
    r<   r   r   )json
ValueErrortextr   requestr   r   )r8   r   r   r   r   from_http_response  s       r   c                 C   s   t | tS )zReturn the exception class for a specific :class:`grpc.StatusCode`.

    Args:
        status_code (grpc.StatusCode): The gRPC status code.

    Returns:
        :func:`type`: the appropriate subclass of :class:`GoogleAPICallError`.
    )r   r   r1   r   r   r   r   exception_class_for_grpc_status1  s    	r   c                 K   s>   t | trt| | } t| }||f|}|jdkr:| |_|S )a  Create a :class:`GoogleAPICallError` from a :class:`grpc.StatusCode`.

    Args:
        status_code (Union[grpc.StatusCode, int]): The gRPC status code.
        message (str): The exception message.
        kwargs: Additional arguments passed to the :class:`GoogleAPICallError`
            constructor.

    Returns:
        GoogleAPICallError: An instance of the appropriate subclass of
            :class:`GoogleAPICallError`.
    N)
isinstancerE   _INT_TO_GRPC_CODEr   r   r,   r   r   r   r   from_grpc_status=  s    

r   c                 C   s   t | dot | dS )Nr*   r7   )r:   )rpc_excr   r   r   _is_informative_grpc_errorW  s    r   c              
      s   t st  g d fS zt | }W n tk
r<   g d f Y S X |sJg d fS tjtjtjtjtj	tj
tjtjtjtjg
}d }g }|jD ]Z tt fdd|}t|dkr }n|d  } | || t|tjr|}q||fS )Nc                    s     | jS r   )ZIsZ
DESCRIPTOR)xr   r   r   r   w      z+_parse_grpc_error_details.<locals>.<lambda>r   )r   r   Z	from_callNotImplementedErrorr   rS   ZPreconditionFailureZQuotaFailureZ	ErrorInfoZ	RetryInfoZResourceInfoZRequestInfoZ	DebugInfoZHelpZLocalizedMessager7   rD   r   lenZUnpackappendr   )r   statusZpossible_errorsr9   Zerror_detailsZmatched_detail_clsinfor   r   r   _parse_grpc_error_details[  sD    



r   c                 C   s^   t dk	rt| t jst| rFt| \}}t|  |  | f|| |dS tt	| | f| dS dS )a  Create a :class:`GoogleAPICallError` from a :class:`grpc.RpcError`.

    Args:
        rpc_exc (grpc.RpcError): The gRPC error.

    Returns:
        GoogleAPICallError: An instance of the appropriate subclass of
            :class:`GoogleAPICallError`.
    Nr   )r6   r8   )
rV   r   ZCallr   r   r   r*   r7   r1   str)r   r7   Zerr_infor   r   r   from_grpc_error  s     
	r   )N)Pr   
__future__r   r   http.clientrJ   typingr   r   r   r   Z
google.rpcr   r   rV   Zgrpc_statusr   ImportErrorr   rE   	ExceptionrF   r   r   rW   r   valuer   r   r   r(   r'   r1   rH   rI   rM   rO   rQ   rR   rS   rU   rX   rY   rZ   r\   r]   r_   r`   rb   rd   rf   rg   rh   rj   rl   rn   rp   rq   rr   rs   ru   rw   rx   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   


x
	   **