o
    NK&h;2                     @  s  U d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ded< zddlmZ eeddZW n eyd   ddlmZ Y nw ejrpddlmZmZ dZ dZ!dZ"dZ#G dd dZ$G dd de$Z%G dd de$Z&G dd de$Z'dd Z(ddd d!Z)d"d# e)* D Z+e&e'e%e%d$Z,d%d&d'd%d&d'd%d(Z-d)d* Z.	d>d?d9d:Z/d@d<d=Z0dS )Aa  
Sanic `provides a pattern
<https://sanicframework.org/guide/best-practices/exceptions.html#using-sanic-exceptions>`_
for providing a response when an exception occurs. However, if you do no handle
an exception, it will provide a fallback. There are three fallback types:

- HTML - *default*
- Text
- JSON

Setting ``app.config.FALLBACK_ERROR_FORMAT = "auto"`` will enable a switch that
will attempt to provide an appropriate response format based upon the
request type.
    )annotationsN)partial)
extract_tb)
BadRequestSanicException)STATUS_CODES)deprecationlogger)	ErrorPage)htmljsontextzt.Callable[..., str]dumpsr   F)escape_forward_slashes)HTTPResponseRequestautozGThe application encountered an unexpected error and could not continue.i  application/jsonc                   @  sr   e Zd ZdZeeZddd	ZedddZedd Z	edd Z
edd ZdddZdddZdddZdS )BaseRenderera3  Base class that all renderers must inherit from.

    This class defines the structure for rendering objects, handling the core functionality that specific renderers may extend.

    Attributes:
        request (Request): The incoming request object that needs rendering.
        exception (Exception): Any exception that occurred and needs to be rendered.
        debug (bool): Flag indicating whether to render with debugging information.

    Methods:
        dumps: A static method that must be overridden by subclasses to define the specific rendering.

    Args:
        request (Request): The incoming request object that needs rendering.
        exception (Exception): Any exception that occurred and needs to be rendered.
        debug (bool): Flag indicating whether to render with debugging information.
    requestr   	exception	Exceptiondebugboolc                 C  s   || _ || _|| _d S N)r   r   r   )selfr   r   r    r   C/var/www/html/venv/lib/python3.10/site-packages/sanic/errorpages.py__init__G   s   
zBaseRenderer.__init__returnt.Dict[str, str]c                 C  s   t | jtrt| jdi S i S )z(The headers to be used for the response.headers)
isinstancer   r   getattrr   r   r   r   r"   L      zBaseRenderer.headersc                 C  s   t | jtrt| jdtS tS )z,The status code to be used for the response.status_code)r#   r   r   r$   FALLBACK_STATUSr%   r   r   r   statusS   r&   zBaseRenderer.statusc                 C  s    | j s	t| jtrt| jS tS )z%The text to be used for the response.)r   r#   r   r   strFALLBACK_TEXTr%   r   r   r   r   Z   s   
zBaseRenderer.textc                 C  s"   t | jd }| j d| S )z&The title to be used for the response.   Error Occurredu    — r   getr)   decode)r   status_textr   r   r   titlea   s   zBaseRenderer.titler   c                 C  s<   | j rt| jdds| jn| j }| j|_|j| j |S )znOutputs the exception as a response.

        Returns:
            HTTPResponse: The response object.
        quietF)r   r$   r   fullminimalr)   r"   updater   outputr   r   r   renderg   s   zBaseRenderer.renderc                 C     t )zProvide a formatted message that is meant to not show any sensitive data or details.

        This is the default fallback for production environments.

        Returns:
            HTTPResponse: The response object.
        NotImplementedErrorr%   r   r   r   r4   v   s   zBaseRenderer.minimalc                 C  r9   )zProvide a formatted message that has all details and is mean to be used primarily for debugging and non-production environments.

        Returns:
            HTTPResponse: The response object.
        r:   r%   r   r   r   r3      s   zBaseRenderer.fullN)r   r   r   r   r   r   )r    r!   r    r   )__name__
__module____qualname____doc__staticmethodr   r   propertyr"   r)   r   r1   r8   r4   r3   r   r   r   r   r   2   s    






r   c                      s,   e Zd ZdZd fddZdddZ  ZS )	HTMLRendererzARender an exception as HTML.

    The default fallback type.
    r    r   c                   s,   t | jt jt j| j| jd}t| S )N)r   r1   r   r   exc)	r
   r   superr1   r   r   r   r   r8   )r   page	__class__r   r   r3      s   zHTMLRenderer.fullc                 C  s   |   S r   r3   r%   r   r   r   r4      s   zHTMLRenderer.minimalr<   )r=   r>   r?   r@   r3   r4   __classcell__r   r   rG   r   rC      s    
rC   c                      sX   e Zd ZdZdZdZdddZddd	Ze fd
dZ	dd Z
dd Zdd Z  ZS )TextRendererz"Render an exception as plain text.z{title}
{bar}
{text}

{body}z  r    r   c                 C  .   t | jj| j| j dt| j | jdddS )N=TrI   r1   r   barbodyr   OUTPUT_TEXTformatr1   len_generate_bodyr%   r   r   r   r3         
zTextRenderer.fullc                 C  rL   )NrM   FrI   rN   rQ   r%   r   r   r   r4      rV   zTextRenderer.minimalc                   s   dt  j S )Nu   ⚠️ )rE   r1   r%   rG   r   r   r1      s   zTextRenderer.titlec          
      C  s   g }|r@t  \}}}g }|| jjj d| j d| jj d| jjj dg7 }|r7|	| 
| |j}|s*||d d d 7 }ddt|ffD ]\}}t| j|d }	|	r_|r_|| |	|7 }qHd|S )	N: z while handling path zTraceback of z (most recent call last):
contextTextra
)sysexc_infor   rH   r=   r   pathappnameappend_format_exc	__cause__r   r$   _generate_object_display_listjoin)
r   r3   lines_	exc_value__
exceptionsattrdisplayinfor   r   r   rU      s*   
zTextRenderer._generate_bodyc                   s<   d  fddt|jD } j |jj d| d| S )Nz

c                   sD   g | ]} j d   d|j d|j d|j d j d   |j 
qS )   zFile z, line z, in r\   )SPACERfilenamelinenora   line.0framer%   r   r   
<listcomp>   s    z,TextRenderer._format_exc.<locals>.<listcomp>rW   r\   )rf   r   __traceback__rp   rH   r=   )r   rD   framesr   r%   r   rc      s   
zTextRenderer._format_excc                 C  sN   d|   g}| D ]\}}| |}|| jd  | d|  q|S )Nr\   ro   rW   )r1   itemsr   rb   rp   )r   obj
descriptorrg   keyvaluerm   r   r   r   re      s
   
 z*TextRenderer._generate_object_display_listr<   )r=   r>   r?   r@   rR   rp   r3   r4   rB   r1   rU   rc   re   rJ   r   r   rG   r   rK      s    



rK   c                   @  s8   e Zd ZdZdddZdddZdd	 Zed
d ZdS )JSONRendererzRender an exception as JSON.r    r   c                 C     | j dd}t|| jdS )NTrI   r   _generate_outputr   r   r6   r   r   r   r3         zJSONRenderer.fullc                 C  r   )NFrI   r   r   r6   r   r   r   r4      r   zJSONRenderer.minimalc          
      C  s   | j | j| jd}ddt|ffD ]\}}t| j|d }|r$|r$|||< q|rat \}}}g }	|rL|	|j	j
t|dd t|jD d |j}|s2| jj|d< | jj|d< |	d d d	 |d
< |S )N)descriptionr)   messagerY   r[   c                 S  s"   g | ]}|j |j|j|jd qS ))filers   ra   src)rq   rr   ra   rs   rt   r   r   r   rw     s    z1JSONRenderer._generate_output.<locals>.<listcomp>)typer   ry   r_   argsrX   rk   )r1   r)   r   r   r$   r   r]   r^   rb   rH   r=   r*   r   rx   rd   r   r_   r   )
r   r3   r7   rl   rm   rn   rh   ri   rj   rk   r   r   r   r      s6   zJSONRenderer._generate_outputc                 C  s   t | jd S )Nr,   r-   r%   r   r   r   r1     s   zJSONRenderer.titleNr<   )	r=   r>   r?   r@   r3   r4   r   rB   r1   r   r   r   r   r      s    

(r   c                 C  s   |   dd ddS )zEMinimal HTML escaping, not for attribute values (unlike html.escape).&z&amp;<z&lt;)replace)r   r   r   r   escape   s   r   
text/plain	text/html)r   r   r   c                 C  s   i | ]\}}||qS r   r   )ru   kvr   r   r   
<dictcomp>*  s    r   )r   r   zmultipart/form-datar   r   r   r   )r   r   r   JSONResponser   r   r   c                 C  s&   | t vr| dkrtd|  dS dS )zCheck that the format is known.r   zUnknown format: N)MIME_BY_CONFIGr   )rS   r   r   r   check_error_format?  s   r   r   r   r   r   r   r   fallbackr*   baset.Type[BaseRenderer]renderer t.Optional[t.Type[BaseRenderer]]r    r   c                 C  s<   |st | |}t||}ttjt |}|| || S )z=Render a response for the default FALLBACK exception handler.)
guess_mimeRENDERERS_BY_CONTENT_TYPEr.   tcastTyper   r8   )r   r   r   r   r   r   mtr   r   r   exception_responseE  s
   	
r   reqc                 C  sN  i }d}| j r| j j}| j jj}|tv r|||< |s"|tv r"d||< |sf|dkrf| jtrft| jv r8d|d< n.t| j	ddv rFd|d< n d}z| j
}W n	 tyV   Y nw |rfd|d< td	| d
d |dkrwtD ]
}||vrvd||< qldd |D }| jj| }|rt|j }	||	 }
td|j|	|
 |jS tdt| j |jS )z<Guess the MIME type for the response based upon the request. FALLBACK_ERROR_FORMATr   zrequest.acceptr   zcontent-typeNzrequest.jsonzResponse type was determined by the JSON content of the request. This behavior is deprecated and will be removed in v24.3. Please specify the format either by
  error_format="json" on route zg, by
  FALLBACK_ERROR_FORMAT = "json", or by adding header
  accept: application/json to your requests.gL8@anyc                 S  s   g | ]}t | qS r   )r   )ru   r   r   r   r   rw     s    zguess_mime.<locals>.<listcomp>z5Error Page: The client accepts %s, using '%s' from %sz2Error Page: No format found, the client accepts %s)routera   r[   error_formatr   acceptmatchJSONr"   getoner   r   r   CONFIG_BY_MIMEmimer	   r   headerrepr)r   r   formatsra   fcr   mimesmrS   sourcer   r   r   r   V  sd   





r   r   )r   r   r   r   r   r   r   r*   r   r   r   r   r    r   )r   r   r   r*   r    r*   )1r@   
__future__r   r]   typingr   	functoolsr   	tracebackr   sanic.exceptionsr   r   sanic.helpersr   	sanic.logr   r	   sanic.pages.errorr
   sanic.responser   r   r   __annotations__ujsonr   ImportErrorTYPE_CHECKINGsanicr   r   DEFAULT_FORMATr+   r(   r   r   rC   rK   r   r   r   rz   r   r   RESPONSE_MAPPINGr   r   r   r   r   r   r   <module>   sf    WK8
