o
    ñ;—iÃ(  ã                   @   sä  d Z ddlZddlZddlmZmZ ddlZddlZej	j
Z
G dd„ deƒZG dd„ dejƒZG dd	„ d	eeƒZG d
d„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ de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ƒZ&G d8d9„ d9eƒZ'G d:d;„ d;eƒZ(G d<d=„ d=eƒZ)G d>d?„ d?e*ƒZ+G d@dA„ dAe*ƒZ,G dBdC„ dCe*ƒZ-G dDdE„ dEe*ƒZ.G dFdG„ dGeƒZ/G dHdI„ dIeƒZ0dJdK„ Z1dXdLe2dMe2fdNdO„Z3G dPdQ„ dQe4ƒZ5G dRdS„ dSeƒZ6G dTdU„ dUeƒZ7G dVdW„ dWeƒZ8dS )Ya©  
Modal-specific exception types.

## Notes on `grpclib.GRPCError` migration

Historically, the Modal SDK could propagate `grpclib.GRPCError` exceptions out
to user code.  As of v1.3, we are in the process of gracefully migrating to
always raising a Modal exception type in these cases. To avoid breaking user
code that relies on catching `grpclib.GRPCError`, a subset of Modal exception
types temporarily inherit from `grpclib.GRPCError`.

We encourage users to migrate any code that currently catches `grpclib.GRPCError`
to instead catch the appropriate Modal exception type. The following mapping
between GRPCError status codes and Modal exception types is currently in use:

```
CANCELLED -> ServiceError
UNKNOWN -> ServiceError
INVALID_ARGUMENT -> InvalidError
DEADLINE_EXCEEDED -> ServiceError
NOT_FOUND -> NotFoundError
ALREADY_EXISTS -> AlreadyExistsError
PERMISSION_DENIED -> PermissionDeniedError
RESOURCE_EXHAUSTED -> ResourceExhaustedError
FAILED_PRECONDITION -> ConflictError
ABORTED -> ConflictError
OUT_OF_RANGE -> InvalidError
UNIMPLEMENTED -> UnimplementedError
INTERNAL -> InternalError
UNAVAILABLE -> ServiceError
DATA_LOSS -> DataLossError
UNAUTHENTICATED -> AuthError
```

é    N)ÚAnyÚOptionalc                   @   ó   e Zd ZdZdS )ÚErrorah  
    Base class for all Modal errors. See [`modal.exception`](https://modal.com/docs/reference/modal.exception)
    for the specialized error classes.

    **Usage**

    ```python notest
    import modal

    try:
        ...
    except modal.Error:
        # Catch any exception raised by Modal's systems.
        print("Responding to error...")
    ```
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úC/home/ubuntu/.local/lib/python3.10/site-packages/modal/exception.pyr   0   ó    r   c                   @   sÚ   e Zd ZU dZeed< ejed< eed< dde	e fdd„Z
d	efd
d„Zddd„Zed	efdd„ƒZejded	dfdd„ƒZed	ejfdd„ƒZejdejd	dfdd„ƒZed	efdd„ƒZejded	dfdd„ƒZdS )Ú_GRPCErrorWrappera÷  This transitional class helps us migrate away from propagating `grpclib.GRPCError` to users.

    It serves two purposes:
    - It avoids abruptly breaking user code that catches `grpclib.GRPCError`
    - It actively warns when users access attributes defined by `grpclib.GRPCError`

    This won't catch all cases (users might react indiscriminately to GRPCError without checking the status).

    The mapping between GRPCError status codes and our error types is defined in `modal._grpc_client`.

    Ú_grpc_messageÚ_grpc_statusÚ_grpc_detailsNÚmessagec                 C   s   |pd| _ d S )NÚ )Ú_message)Úselfr   r   r   r   Ú__init__U   s   z_GRPCErrorWrapper.__init__Úreturnc                 C   s   t | ƒj› d| j›dS )Nú(ú))Útyper   r   ©r   r   r   r   Ú__repr__Z   s   z_GRPCErrorWrapper.__repr__c                 C   s0   ddl m} t| ƒj}|dd|› ddd d S )Né   )Údeprecation_warning)ié  é   é	   z}Modal will stop propagating the `grpclib.GRPCError` type in the future. Update your code so that it catches `modal.exception.zE` directly to avoid changes to error handling behavior in the future.T)Úpending)Ú_utils.deprecationr   r   r   )r   r   Úexc_typer   r   r   Ú$_warn_on_grpc_error_attribute_access]   s   
ÿ
ûz6_GRPCErrorWrapper._warn_on_grpc_error_attribute_accessc                 C   ó   |   ¡  | jS ©N)r$   r   r   r   r   r   r   i   ó   z_GRPCErrorWrapper.messageÚvaluec                 C   ó
   || _ d S r&   )r   ©r   r(   r   r   r   r   n   ó   
c                 C   r%   r&   )r$   r   r   r   r   r   Ústatusr   r'   z_GRPCErrorWrapper.statusc                 C   r)   r&   )r   r*   r   r   r   r,   w   r+   c                 C   r%   r&   )r$   r   r   r   r   r   Údetails{   r'   z_GRPCErrorWrapper.detailsc                 C   r)   r&   )r   r*   r   r   r   r-   €   r+   r&   )r   N)r   r   r	   r
   ÚstrÚ__annotations__ÚgrpclibÚStatusr   r   r   r   r$   Úpropertyr   Úsetterr,   r-   r   r   r   r   r   C   s(   
 

r   c                   @   r   )ÚAlreadyExistsErrorzDRaised when a resource creation conflicts with an existing resource.Nr   r   r   r   r   r4   …   r   r4   c                   @   r   )Ú	AuthErrorz;Raised when a client has missing or invalid authentication.Nr   r   r   r   r   r5   ‰   r   r5   c                   @   r   )ÚInternalErrorz9Raised when an internal error occurs in the Modal system.Nr   r   r   r   r   r6      r   r6   c                   @   r   )ÚInvalidErrorz(Raised when user does something invalid.Nr   r   r   r   r   r7   ‘   r   r7   c                   @   r   )ÚConflictErrorzTRaised when a resource conflict occurs between the request and current system state.Nr   r   r   r   r   r8   •   r   r8   c                   @   r   )ÚDataLossErrorz&Raised when data is lost or corrupted.Nr   r   r   r   r   r9   ™   r   r9   c                   @   r   )ÚNotFoundErrorz/Raised when a requested resource was not found.Nr   r   r   r   r   r:      r   r:   c                   @   r   )ÚPermissionDeniedErrorzORaised when a user does not have permission to perform the requested operation.Nr   r   r   r   r   r;   ¡   r   r;   c                   @   r   )ÚResourceExhaustedErrorzRRaised when a server-side resource has been exhausted, e.g. a quota or rate limit.Nr   r   r   r   r   r<   ¥   r   r<   c                   @   r   )ÚServiceErrorzARaised when an error occurs in basic client/server communication.Nr   r   r   r   r   r=   ©   r   r=   c                   @   r   )ÚUnimplementedErrorzFRaised when a requested operation is not implemented or not supported.Nr   r   r   r   r   r>   ­   r   r>   c                   @   r   )ÚRemoteErrorz0Raised when an error occurs on the Modal server.Nr   r   r   r   r   r?   ±   r   r?   c                   @   r   )ÚTimeoutErrorzBase class for Modal timeouts.Nr   r   r   r   r   r@   µ   r   r@   c                   @   r   )ÚSandboxTimeoutErrorzIRaised when a Sandbox exceeds its execution duration limit and times out.Nr   r   r   r   r   rA   ¹   r   rA   c                   @   r   )ÚExecTimeoutErrorzSRaised when a container process exceeds its execution duration limit and times out.Nr   r   r   r   r   rB   ½   r   rB   c                   @   r   )ÚSandboxTerminatedErrorz;Raised when a Sandbox is terminated for an internal reason.Nr   r   r   r   r   rC   Á   r   rC   c                   @   r   )ÚFunctionTimeoutErrorzJRaised when a Function exceeds its execution duration limit and times out.Nr   r   r   r   r   rD   Å   r   rD   c                   @   r   )ÚMountUploadTimeoutErrorz%Raised when a Mount upload times out.Nr   r   r   r   r   rE   É   r   rE   c                   @   r   )ÚVolumeUploadTimeoutErrorz&Raised when a Volume upload times out.Nr   r   r   r   r   rF   Í   r   rF   c                   @   r   )ÚInteractiveTimeoutErrorzRRaised when interactive frontends time out while trying to connect to a container.Nr   r   r   r   r   rG   Ñ   r   rG   c                   @   r   )ÚOutputExpiredErrorz8Raised when the Output exceeds expiration and times out.Nr   r   r   r   r   rH   Õ   r   rH   c                   @   r   )ÚConnectionErrorzBRaised when an issue occurs while connecting to the Modal servers.Nr   r   r   r   r   rI   Ù   r   rI   c                   @   r   )ÚVersionErrorz?Raised when the current client version of Modal is unsupported.Nr   r   r   r   r   rJ   Ý   r   rJ   c                   @   r   )ÚExecutionErrorz9Raised when something unexpected happened during runtime.Nr   r   r   r   r   rK   á   r   rK   c                   @   r   )ÚDeserializationErrorzSRaised to provide more context when an error is encountered during deserialization.Nr   r   r   r   r   rL   å   r   rL   c                   @   r   )ÚSerializationErrorzQRaised to provide more context when an error is encountered during serialization.Nr   r   r   r   r   rM   é   r   rM   c                   @   r   )ÚRequestSizeErrorzdRaised when an operation produces a gRPC request that is rejected by the server for being too large.Nr   r   r   r   r   rN   í   r   rN   c                   @   r   )ÚDeprecationErrorzLUserWarning category emitted when a deprecated Modal feature or API is used.Nr   r   r   r   r   rO   ñ   r   rO   c                   @   r   )ÚPendingDeprecationErrorzWSoon to be deprecated feature. Only used intermittently because of multi-repo concerns.Nr   r   r   r   r   rP   ÷   r   rP   c                   @   r   )ÚServerWarningzGWarning originating from the Modal server and re-issued in client code.Nr   r   r   r   r   rQ   û   r   rQ   c                   @   r   )ÚAsyncUsageWarningzLWarning emitted when a blocking Modal interface is used in an async context.Nr   r   r   r   r   rR   ÿ   r   rR   c                   @   r   )ÚInternalFailurezRetriable internal error.Nr   r   r   r   r   rS     r   rS   c                   @   s   e Zd ZdZdefdd„ZdS )Ú_CliUserExecutionErroraA  mdmd:hidden
    Private wrapper for exceptions during when importing or running Apps from the CLI.

    This intentionally does not inherit from `modal.exception.Error` because it
    is a private type that should never bubble up to users. Exceptions raised in
    the CLI at this stage will have tracebacks printed.
    Úuser_sourcec                 C   r)   r&   )rU   )r   rU   r   r   r   r     s   
z_CliUserExecutionError.__init__N)r   r   r	   r
   r.   r   r   r   r   r   rT     s    rT   c                 C   s   t  d¡ tdƒ‚)Né   z1Simulated preemption interrupt from modal-client!)ÚsignalÚalarmÚKeyboardInterrupt)ÚsignumÚframer   r   r   Ú_simulate_preemption_interrupt  s   
r\   Úwait_secondsÚjitter_secondsc                 C   sD   | dkrt dƒ‚t tjt¡ |rt d|¡nd}t | | ¡ dS )a×  
    Utility for simulating a preemption interrupt after `wait_seconds` seconds.
    The first interrupt is the SIGINT signal. After 30 seconds, a second
    interrupt will trigger.

    This second interrupt simulates SIGKILL, and should not be caught.
    Optionally add between zero and `jitter_seconds` seconds of additional waiting before first interrupt.

    **Usage:**

    ```python notest
    import time
    from modal.exception import simulate_preemption

    simulate_preemption(3)

    try:
        time.sleep(4)
    except KeyboardInterrupt:
        print("got preempted") # Handle interrupt
        raise
    ```

    See https://modal.com/docs/guide/preemption for more details on preemption
    handling.
    r   z#Time to wait must be greater than 0N)Ú
ValueErrorrW   ÚSIGALRMr\   ÚrandomÚ	randrangerX   )r]   r^   Újitterr   r   r   Úsimulate_preemption  s
   rd   c                   @   r   )ÚInputCancellationzóRaised when the current input is cancelled by the task

    Intentionally a BaseException instead of an Exception, so it won't get
    caught by unspecified user exception clauses that might be used for retries and
    other control flow.
    Nr   r   r   r   r   re   =  r   re   c                   @   ó   e Zd ZdS )ÚModuleNotMountableN©r   r   r	   r   r   r   r   rg   F  ó    rg   c                   @   rf   )ÚClientClosedNrh   r   r   r   r   rj   J  ri   rj   c                   @   r   )ÚFilesystemExecutionErrorzORaised when an unknown error is thrown during a container filesystem operation.Nr   r   r   r   r   rk   N  r   rk   )r   )9r
   ra   rW   Útypingr   r   r0   Úsynchronicity.exceptionsÚsynchronicityÚ
exceptionsÚUserCodeExceptionÚ	Exceptionr   Ú	GRPCErrorr   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   ÚUserWarningrO   rP   rQ   rR   rS   rT   r\   Úintrd   ÚBaseExceptionre   rg   rj   rk   r   r   r   r   Ú<module>   s`   $B"	