o
    il5                     @   sJ  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlZddlZddl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d deZG d d! d!eZd"ed#efd$d%Z dS )&zCommon Temporal exceptions.    N)	timedelta)IntEnum)AnyOptionalSequenceTuplec                   @   s&   e Zd ZdZedee fddZdS )TemporalErrorz!Base for all Temporal exceptions.returnc                 C      | j S )zVCause of the exception.

        This is the same as ``Exception.__cause__``.
        )	__cause__self r   [/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/exceptions.pycause   s   zTemporalError.causeN)__name__
__module____qualname____doc__propertyr   BaseExceptionr   r   r   r   r   r      s    r   c                	       sz   e Zd ZdZddddedeejjj	j
 dee ddf fdd	Zedefd
dZedeejjj	j
 fddZ  ZS )FailureErrorz=Base for runtime failures during workflow/activity execution.N)failureexc_argsmessager   r   r	   c                   s*   |du r|f}t  j|  || _|| _dS )zInitialize a failure error.N)super__init___message_failure)r   r   r   r   	__class__r   r   r      s
   
zFailureError.__init__c                 C   r
   )zMessage.)r   r   r   r   r   r   *      zFailureError.messagec                 C   r
   )z#Underlying protobuf failure object.)r   r   r   r   r   r   /   r!   zFailureError.failure)r   r   r   r   strr   
temporalioapir   v1Failurer   r   r   r   __classcell__r   r   r   r   r      s"    $r   c                	       s<   e Zd ZdZdddededee ddf fdd	Z  ZS )
WorkflowAlreadyStartedErroraL  Thrown by a client or workflow when a workflow execution has already started.

    Attributes:
        workflow_id: ID of the already-started workflow.
        workflow_type: Workflow type name of the already-started workflow.
        run_id: Run ID of the already-started workflow if this was raised by the
            client.
    N)run_idworkflow_idworkflow_typer)   r	   c                   s"   t  d || _|| _|| _dS )z,Initialize a workflow already started error.z"Workflow execution already startedN)r   r   r*   r+   r)   )r   r*   r+   r)   r   r   r   r   ?   s   
z$WorkflowAlreadyStartedError.__init__)r   r   r   r   r"   r   r   r'   r   r   r   r   r(   5   s    
r(   c                   @   s4   e Zd ZdZeejjjj	j
Zeejjjj	jZdS )ApplicationErrorCategoryziSeverity category for your application error. Maps to corresponding client-side logging/metrics behaviorsN)r   r   r   r   intr#   r$   enumsr%   r,   &APPLICATION_ERROR_CATEGORY_UNSPECIFIEDUNSPECIFIED!APPLICATION_ERROR_CATEGORY_BENIGNBENIGNr   r   r   r   r,   I   s    r,   c                       s   e Zd ZdZdddejddededee de	d	ee
 d
eddf fddZedee fddZedee fddZede	fddZedee
 fddZedefddZ  ZS )ApplicationErrorz0Error raised during workflow/activity execution.NF)typenon_retryablenext_retry_delaycategoryr   detailsr4   r5   r6   r7   r	   c                   sF   t  j||s|n| d| fd || _|| _|| _|| _|| _dS )z Initialize an application error.z: )r   N)r   r   _details_type_non_retryable_next_retry_delay	_category)r   r   r4   r5   r6   r7   r8   r   r   r   r   Y   s   

zApplicationError.__init__c                 C   r
   z"User-defined details on the error.r9   r   r   r   r   r8   n   r!   zApplicationError.detailsc                 C   r
   )zGeneral error type.r:   r   r   r   r   r4   s   r!   zApplicationError.typec                 C   r
   )a  Whether the error was set as non-retryable when created.

        Note: This is not whether the error is non-retryable via other means
        such as retry policy. This is just whether the error was marked
        non-retryable upon creation by the user.
        r;   r   r   r   r   r5   x   s   zApplicationError.non_retryablec                 C   r
   )zDelay before the next activity retry attempt.

        User activity code may set this when raising ApplicationError to specify
        a delay before the next activity retry.
        )r<   r   r   r   r   r6      s   z!ApplicationError.next_retry_delayc                 C   r
   )z*Severity category of the application error)r=   r   r   r   r   r7      r!   zApplicationError.category)r   r   r   r   r,   r0   r"   r   r   boolr   r   r   r   r8   r4   r5   r6   r7   r'   r   r   r   r   r3   V   s>    	r3   c                       sF   e Zd ZdZddededdf fddZedee fd	d
Z	  Z
S )CancelledErrorz/Error raised on workflow/activity cancellation.	Cancelledr   r8   r	   Nc                       t  | || _dS )zInitialize a cancelled error.Nr   r   r9   r   r   r8   r   r   r   r         
zCancelledError.__init__c                 C   r
   r>   r?   r   r   r   r   r8      r!   zCancelledError.details)rD   r   r   r   r   r"   r   r   r   r   r8   r'   r   r   r   r   rC      s
    rC   c                       sD   e Zd ZdZdededdf fddZedee fdd	Z	  Z
S )
TerminatedErrorz&Error raised on workflow cancellation.r   r8   r	   Nc                    rE   )zInitialize a terminated error.NrF   rG   r   r   r   r      rH   zTerminatedError.__init__c                 C   r
   r>   r?   r   r   r   r   r8      r!   zTerminatedError.detailsrI   r   r   r   r   rJ      s
    rJ   c                   @   sX   e Zd ZdZeejjjj	j
Zeejjjj	jZeejjjj	jZeejjjj	jZdS )TimeoutTypez-Type of timeout for :py:class:`TimeoutError`.N)r   r   r   r   r-   r#   r$   r.   r%   rK   TIMEOUT_TYPE_START_TO_CLOSESTART_TO_CLOSETIMEOUT_TYPE_SCHEDULE_TO_STARTSCHEDULE_TO_STARTTIMEOUT_TYPE_SCHEDULE_TO_CLOSESCHEDULE_TO_CLOSETIMEOUT_TYPE_HEARTBEAT	HEARTBEATr   r   r   r   rK      s    rK   c                       sf   e Zd ZdZdedee dee ddf fddZ	e
dee fd	d
Ze
dee fddZ  ZS )TimeoutErrorz*Error raised on workflow/activity timeout.r   r4   last_heartbeat_detailsr	   Nc                   s   t  | || _|| _dS )zInitialize a timeout error.N)r   r   r:   _last_heartbeat_details)r   r   r4   rU   r   r   r   r      s   
zTimeoutError.__init__c                 C   r
   )zType of timeout error.r@   r   r   r   r   r4      r!   zTimeoutError.typec                 C   r
   )z<Last heartbeat details if this is for an activity heartbeat.)rV   r   r   r   r   rU      r!   z#TimeoutError.last_heartbeat_details)r   r   r   r   r"   r   rK   r   r   r   r   r4   rU   r'   r   r   r   r   rT      s    rT   c                       sF   e Zd ZdZdddededdf fdd	Zedefd
dZ  Z	S )ServerErrorz)Error originating in the Temporal server.F)r5   r   r5   r	   Nc                   rE   )zInitialize a server error.N)r   r   r;   )r   r   r5   r   r   r   r      rH   zServerError.__init__c                 C   r
   )z$Whether this error is non-retryable.rA   r   r   r   r   r5      r!   zServerError.non_retryable)
r   r   r   r   r"   rB   r   r   r5   r'   r   r   r   r   rW      s
     rW   c                   @   s   e Zd ZdZeejjjj	j
Zeejjjj	jZeejjjj	jZeejjjj	jZeejjjj	jZeejjjj	jZeejjjj	jZdS )
RetryStatez:Current retry state of the workflow/activity during error.N)r   r   r   r   r-   r#   r$   r.   r%   rX   RETRY_STATE_IN_PROGRESSIN_PROGRESS!RETRY_STATE_NON_RETRYABLE_FAILURENON_RETRYABLE_FAILURERETRY_STATE_TIMEOUTTIMEOUT$RETRY_STATE_MAXIMUM_ATTEMPTS_REACHEDMAXIMUM_ATTEMPTS_REACHED RETRY_STATE_RETRY_POLICY_NOT_SETRETRY_POLICY_NOT_SET!RETRY_STATE_INTERNAL_SERVER_ERRORINTERNAL_SERVER_ERRORRETRY_STATE_CANCEL_REQUESTEDCANCEL_REQUESTEDr   r   r   r   rX      s&    rX   c                       s   e Zd ZdZdededededededee d	d
f fddZe	d	efddZ
e	d	efddZe	d	efddZe	d	efddZe	d	efddZe	d	ee fddZ  ZS )ActivityErrorz!Error raised on activity failure.r   scheduled_event_idstarted_event_ididentityactivity_typeactivity_idretry_stater	   Nc                   s4   t  | || _|| _|| _|| _|| _|| _dS )zInitialize an activity error.N)r   r   _scheduled_event_id_started_event_id	_identity_activity_type_activity_id_retry_state)r   r   rh   ri   rj   rk   rl   rm   r   r   r   r      s   
zActivityError.__init__c                 C   r
   )z"Scheduled event ID for this error.rn   r   r   r   r   rh     r!   z ActivityError.scheduled_event_idc                 C   r
   z Started event ID for this error.ro   r   r   r   r   ri     r!   zActivityError.started_event_idc                 C   r
   )zIdentity for this error.)rp   r   r   r   r   rj     r!   zActivityError.identityc                 C   r
   )zActivity type for this error.)rq   r   r   r   r   rk      r!   zActivityError.activity_typec                 C   r
   )zActivity ID for this error.)rr   r   r   r   r   rl   %  r!   zActivityError.activity_idc                 C   r
   zRetry state for this error.rs   r   r   r   r   rm   *  r!   zActivityError.retry_state)r   r   r   r   r"   r-   r   rX   r   r   rh   ri   rj   rk   rl   rm   r'   r   r   r   r   rg      s>    	
rg   c                       s   e Zd ZdZdededededededed	ee d
df fddZe	d
efddZ
e	d
efddZe	d
efddZe	d
efddZe	d
efddZe	d
efddZe	d
ee fddZ  ZS )ChildWorkflowErrorz'Error raised on child workflow failure.r   	namespacer*   r)   r+   initiated_event_idri   rm   r	   Nc          	         s:   t  | || _|| _|| _|| _|| _|| _|| _dS )z"Initialize a child workflow error.N)	r   r   
_namespace_workflow_id_run_id_workflow_type_initiated_event_idro   rs   )	r   r   rz   r*   r)   r+   r{   ri   rm   r   r   r   r   3  s   
zChildWorkflowError.__init__c                 C   r
   )zNamespace for this error.)r|   r   r   r   r   rz   I  r!   zChildWorkflowError.namespacec                 C   r
   )zWorkflow ID for this error.)r}   r   r   r   r   r*   N  r!   zChildWorkflowError.workflow_idc                 C   r
   )zRun ID for this error.)r~   r   r   r   r   r)   S  r!   zChildWorkflowError.run_idc                 C   r
   )zWorkflow type for this error.)r   r   r   r   r   r+   X  r!   z ChildWorkflowError.workflow_typec                 C   r
   )z"Initiated event ID for this error.)r   r   r   r   r   r{   ]  r!   z%ChildWorkflowError.initiated_event_idc                 C   r
   ru   rv   r   r   r   r   ri   b  r!   z#ChildWorkflowError.started_event_idc                 C   r
   rw   rx   r   r   r   r   rm   g  r!   zChildWorkflowError.retry_state)r   r   r   r   r"   r-   r   rX   r   r   rz   r*   r)   r+   r{   ri   rm   r'   r   r   r   r   ry   0  sF    	
ry   c                       s   e Zd ZdZdedededededef fdd	Zed
efddZed
efddZ	ed
efddZ
ed
efddZed
efddZ  ZS )NexusOperationErrorz:Error raised on Nexus operation failure inside a Workflow.r   rh   endpointservice	operationoperation_tokenc                   s.   t  | || _|| _|| _|| _|| _dS )a  Initialize a Nexus operation error.

        Args:
            message: The error message.
            scheduled_event_id: The NexusOperationScheduled event ID for the failed operation.
            endpoint: The endpoint name for the failed operation.
            service: The service name for the failed operation.
            operation: The name of the failed operation.
            operation_token: The operation token returned by the failed operation.
        N)r   r   rn   	_endpoint_service
_operation_operation_token)r   r   rh   r   r   r   r   r   r   r   r   p  s   
zNexusOperationError.__init__r	   c                 C   r
   )z>The NexusOperationScheduled event ID for the failed operation.rt   r   r   r   r   rh     r!   z&NexusOperationError.scheduled_event_idc                 C   r
   )z+The endpoint name for the failed operation.)r   r   r   r   r   r     r!   zNexusOperationError.endpointc                 C   r
   )z*The service name for the failed operation.)r   r   r   r   r   r     r!   zNexusOperationError.servicec                 C   r
   )z!The name of the failed operation.)r   r   r   r   r   r     r!   zNexusOperationError.operationc                 C   r
   )z5The operation token returned by the failed operation.)r   r   r   r   r   r     r!   z#NexusOperationError.operation_token)r   r   r   r   r"   r-   r   r   rh   r   r   r   r   r'   r   r   r   r   r   m  s2    r   	exceptionr	   c                 C   s@   t | tjpt | tpt | tpt | tpt | tot | jtS )aC  Check whether the given exception is considered a cancellation exception
    according to Temporal.

    This is often used in a conditional of a catch clause to check whether a
    cancel occurred inside of a workflow. This can occur from
    :py:class:`asyncio.CancelledError` or :py:class:`CancelledError` or either
    :py:class:`ActivityError` or :py:class:`ChildWorkflowError` if either of
    those latter two have a :py:class:`CancelledError` cause.

    Args:
        exception: Exception to check.

    Returns:
        True if a cancelled exception, false if not.
    )
isinstanceasynciorC   rg   ry   r   r   )r   r   r   r   is_cancelled_exception  s   

r   )!r   r   datetimer   enumr   typingr   r   r   r   temporalio.api.common.v1r#   temporalio.api.enums.v1temporalio.api.failure.v1	Exceptionr   r   r(   r,   r3   rC   rJ   rK   rT   rW   rX   rg   ry   r   r   rB   r   r   r   r   r   <module>   s.    ;6=8