o
    ,wi?                     @   sv   d Z ddlZddlZddlZddlmZ ddlmZ ejdddd Zde	fd	d
Z
ejdeg e	f fddZdS )a  Wraps exceptions with additional Fiddle-specific information.

Python provides an official solution, "raise FiddleException(...) from e", to do
this. Unfortunately, the stack traces highlight Fiddle internals in this case,
when often a user error (from `e`'s original stack trace) is more relevant.

Hence, we reraise a proxied version of the original exception that adds
additional Fiddle-specific context to the exception message. This approach was
inspired by Gin's exception logic.
    N)Callable)logging)maxsizec                    s(   G  fddd } j |_  j|_|S )z<Subclasses `exception_type` with one that appends a message.c                       s6   e Zd ZdZ jZdd Zdd Zdd Zdd	 Zd
S )z,make_exception_class.<locals>.ExceptionProxyz;Acts as a proxy for an exception with an augmented message.c                 S   s   || _ || _d S N)proxy_base_exceptionproxy_message)selfr   r    r	   [/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/fiddle/_src/reraised_exception.py__init__+   s   
z5make_exception_class.<locals>.ExceptionProxy.__init__c                 S   s   t | j|S r   )getattrr   )r   	attr_namer	   r	   r
   __getattr__/   s   z8make_exception_class.<locals>.ExceptionProxy.__getattr__c                 S   s   t | j| jffS r   )decorate_exceptionr   r   r   r	   r	   r
   
__reduce__2      z7make_exception_class.<locals>.ExceptionProxy.__reduce__c                 S   s   t | j| j S r   )strr   r   r   r	   r	   r
   __str__5   r   z4make_exception_class.<locals>.ExceptionProxy.__str__N)__name__
__module____qualname____doc__r   r   r   r   r	   exception_typer	   r
   ExceptionProxy'   s    r   )r   r   )r   r   r	   r   r
   make_exception_class#   s   r   messagec                 C   sB   zt t| }|| || jW S  ty    td |  Y S w )Nz Creating the proxy class failed.)r   typewith_traceback__traceback__	Exceptionr   	exception)r"   r   	proxy_clsr	   r	   r
   r   =   s   
r   lazy_messagec              
   c   sV    zdV  W dS  t y* } zz|  }W n   td |dt||dd}~ww )z*Context manager which reraises exceptions.Nz(Formatting the debug information failed.)r!   r   r"   r   )r$   excr   r	   r	   r
   try_with_lazy_messageF   s   

r&   )r   
contextlib	functoolsr   typingr   absl	lru_cacher   r   r   contextmanagerr&   r	   r	   r	   r
   <module>   s   

	