o
    i                     @  s   d dl mZ d dlmZ d dlmZ d dlmZmZ edddZ		 eddd	Z
	 ed
Z	 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eddG dd dZeddG dd dZdS )    )annotations)	dataclass)Enum)OptionalTypeVarInputTT)contravariantOutputT)	covariantServiceHandlerTServiceTc                      sR   e Zd ZdZddd fd
dZedddZedddZedddZ  Z	S )HandlerErrora  
    A Nexus handler error.

    This exception is used to represent errors that occur during the handling of a
    Nexus operation that should be reported to the caller as a handler error.

    Example:
        .. code-block:: python

            import nexusrpc

            # Raise a bad request error
            raise nexusrpc.HandlerError(
                "Invalid input provided",
                type=nexusrpc.HandlerErrorType.BAD_REQUEST
            )

            # Raise a retryable internal error
            raise nexusrpc.HandlerError(
                "Database unavailable",
                type=nexusrpc.HandlerErrorType.INTERNAL,
                retryable=True
            )
    N)retryable_overridemessagestrtypeHandlerErrorTyper   Optional[bool]c                  s   t  | || _|| _dS )a  
        Initialize a new HandlerError.

        :param message: A descriptive message for the error. This will become
                        the `message` in the resulting Nexus Failure object.

        :param type: The :py:class:`HandlerErrorType` of the error.

        :param retryable_override: Optionally set whether the error should be
                                   retried. By default, the error type is used
                                   to determine this.
        N)super__init___type_retryable_override)selfr   r   r   	__class__ V/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/nexusrpc/_common.pyr   .   s   
zHandlerError.__init__returnc                 C     | j S )zU
        The optional retryability override set when this error was created.
        )r   r   r   r   r   r   E      zHandlerError.retryable_overrideboolc                 C  s\   | j dur| j S tjtjtjtjtjh}tjtjtj	tj
h}| j|v r%dS | j|v r,dS dS )a  
        Whether this error should be retried.

        If :py:attr:`retryable_override` is None, then the default behavior for the
        error type is used. See
        https://github.com/nexus-rpc/api/blob/main/SPEC.md#predefined-handler-errors
        NFT)r   r   BAD_REQUESTUNAUTHENTICATEDUNAUTHORIZED	NOT_FOUNDNOT_IMPLEMENTEDRESOURCE_EXHAUSTEDINTERNALUNAVAILABLEUPSTREAM_TIMEOUTr   )r   non_retryable_typesretryable_typesr   r   r   	retryableL   s$   
	

zHandlerError.retryablec                 C  r   )z
        The type of handler error.

        See :py:class:`HandlerErrorType` and
        https://github.com/nexus-rpc/api/blob/main/SPEC.md#predefined-handler-errors.
        )r   r   r   r   r   r   l   s   zHandlerError.type)r   r   r   r   r   r   )r   r   )r   r!   )r   r   )
__name__
__module____qualname____doc__r   propertyr   r-   r   __classcell__r   r   r   r   r      s    r   c                   @  sD   e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 d	Z	 d
ZdS )r   zuNexus handler error types.

    See https://github.com/nexus-rpc/api/blob/main/SPEC.md#predefined-handler-errors
    r"   r#   r$   r%   r'   r(   r&   r)   r*   N)r.   r/   r0   r1   r"   r#   r$   r%   r'   r(   r&   r)   r*   r   r   r   r   r   w   s(    r   c                      s0   e Zd ZdZd fddZedd	d
Z  ZS )OperationErrora  
    An error that represents "failed" and "canceled" operation results.

    :param message: A descriptive message for the error. This will become the
                    `message` in the resulting Nexus Failure object.

    :param state:

    Example:
        .. code-block:: python

            import nexusrpc

            # Indicate operation failed
            raise nexusrpc.OperationError(
                "Processing failed due to invalid data",
                state=nexusrpc.OperationErrorState.FAILED
            )

            # Indicate operation was canceled
            raise nexusrpc.OperationError(
                "Operation was canceled by user request",
                state=nexusrpc.OperationErrorState.CANCELED
            )
    r   r   stateOperationErrorStatec                  s   t  | || _d S )N)r   r   _state)r   r   r5   r   r   r   r      s   
zOperationError.__init__r   c                 C  r   )z-
        The state of the operation.
        )r7   r   r   r   r   r5      r    zOperationError.state)r   r   r5   r6   )r   r6   )r.   r/   r0   r1   r   r2   r5   r3   r   r   r   r   r4      s
    r4   c                   @  s&   e Zd ZdZdZ	 dZ	 dZ	 dZdS )OperationStatez6
    Describes the current state of an operation.
    running	succeededfailedcanceledN)r.   r/   r0   r1   RUNNING	SUCCEEDEDFAILEDCANCELEDr   r   r   r   r8      s    r8   c                   @  s   e Zd ZdZdZ	 dZdS )r6   zR
    The state of an operation as described by an :py:class:`OperationError`.
    r;   r<   N)r.   r/   r0   r1   r?   r@   r   r   r   r   r6      s    r6   )frozenc                   @  s$   e Zd ZU dZded< 	 ded< dS )OperationInfoz)
    Information about an operation.
    r   tokenr8   r5   Nr.   r/   r0   r1   __annotations__r   r   r   r   rB     s   
 rB   c                   @  s$   e Zd ZU dZded< 	 ded< dS )Linkz
    A Link contains a URL and a type that can be used to decode the URL.

    The URL may contain arbitrary data (percent-encoded). It can be used to pass
    information about the caller to the handler, or vice versa.
    r   urlr   NrD   r   r   r   r   rF     s   
 rF   N)
__future__r   dataclassesr   enumr   typingr   r   r   r	   r   r   	Exceptionr   r   r4   r8   r6   rB   rF   r   r   r   r   <module>   s*    cF'