a
    ij                     @  s|   d Z ddlmZ ddlZddlmZmZmZmZm	Z	 e	e
eg e
f f Zddddddd	d
ZejdddddddZdS )zUtils to reraise.    )annotationsN)CallableIteratorNoReturnOptionalUnion	ExceptionzOptional[_Str]r   )eprefixsuffixreturnc                   s   t |r| n|}t |r | n|}|p*d}|r8d| nd}|   | }G  fdddt }t j|_t j|_t j|_||}| j jdS )a  Reraise an exception with an additional message.

  Benefit: Contrary to `raise ... from ...` and
  `raise Exception().with_traceback(tb)`, this function will:

  * Keep the original exception type, attributes,...
  * Avoid multi-nested `During handling of the above exception, another
    exception occurred`. Only the single original stacktrace is displayed.

  This result in cleaner and more compact error messages.

  Usage:

  ```
  try:
    fn(x)
  except Exception as e:
    epy.reraise(e, prefix=f'Error for {x}: ')
  ```

  Args:
    e: Exception to reraise
    prefix: Prefix to add to the exception message.
    suffix: Suffix to add to the exception message.
   
c                      s6   e Zd ZdZdd Zdd fddZejZejZdS )	z!reraise.<locals>.WrappedExceptionz(Exception proxy with additional message.c                 S  s   t | | d S N)r   __init__)selfmsg r   U/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/epy/reraise_utils.pyr   Q   s    z*reraise.<locals>.WrappedException.__init__str)namec                   s
   t  |S r   )getattr)r   r   r	   r   r   __getattr__V   s    z-reraise.<locals>.WrappedException.__getattr__N)	__name__
__module____qualname____doc__r   r   BaseException__repr____str__r   r   r   r   WrappedExceptionN   s
   r!   N)callabletyper   r   r   with_traceback__traceback__	__cause__)r	   r
   r   r   r!   Znew_exceptionr   r   r   reraise&   s    	r'   zIterator[None])r
   r   r   c              
   c  sB   z
dV  W n2 t y< } zt|| |d W Y d}~n
d}~0 0 dS )a  Context manager which reraise exceptions with an additional message.

  Benefit: Contrary to `raise ... from ...` and
  `raise Exception().with_traceback(tb)`, this function will:

  * Keep the original exception type, attributes,...
  * Avoid multi-nested `During handling of the above exception, another
    exception occurred`. Only the single original stacktrace is displayed.

  This result in cleaner and more compact error messages.

  Usage:

  ```python
  with epy.maybe_reraise(prefix=f'Error for {x}:'):
    fn(x)
  ```

  Args:
    prefix: Prefix to add to the exception message. Can be a function for
      lazy-evaluation.
    suffix: Suffix to add to the exception message. Can be a function for
      lazy-evaluation.

  Yields:
    None
  N)r
   r   )r   r'   )r
   r   r	   r   r   r   maybe_reraiseo   s     
r(   )NN)NN)r   
__future__r   
contextlibtypingr   r   r   r   r   r   Z_Strr'   contextmanagerr(   r   r   r   r   <module>   s     I  