B
    uf~K                 @   s  U d Z ddlmZ ddlmZ ddlZddlmZ ddlmZ ddl	Z	ddl
mZ yBddlZyddlmZ W n$ ek
r   e	d	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xejD ]Zeeejd < qW 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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 d/d0 d0e&Z-G d1d2 d2e-Z.G d3d4 d4e&Z/G d5d6 d6e&Z0G d7d8 d8e&Z1G d9d: d:e1Z2G d;d< d<e1Z3G d=d> d>e&Z4G d?d@ d@e&Z5G dAdB dBe&Z6G dCdD dDe&Z7G dEdF dFe7Z8G dGdH dHe&Z9G dIdJ dJe Z:G dKdL dLe:Z;G dMdN dNe:Z<G dOdP dPe:Z=G dQdR dRe:Z>G dSdT dTe:Z?G dUdV dVe:Z@G dWdX dXe:ZAG dYdZ dZeAZBd[d\ ZCd]d^ ZDd_d` ZEdadb ZFdcdd ZGdedf ZHdgdh ZIdidj ZJdS )kzExceptions 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)Dict)Union)error_details_pb2)
rpc_statuszCPlease install grpcio-status to obtain helpful grpc error messages._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   \/home/ankuromar296_gmail_com/myenv/lib/python3.7/site-packages/google/api_core/exceptions.pyr
   ;   s   r
   c               @   s   e Zd ZdZdS )DuplicateCredentialArgsz,Raised when multiple credentials are passed.N)r   r   r   r   r   r   r   r   r   A   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   P   s    zRetryError.__init__c             C   s   | j S )z5The last exception raised when retrying the function.)r   )r   r   r   r   r   U   s    zRetryError.causec             C   s   d | j| jS )Nz{}, last exception: {})formatr   r   )r   r   r   r   __str__Z   s    zRetryError.__str__)	r   r   r   r   r   propertyr   r   __classcell__r   r   )r   r   r   G   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 )N)type__new__coder   
setdefaultgrpc_status_coder	   )mcsnamebases
class_dictclsr   r   r   r    a   s    

z_GoogleAPICallErrorMeta.__new__N)r   r   r   r   r    r   r   r   r   r   ^   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 )N)r   r)   r   r   _errors_details	_response_error_info)r   r   errorsdetailsresponse
error_info)r   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   r/   r.   join)r   	error_msgr.   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)r-   reason)r   r   r   r   r9      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)r-   domain)r   r   r   r   r:      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)r-   metadata)r   r   r   r   r;      s    
zGoogleAPICallError.metadatac             C   s
   t | jS )zuDetailed error information.

        Returns:
            Sequence[Any]: A list of additional error details.
        )listr*   )r   r   r   r   r.      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
        )r<   r+   )r   r   r   r   r/      s    zGoogleAPICallError.detailsc             C   s   | j S )zYOptional[Union[requests.Request, grpc.Call]]: The response or
        gRPC call metadata.)r,   )r   r   r   r   r0      s    zGoogleAPICallError.response)r   r   NN)r   r   r   r   r!   r   int__annotations__r#   r   r   r   r9   r:   r;   r.   r/   r0   r   r   r   )r   r   r)   j   s   
			r)   )	metaclassc               @   s   e Zd ZdZdS )Redirectionz8Base class for for all redirection (HTTP 3xx) responses.N)r   r   r   r   r   r   r   r   r@      s   r@   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   rA      s   rA   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   rB   rC   NOT_MODIFIEDr!   r   r   r   r   rE      s   rE   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   rB   rC   TEMPORARY_REDIRECTr!   r   r   r   r   rG      s   rG   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   rI      s   rI   c               @   s   e Zd ZdZdS )ClientErrorz5Base class for all client error (HTTP 4xx) responses.N)r   r   r   r   r   r   r   r   rJ     s   rJ   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   rB   rC   BAD_REQUESTr!   r   r   r   r   rK     s   rK   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   rM     s   rM   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   rN   rO   ZFAILED_PRECONDITIONr#   r   r   r   r   rP     s   rP   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   rN   rO   ZOUT_OF_RANGEr#   r   r   r   r   rQ     s   rQ   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   rB   rC   UNAUTHORIZEDr!   r   r   r   r   rR     s   rR   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   rN   rO   ZUNAUTHENTICATEDr#   r   r   r   r   rT   %  s   rT   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   rB   rC   	FORBIDDENr!   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 )PermissionDeniedzDException mapping a :attr:`grpc.StatusCode.PERMISSION_DENIED` error.N)r   r   r   r   rN   rO   ZPERMISSION_DENIEDr#   r   r   r   r   rW   1  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 )NotFoundz`Exception mapping a ``404 Not Found`` response or a
    :attr:`grpc.StatusCode.NOT_FOUND` error.N)r   r   r   r   rB   rC   	NOT_FOUNDr!   rN   rO   r#   r   r   r   r   rX   7  s   rX   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   rB   rC   METHOD_NOT_ALLOWEDr!   r   r   r   r   rZ   ?  s   rZ   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   rB   rC   CONFLICTr!   r   r   r   r   r\   E  s   r\   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   rN   rO   ZALREADY_EXISTSr#   r   r   r   r   r^   K  s   r^   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   rN   rO   ZABORTEDr#   r   r   r   r   r_   Q  s   r_   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   rB   rC   LENGTH_REQUIREDr!   r   r   r   r   r`   W  s   r`   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   rB   rC   PRECONDITION_FAILEDr!   r   r   r   r   rb   ]  s   rb   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   rB   rC   REQUESTED_RANGE_NOT_SATISFIABLEr!   r   r   r   r   rd   c  s   rd   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   rB   rC   TOO_MANY_REQUESTSr!   r   r   r   r   rf   i  s   rf   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   rN   rO   ZRESOURCE_EXHAUSTEDr#   r   r   r   r   rh   o  s   rh   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!   rN   rO   	CANCELLEDr#   r   r   r   r   ri   u  s   ri   c               @   s   e Zd ZdZdS )ServerErrorzBase for 5xx responses.N)r   r   r   r   r   r   r   r   rk   ~  s   rk   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   rB   rC   INTERNAL_SERVER_ERRORr!   rN   rO   ZINTERNALr#   r   r   r   r   rl     s   rl   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   rN   rO   UNKNOWNr#   r   r   r   r   rn     s   rn   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   rN   rO   Z	DATA_LOSSr#   r   r   r   r   rp     s   rp   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   rB   rC   NOT_IMPLEMENTEDr!   rN   rO   ZUNIMPLEMENTEDr#   r   r   r   r   rq     s   rq   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   rB   rC   BAD_GATEWAYr!   r   r   r   r   rs     s   rs   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   rB   rC   SERVICE_UNAVAILABLEr!   rN   rO   UNAVAILABLEr#   r   r   r   r   ru     s   ru   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   rB   rC   GATEWAY_TIMEOUTr!   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 )DeadlineExceededzDException mapping a :attr:`grpc.StatusCode.DEADLINE_EXCEEDED` error.N)r   r   r   r   rN   rO   ZDEADLINE_EXCEEDEDr#   r   r   r   r   rz     s   rz   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   getr)   )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   kwargserror_classr4   r   r   r   from_http_status  s
    
r   c             C   s   y|   }W n& tk
r2   dd| jp(dii}Y nX |di dd}|di dd}|di dd}ttdd |}|r|d	 nd
}dj| jj| jj	|d}t
| j|||| |d}|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.
    r4   r   zunknown errorr.   r   r/   c             S   s   |  dddkS )Nz@type z(type.googleapis.com/google.rpc.ErrorInfo)r{   )detailr   r   r   <lambda>  s   
z$from_http_response.<locals>.<lambda>r   Nz{method} {url}: {error})methodurlr4   )r.   r/   r0   r1   )json
ValueErrortextr{   r<   filterr   requestr   r   r   r|   )r0   payloaderror_messager.   r/   r1   r   	exceptionr   r   r   from_http_response  s0    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{   r)   )r|   r   r   r   exception_class_for_grpc_status  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)
isinstancer=   _INT_TO_GRPC_CODEr{   r   r#   )r|   r   r~   r   r4   r   r   r   from_grpc_status  s    

r   c             C   s   t | dot | dS )Nr!   r/   )r2   )rpc_excr   r   r   _is_informative_grpc_error.  s    r   c          
      s   yt | }W n tk
r&   g d fS X |s4g 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 }xd|jD ]Z tt fdd|}t|dkr }n|d  } | || t|tjrp|}qpW ||fS )Nc                s     | jS )N)IsZ
DESCRIPTOR)x)r   r   r   r   K      z+_parse_grpc_error_details.<locals>.<lambda>r   )r   Z	from_callNotImplementedErrorr   rK   ZPreconditionFailureZQuotaFailureZ	ErrorInfoZ	RetryInfoZResourceInfoZRequestInfoZ	DebugInfoZHelpZLocalizedMessager/   r<   r   lenUnpackappendr   )r   statusZpossible_errorsr1   Zerror_detailsZmatched_detail_clsinfor   )r   r   _parse_grpc_error_details2  s:    



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`.
    N)r.   r/   r0   r1   )r.   r0   )
rN   r   Callr   r   r   r!   r/   r)   str)r   r/   Zerr_infor   r   r   from_grpc_errorY  s    r   )Kr   
__future__r   r   http.clientrB   typingr   r   warningsZ
google.rpcr   rN   Zgrpc_statusr   ImportErrorwarnImportWarningr   r=   	Exceptionr>   r	   r   rO   r   valuer
   r   r   r   r   r)   r@   rA   rE   rG   rI   rJ   rK   rM   rP   rQ   rR   rT   rU   rW   rX   rZ   r\   r^   r_   r`   rb   rd   rf   rh   ri   rk   rl   rn   rp   rq   rs   ru   rx   rz   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   s   

x
	.'