o
    [hi                     @   sd   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ G dd dejjZdS )    N)run_in_threadpool)HTTPException)Request)MessageReceiveScopeSendc                   @   s&   e Zd ZdedededdfddZdS )ExceptionMiddlewarescopereceivesendreturnNc           
   
      s  |d dkr|  ||I dH  dS d dtddf fdd}z|  |||I dH  W dS  ty } zSd}t|trD| j|j}|du rM| |}|du rS| r\d	}t	||t
||d
}t|rp|||I dH }	n	t|||I dH }	|	|||I dH  W Y d}~dS d}~ww )zoTree just removed the 'from None'. everything else is the same
        Need this until https://github.com/encode/starlette/issues/1114 is fixed
        typehttpNFmessager   c                    s$   | d dkr	d | I d H  d S )Nr   zhttp.response.startT )r   Zresponse_startedr   r   r/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/patch.pysender   s   z,ExceptionMiddleware.__call__.<locals>.senderz7Caught handled exception, but response already started.)r   )Zappr   	Exception
isinstancer   Z_status_handlersgetstatus_codeZ_lookup_exception_handlerRuntimeErrorr   asyncioiscoroutinefunctionr   )
selfr
   r   r   r   exchandlermsgrequestresponser   r   r   __call__   s4   



 zExceptionMiddleware.__call__)__name__
__module____qualname__r   r   r   r"   r   r   r   r   r	   
   s    r	   )r   Zstarlette.exceptionsZ	starletteZstarlette.concurrencyr   r   Zstarlette.requestsr   Zstarlette.typesr   r   r   r   
exceptionsr	   r   r   r   r   <module>   s    