o
    _�¿j�"  ã                   @  s¸   d Z ddlmZ ddlm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¢ZdS )uè  indiaopt exception hierarchy.

Every public exception traces back to :class:`IndiaOptError` so callers can
catch either the specific subclass or the base class, depending on how much
granularity they need.

Hierarchy::

    IndiaOptError
    â”œâ”€â”€ FetchError
    â”‚   â”œâ”€â”€ NetworkError
    â”‚   â”œâ”€â”€ RateLimitError
    â”‚   â””â”€â”€ ParseError
    â”œâ”€â”€ CircuitOpenError
    â”œâ”€â”€ FetchTimeoutError
    â”œâ”€â”€ ConfigurationError
    â””â”€â”€ ValidationError
é    )Úannotations)ÚAnyc                      s8   e Zd ZdZdddœd‡ fdd„Zd‡ fdd„Z‡  ZS )ÚIndiaOptErroram  Base exception for all indiaopt errors.

    All exceptions raised by this library are subclasses of
    :class:`IndiaOptError`, so you can use a single broad ``except``
    clause when desired::

        try:
            result = await client.fetch_option_chain("NIFTY")
        except IndiaOptError as exc:
            logger.error("indiaopt error: %s", exc)
    N©ÚcontextÚrecoveryÚmessageÚstrr   údict[str, Any] | Noner   ú
str | NoneÚreturnÚNonec                  s    t ƒ  |¡ |p	i | _|| _d S )N)ÚsuperÚ__init__r   r   )Úselfr   r   r   ©Ú	__class__© úN/home/dinkstrade/pdmp/venv/lib/python3.10/site-packages/indiaopt/exceptions.pyr   &   s   

zIndiaOptError.__init__c                   s^   t ƒ  ¡ g}| jr| d| j› �¡ | jr*d dd„ | j ¡ D ƒ¡}| d|› �¡ d |¡S )Nu     âžœ Recovery: z, c                 s  s"   � | ]\}}|› d |›�V  qdS )ú=Nr   )Ú.0ÚkÚvr   r   r   Ú	<genexpr>6   s   €  z(IndiaOptError.__str__.<locals>.<genexpr>u     âžœ Context: Ú
)r   Ú__str__r   Úappendr   ÚjoinÚitems)r   ÚpartsÚctxr   r   r   r   1   s   
zIndiaOptError.__str__)r   r	   r   r
   r   r   r   r   )r   r	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   r   r   r      s    ûr   c                      s0   e Zd ZdZddddddœd‡ fdd„Z‡  ZS )Ú
FetchErroraR  Raised when a data fetch operation fails after all retry attempts.

    Attributes:
        symbol:      The trading symbol that failed.
        exchange:    The exchange name (``"NSE"`` or ``"BSE"``).
        status_code: HTTP status code of the last response (if any).
        attempts:    Number of attempts made before giving up.
    ÚNSENr   )ÚexchangeÚstatus_codeÚattemptsr   r   r   r	   Úsymbolr(   r)   ú
int | Noner*   Úintr   r
   r   r   r   r   c          	        sV   |||dœ|d urd|ini ¥|pi ¥}t ƒ j|||d� || _|| _|| _|| _d S )N)r+   r(   r*   r)   r   )r   r   r+   r(   r)   r*   )	r   r   r+   r(   r)   r*   r   r   r    r   r   r   r   H   s   ýüû
zFetchError.__init__)r   r	   r+   r	   r(   r	   r)   r,   r*   r-   r   r
   r   r   r   r   ©r!   r"   r#   r$   r   r%   r   r   r   r   r&   >   s    ÷r&   c                   @  ó   e Zd ZdZdS )ÚNetworkErrorzÄRaised when a low-level network error occurs (connection refused, DNS, etc.).

    Example::

        except NetworkError as exc:
            print(f"Network problem for {exc.symbol}: {exc}")
    N©r!   r"   r#   r$   r   r   r   r   r0   a   ó    r0   c                      s2   e Zd ZdZdddddddœd‡ fdd„Z‡  ZS )ÚRateLimitErrorzôRaised when the exchange rate-limits the client (HTTP 429 / 403).

    Includes a ``retry_after`` hint when the exchange provides a
    ``Retry-After`` header.

    Attributes:
        retry_after: Suggested wait time in seconds, if known.
    r'   Nr   )r(   r)   r*   Úretry_afterr   r   r   r	   r+   r(   r)   r,   r*   r-   r4   úfloat | Noner   r
   r   r   r   r   c          	   
     s>   t ƒ j|||||||p|d urd|d›d�ndd� || _d S )NúWait ú.0fzs before retrying.z!Back off and retry after a delay.)r+   r(   r)   r*   r   r   )r   r   r4   )	r   r   r+   r(   r)   r*   r4   r   r   r   r   r   r   u   s   ÿö
zRateLimitError.__init__)r   r	   r+   r	   r(   r	   r)   r,   r*   r-   r4   r5   r   r
   r   r   r   r   r.   r   r   r   r   r3   k   s    ör3   c                      s.   e Zd ZdZdddddœd‡ fdd„Z‡  ZS )Ú
ParseErrora  Raised when the exchange response cannot be parsed.

    Typically caused by the exchange returning a redirect, CAPTCHA page,
    or a malformed JSON payload.

    Attributes:
        raw_preview: First 200 chars of the raw response body for debugging.
    r'   r   Ú N)r(   r*   Úraw_previewr   r   r	   r+   r(   r*   r-   r:   r   r
   r   r   c                  s"   t ƒ j|||||dd� || _d S )NzPCheck if the exchange is returning HTML instead of JSON (CAPTCHA / maintenance).)r+   r(   r*   r   r   )r   r   r:   )r   r   r+   r(   r*   r:   r   r   r   r   r   ›   s   
ú
zParseError.__init__)r   r	   r+   r	   r(   r	   r*   r-   r:   r	   r   r
   r   r   r.   r   r   r   r   r8   ‘   s    ør8   c                      ó(   e Zd ZdZddœd‡ fdd„Z‡  ZS )ÚCircuitOpenErroraþ  Raised when the circuit breaker is open for a symbol.

    The circuit opens after :attr:`~indiaopt.config.settings.Settings.circuit_failure_threshold`
    consecutive failures and remains open for
    :attr:`~indiaopt.config.settings.Settings.circuit_backoff_seconds`.

    Attributes:
        symbol:         The trading symbol whose circuit is open.
        open_until:     Monotonic timestamp when the circuit will close.
        failures:       Number of consecutive failures that opened the circuit.
    r'   ©r(   r+   r	   Ú
open_untilÚfloatÚfailuresr-   r(   r   r   c             
     s|   dd l }td|| ¡  ƒ}tƒ jd|› d|› d|› d|d›d�	|||t|ƒd	œd
|d›d�d� || _|| _|| _|| _	d S )Nr   g        zCircuit breaker OPEN for ú/z (z$ consecutive failures). Retrying in r7   zs.)r+   r(   r@   Úretry_in_secondsr6   zAs or reset the circuit manually via client.reset_circuit(symbol).r   )
ÚtimeÚmaxÚ	monotonicr   r   Úroundr+   r>   r@   r(   )r   r+   r>   r@   r(   rC   Ú	remainingr   r   r   r   À   s&   ÿÿü÷
zCircuitOpenError.__init__)
r+   r	   r>   r?   r@   r-   r(   r	   r   r   r.   r   r   r   r   r<   ³   s    úr<   c                      s(   e Zd ZdZddœd‡ fdd„Z‡  ZS )ÚFetchTimeoutErrorzÀRaised when a fetch operation exceeds the configured timeout.

    Attributes:
        symbol:     The trading symbol that timed out.
        timeout_s:  The timeout threshold in seconds.
    r'   r=   r+   r	   Ú	timeout_sr?   r(   r   r   c                  sF   t ƒ jd|d›d|› d|› d�|||dœdd� || _|| _|| _d S )	NzFetch timed out after z.1fzs for rA   Ú.)r+   r(   rI   z>Increase Settings.fetch_timeout or check network connectivity.r   )r   r   r+   rI   r(   )r   r+   rI   r(   r   r   r   r   ç   s   
ý
zFetchTimeoutError.__init__)r+   r	   rI   r?   r(   r	   r   r   r.   r   r   r   r   rH   ß   s    rH   c                   @  r/   )ÚConfigurationErrorzžRaised when the library is configured incorrectly.

    Example::

        except ConfigurationError as exc:
            print(f"Fix your config: {exc}")
    Nr1   r   r   r   r   rK   õ   r2   rK   c                      r;   )ÚValidationErrorzªRaised when caller-supplied input fails validation.

    Attributes:
        field:  The name of the invalid field.
        value:  The value that failed validation.
    N)r   r   r	   ÚfieldÚvalueÚobjectr   r   r   r   c                  s(   t ƒ j|||dœ|d� || _|| _d S )N)rM   rN   r   )r   r   rM   rN   )r   r   rM   rN   r   r   r   r   r     s   ý
zValidationError.__init__)
r   r	   rM   r	   rN   rO   r   r   r   r   r.   r   r   r   r   rL   ÿ   s    úrL   )	r<   rK   r&   rH   r   r0   r8   r3   rL   N)r$   Ú
__future__r   Útypingr   Ú	Exceptionr   r&   r0   r3   r8   r<   rH   rK   rL   Ú__all__r   r   r   r   Ú<module>   s    %#
&",
