o
    ›¨Êhu  ã                   @   s:  d dl Z d dlmZ d dlZd dlmZmZ d dlmZ G dd„ de	ƒZ
G dd„ de
ƒZG d	d
„ d
eeƒZG dd„ d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
eƒZG dd„ d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eƒZG d!d"„ d"eƒZdS )#é    N)Úformat_exception_only)ÚImproperlyConfiguredÚSuspiciousOperation)Ú	HTTPErrorc                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚAnymailErrorz’Base class for exceptions raised by Anymail

    Overrides __str__ to provide additional information about
    the ESP API call and response.
    c                    s¤   |  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  d| jr-| jjnd¡| _t| tƒr>| 	dd¡| _
n|  dd¡| _
tt| ƒj|i |¤Ž dS )	aÕ  
        Optional kwargs:
          email_message: the original EmailMessage being sent
          status_code: HTTP status code of response to ESP send call
          backend: the backend instance involved
          payload: data arg (*not* json-stringified) for the ESP send call
          response: requests.Response from the send call
          raised_from: original/wrapped Exception
          esp_name: what to call the ESP (read from backend if provided)
        ÚbackendNÚemail_messageÚpayloadÚstatus_codeÚraised_fromÚesp_nameÚresponse)Úpopr   r   r	   r
   r   r   Ú
isinstancer   Úgetr   Úsuperr   Ú__init__©ÚselfÚargsÚkwargs©Ú	__class__© úD/var/www/html/env/lib/python3.10/site-packages/anymail/exceptions.pyr      s   ÿ
zAnymailError.__init__c                 C   s:   d  dd„ | jD ƒ¡|  ¡ |  ¡ |  ¡ g}d  td |ƒ¡S )Nú c                 S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0Úargr   r   r   Ú
<listcomp>+   s    z(AnymailError.__str__.<locals>.<listcomp>Ú
)Újoinr   Údescribe_raised_fromÚdescribe_sendÚdescribe_responseÚfilter)r   Úpartsr   r   r   Ú__str__)   s   üzAnymailError.__str__c                 C   sl   | j du rdS d}z|dd | j j¡ 7 }W n	 ty   Y nw z|d| j j 7 }W |S  ty5   Y |S w )zFReturn a string describing the ESP send in self.email_message, or NoneNzSending a messagez to %sú,z from %s)r   r!   ÚtoÚAttributeErrorÚ
from_email)r   Údescriptionr   r   r   r#   2   s   
ÿþþzAnymailError.describe_sendc                 C   sÐ   | j du rdS | jj}t|tjƒr'z| d¡}W n ty&   | d¡}Y nw d| jp,d| j |f }z| j 	¡ }|dt	j
|dd� 7 }W |S  tttfyg   z|d	| jj 7 }W Y |S  tyf   Y Y |S w w )
zCReturn a formatted string of self.status_code and response, or NoneNzutf-8z
iso-8859-1z%s API response %d: %sÚESPr    é   )Úindentr   )r
   r   Úreasonr   ÚsixÚbinary_typeÚdecodeÚUnicodeDecodeErrorr   ÚjsonÚdumpsr*   ÚKeyErrorÚ
ValueErrorÚtext)r   r0   r,   Újson_responser   r   r   r$   A   s.   
ÿ
ûþþýzAnymailError.describe_responsec                 C   s*   | j du rdS d tt| j ƒ| j ƒ¡ ¡ S )zReturn the original exceptionNÚ )r   r!   r   ÚtypeÚstrip)r   r   r   r   r"   Y   s   
z!AnymailError.describe_raised_from)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r#   r$   r"   Ú__classcell__r   r   r   r   r   	   s    	r   c                   @   ó   e Zd ZdZdS )ÚAnymailAPIErrorz3Exception for unsuccessful response from ESP's API.N©r>   r?   r@   rA   r   r   r   r   rD   `   ó    rD   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚAnymailRequestsAPIErrorz8Exception for unsuccessful response from a requests API.c                    s2   t t| ƒj|i |¤Ž | jd ur| jj| _d S d S ©N)r   rG   r   r   r
   r   r   r   r   r   g   s   
ÿz AnymailRequestsAPIError.__init__©r>   r?   r@   rA   r   rB   r   r   r   r   rG   d   s    rG   c                       ó"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚAnymailRecipientsRefusedz@Exception for send where all recipients are invalid or rejected.Nc                    s.   |d u rd}t t| ƒj|g|¢R i |¤Ž d S )Nz/All message recipients were rejected or invalid)r   rK   r   )r   Úmessager   r   r   r   r   r   p   s   "z!AnymailRecipientsRefused.__init__rH   rI   r   r   r   r   rK   m   ó    rK   c                   @   rC   )ÚAnymailInvalidAddressz9Exception when using an invalidly-formatted email addressNrE   r   r   r   r   rN   v   rF   rN   c                   @   rC   )ÚAnymailUnsupportedFeaturea#  Exception for Anymail features that the ESP doesn't support.

    This is typically raised when attempting to send a Django EmailMessage that
    uses options or values you might expect to work, but that are silently
    ignored by or can't be communicated to the ESP's API.

    It's generally *not* raised for ESP-specific limitations, like the number
    of tags allowed on a message. (Anymail expects
    the ESP to return an API error for these where appropriate, and tries to
    avoid duplicating each ESP's validation logic locally.)

    NrE   r   r   r   r   rO   z   rF   rO   c                       rJ   )ÚAnymailSerializationErrorz¬Exception for data that Anymail can't serialize for the ESP's API.

    This typically results from including something like a date or Decimal
    in your merge_vars.

    Nc                    sl   |d u r|  dd ¡}|  d|r|jnd¡}d| }|d ur%|dt|ƒ 7 }tt| ƒj|g|¢R i |¤Ž d S )Nr   r   zthe ESPzVDon't know how to send this data to %s. Try converting it to a string or number first.z
%s)r   r   r   r   rP   r   )r   rL   Úorig_errr   r   r   r   r   r   r   r   ’   s   ÿ"z"AnymailSerializationError.__init__)NNrI   r   r   r   r   rP   ‰   s    rP   c                   @   rC   )ÚAnymailCancelSendz:Pre-send signal receiver can raise to prevent message sendNrE   r   r   r   r   rR   ž   rF   rR   c                   @   rC   )ÚAnymailWebhookValidationFailurez„Exception when a webhook cannot be validated.

    Django's SuspiciousOperation turns into
    an HTTP 400 error in production.
    NrE   r   r   r   r   rS   ¢   rF   rS   c                   @   rC   )ÚAnymailConfigurationErrorz:Exception for Anymail configuration or installation issuesNrE   r   r   r   r   rT   ª   rF   rT   c                       rJ   )ÚAnymailImproperlyInstalledz2Exception for Anymail missing package dependenciesú	<backend>c                    s    d||f }t t| ƒ |¡ d S )NzŠThe %s package is required to use this ESP, but isn't installed.
(Be sure to use `pip install django-anymail[%s]` with your desired ESPs.))r   rU   r   )r   Úmissing_packager   rL   r   r   r   r   ³   s   þz#AnymailImproperlyInstalled.__init__)rV   rI   r   r   r   r   rU   °   rM   rU   c                   @   rC   )ÚAnymailWarningzBase warning for AnymailNrE   r   r   r   r   rX   ¼   rF   rX   c                   @   rC   )ÚAnymailInsecureWebhookWarningz4Warns when webhook configured without any validationNrE   r   r   r   r   rY   À   rF   rY   c                   @   rC   )ÚAnymailDeprecationWarningz'Warning for deprecated Anymail featuresNrE   r   r   r   r   rZ   Ä   rF   rZ   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
_LazyErrorzAAn object that sits inert unless/until used, then raises an errorc                 C   s
   || _ d S rH   ©Ú_error)r   Úerrorr   r   r   r   Ì   s   
z_LazyError.__init__c                 O   ó   | j ‚rH   r\   r   r   r   r   Ú__call__Ï   ó   z_LazyError.__call__c                 C   r_   rH   r\   )r   Úitemr   r   r   Ú__getattr__Ò   ra   z_LazyError.__getattr__N)r>   r?   r@   rA   r   r`   rc   r   r   r   r   r[   Ê   s
    r[   )r5   Ú	tracebackr   r1   Údjango.core.exceptionsr   r   Úrequestsr   Ú	Exceptionr   rD   rG   rK   r8   rN   rO   Ú	TypeErrorrP   rR   rS   rT   ÚImportErrorrU   ÚWarningrX   rY   ÚDeprecationWarningrZ   Úobjectr[   r   r   r   r   Ú<module>   s(    W		