
    8Cf1                    "   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<   	 ddlmZ  eed      Zej8                  rddlmZmZ dZ dZ!dZ"dZ# G d d      Z$ G d de$      Z% G d de$      Z& G d de$      Z'd Z(dddd Z)e)jU                         D  ci c]  \  } }|| 
 c}} Z+e&e'e%e%d!Z,d"d#d$d"d#d$d"d%Z-d& Z.	 d)	 	 	 	 	 	 	 	 	 	 	 	 	 d*d'Z/d+d(Z0y# e$ r	 ddlmZ Y w xY wc c}} w ),a  
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                      e Zd ZdZ ee      ZddZedd       Zed        Z	ed        Z
ed        ZddZddZdd	Zy
)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.
    c                .    || _         || _        || _        y N)request	exceptiondebug)selfr   r   r   s       P/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/sanic/errorpages.py__init__zBaseRenderer.__init__F   s    "
    c                h    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   s    r   r"   zBaseRenderer.headersK   s*     dnnn54>>9b99	r    c                x    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%   s    r   statuszBaseRenderer.statusR   s+     dnnn54>>=/JJr    c                    | 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%   s    r   r   zBaseRenderer.textY   s-     ::DNNNCt~~&&r    c                    t        j                  | j                  d      j                         }| j                   d| S )z&The title to be used for the response.   Error Occurredu    — r   getr)   decode)r   status_texts     r   titlezBaseRenderer.title`   s9     #&&t{{4EFMMO++eK=11r    c                     | j                   r#t        | j                  dd      s| j                  n| j                         }| 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   outputs     r   renderzBaseRenderer.renderf   s\    zz'$..'5"I II
 dll+r    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%   s    r   r7   zBaseRenderer.minimalu   s
     "!r    c                    t         )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%   s    r   r6   zBaseRenderer.full   s
     "!r    N)r   r   r   	Exceptionr   bool)returnzt.Dict[str, str]rB   r   )__name__
__module____qualname____doc__staticmethodr   r   propertyr"   r)   r   r3   r;   r7   r6    r    r   r   r   1   sw    $ E
       2 2
""r    r   c                  ,     e Zd ZdZd fdZddZ xZS )HTMLRendererzARender an exception as HTML.

    The default fallback type.
    c                    t        | j                  t        |   t        |   | j
                  | j                        }t        |j                               S )N)r   r3   r   r   exc)	r   r   superr3   r   r   r   r   r;   )r   page	__class__s     r   r6   zHTMLRenderer.full   sC    **'-LL
 DKKM""r    c                "    | j                         S r   r6   r%   s    r   r7   zHTMLRenderer.minimal   s    yy{r    rC   )rD   rE   rF   rG   r6   r7   __classcell__rQ   s   @r   rL   rL      s    
#r    rL   c                  V     e Zd ZdZdZdZd
dZd
dZe fd       Z	d Z
d Zd	 Z xZS )TextRendererz"Render an exception as plain text.z{title}
{bar}
{text}

{body}z  c           
         t        | j                  j                  | j                  | j                   dt	        | j                        z  | j                  d                  S )N=TrS   r3   r   barbodyr   OUTPUT_TEXTformatr3   len_generate_bodyr%   s    r   r6   zTextRenderer.full   sW    ##jjYY3tzz?*((d(3	 $ 
 	
r    c           
         t        | j                  j                  | j                  | j                   dt	        | j                        z  | j                  d                  S )NrY   FrS   rZ   r]   r%   s    r   r7   zTextRenderer.minimal   sW    ##jjYY3tzz?*((e(4	 $ 
 	
r    c                    dt         |    S )Nu   ⚠️ )rO   r3   )r   rQ   s    r   r3   zTextRenderer.title   s    ((r    c               D   g }|rt        j                         \  }}}g }|| j                  j                  j                   d| j                   d| j
                  j                   d| j
                  j                  j                   dgz  }|r/|j                  | j                  |             |j                  }|r/||d d d   z  }ddt        |      ffD ]7  \  }}t        | j                  |d       }	|	s |s#|| j                  |	|      z  }9 dj                  |      S )	N: z while handling path zTraceback of z (most recent call last):
contextTextra
)sysexc_infor   rQ   rD   r   pathappnameappend_format_exc	__cause__rA   r$   _generate_object_display_listjoin)
r   r6   lines_	exc_value__
exceptionsattrdisplayinfos
             r   ra   zTextRenderer._generate_body   s3   "||~Ay"J>>++445R7G H!!%!2!2 35 0 0 5 56 7- - E !!$"2"29"=>%//	  Z"%%E/'4:1FG 	HMD'4>>46D;;D$GG	H
 yyr    c                h   dj                  t        |j                        D cg c]V  }| j                  dz   d|j                   d|j
                   d|j                   d| j                  dz   |j                   
X c}      }| j                   |j                  j                   d| d| S c c}w )Nz

   zFile z, line z, in rj   re   )
rt   r   __traceback__SPACERfilenamelinenoro   linerQ   rD   )r   rN   frameframess       r   rq   zTextRenderer._format_exc   s    
 ((9(9:	  ;;?#5(8 9~U::,bq 1%**?
 ++s}}556bRxHHs   AB/c                    d|j                          g}|j                         D ];  \  }}| j                  |      }|j                  | j                  dz   | d|        = |S )Nrj   r~   re   )r3   itemsr   rp   r   )r   obj
descriptorru   keyvaluer{   s          r   rs   z*TextRenderer._generate_object_display_list   sm    j&&()*+))+ 	?JCjj'GLLDKK!O,SEG9=>	? r    rC   )rD   rE   rF   rG   r^   r   r6   r7   rI   r3   ra   rq   rs   rT   rU   s   @r   rW   rW      s>    ,4KF

 ) ) 4	Ir    rW   c                  6    e Zd ZdZddZddZd Zed        Zy)JSONRendererzRender an exception as JSON.c                T    | j                  d      }t        || j                        S )NTrS   r   _generate_outputr   r   r9   s     r   r6   zJSONRenderer.full   s&    &&D&1F$**--r    c                T    | j                  d      }t        || j                        S )NFrS   r   r   r9   s     r   r7   zJSONRenderer.minimal   s&    &&E&2F$**--r    c                  | j                   | j                  | j                  d}ddt        |      ffD ]'  \  }}t	        | j
                  |d       }|s |s#|||<   ) |rt        j                         \  }}}g }	|r|	j                  |j                  j                  t        |      t        |j                        D 
cg c]1  }
|
j                  |
j                  |
j                   |
j"                  d3 c}
d       |j$                  }|r| j&                  j(                  |d<   | j&                  j*                  |d<   |	d d d   |d	<   |S c c}
w )
N)descriptionr)   messagerg   ri   )filer   ro   src)typer   r   rm   argsrf   ry   )r3   r)   r   rA   r$   r   rk   rl   rp   rQ   rD   r+   r   r   r   r   ro   r   rr   r   rm   r   )r   r6   r:   rz   r{   r|   rv   rw   rx   ry   r   s              r   r   zJSONRenderer._generate_output   sC   ::kkyy
 0'4:1FG 	$MD'4>>46D#t	$
 "||~Ay"J!! ) 3 3 < <%(^ *4I4K4K)L# !& ).(-(-

',zz	# &//	! $ "\\..F6N!\\..F6N#-dd#3F< ##s   =6E
c                ^    t        j                  | j                  d      j                         S )Nr.   r/   r%   s    r   r3   zJSONRenderer.title  s#    ->?FFHHr    NrC   )	rD   rE   rF   rG   r6   r7   r   rI   r3   rJ   r    r   r   r      s+    &..&P I Ir    r   c                H    |  j                  dd      j                  dd      S )zEMinimal HTML escaping, not for attribute values (unlike html.escape).&z&amp;<z&lt;)replace)r   s    r   escaper     s$    VS'*223??r    
text/plain	text/html)r   r   r   )r   r   zmultipart/form-datar   r   r   r   )r   r   r   JSONResponser   r   r   c                <    | t         vr| dk7  rt        d|        yy)zCheck that the format is known.r   zUnknown format: N)MIME_BY_CONFIGr   )r_   s    r   check_error_formatr   >  s,    ^#&(8/x899 )9#r    c                    |s"t        | |      }t        j                  ||      }t        j                  t        j
                  t           |      } || ||      j                         S )z=Render a response for the default FALLBACK exception handler.)
guess_mimeRENDERERS_BY_CONTENT_TYPEr0   tcastTyper   r;   )r   r   r   fallbackbaserenderermts          r   exception_responser   D  sV     *,00T:vvaff\*H5HGY.5577r    c                   i }d}| j                   rC| j                   j                  }| j                   j                  j                  }|t        v r|||<   |s|t        v rd||<   |s|dk(  r| j
                  j                  t              rft        | j
                  v rd|d<   nNt        | j                  j                  dd      v rd|d<   n&d}	 | j                  }|rd|d<   t        d	| d
d       |dk(  rt        D ]  }||vsd||<    |D cg c]  }t        |    }} | j
                  j                  | }|rLt        |j                     }	||	   }
t        j                   d|j"                   d|	 d|
        |j                  S t        j                   d| j
                         |j                  S # t        $ r Y w xY wc c}w )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@anyzThe client accepts z	, using 'z' from z$No format found, the client accepts )routero   ri   error_formatr   acceptmatchJSONr"   getoner   r   r	   CONFIG_BY_MIMEmimer
   r   header)reqr   formatsro   fckmimesmr_   sources              r   r   r   U  s    GD
yyyy~~IIOO((GAJx>13 x6)cjj.>.>t.D3::.GFOS[['';;,GFO AHH "06 7;V <CC  6 	#A"
	# )001^A0E0

% A'!!((9VHGF8L	

 66M 	;CJJ>JK66M=  ( 1s   F4 G4	G ?G r   )r   r   r   r@   r   rA   r   r+   r   zt.Type[BaseRenderer]r   z t.Optional[t.Type[BaseRenderer]]rB   r   )r   r   r   r+   rB   r+   )1rG   
__future__r   rk   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   rL   rW   r   r   r   r   r   r   RESPONSE_MAPPINGr   r   r   )r   vs   00r   <module>r      s   # 
     7 & ) ' + +  E%8E ??+ T" T"n< (H< HV5I< 5Ip@ 
 $2#7#7#9:41a!Q$:$'	   : 26888 8 	8
 8 /8 8"9e	  L ;s   
C: =D:DD