o
    NK&h0                     @   sB   d dl mZmZmZ d dlmZ d dlmZ G dd dedZdS )    )AnyCallableUnion)	SanicMeta)FutureExceptionc                   @   sv   e Zd ZdddZdefddZdd	d
eee e	ee  f de
defddZdedef dedef fddZdS )ExceptionMixinreturnNc                 O   s   t  | _d S N)set_future_exceptions)selfargskwargs r   J/var/www/html/venv/lib/python3.10/site-packages/sanic/mixins/exceptions.py__init__   s   zExceptionMixin.__init__handlerc                 C   s   t r	   )NotImplementedErrorr   r   r   r   r   _apply_exception_handler   s   z'ExceptionMixin._apply_exception_handlerT)apply
exceptionsr   c                   s    fdd}|S )a[  Decorator used to register an exception handler for the current application or blueprint instance.

        This method allows you to define a handler for specific exceptions that
        may be raised within the routes of this blueprint. You can specify one
        or more exception types to catch, and the handler will be applied to
        those exceptions.

        When used on a Blueprint, the handler will only be applied to routes
        registered under that blueprint. That means they only apply to
        requests that have been matched, and the exception is raised within
        the handler function (or middleware) for that route.

        A general exception like `NotFound` should only be registered on the
        application instance, not on a blueprint.

        See [Exceptions](/en/guide/best-practices/exceptions.html) for more information.

        Args:
            exceptions (Union[Type[Exception], List[Type[Exception]]]): List of
                Python exceptions to be caught by the handler.
            apply (bool, optional): Whether the exception handler should be
                applied. Defaults to True.

        Returns:
            Callable: A decorated method to handle global exceptions for any route
                registered under this blueprint.

        Example:
            ```python
            from sanic import Blueprint, text

            bp = Blueprint('my_blueprint')

            @bp.exception(Exception)
            def handle_exception(request, exception):
                return text("Oops, something went wrong!", status=500)
            ```

            ```python
            from sanic import Sanic, NotFound, text

            app = Sanic('MyApp')

            @app.exception(NotFound)
            def ignore_404s(request, exception):
                return text(f"Yep, I totally found the page: {request.url}")
        c                    s>   t d trt t| }j|  r| | S )Nr   )
isinstancelisttupler   r   addr   )r   future_exceptionr   r   r   r   r   	decoratorC   s   

z+ExceptionMixin.exception.<locals>.decoratorr   )r   r   r   r   r   r   r   	exception   s   5zExceptionMixin.exception.c                 C   s   |  t|S )a$  Enables the process of creating a global exception handler as a convenience.

        This following two examples are equivalent:

        ```python
        @app.exception(Exception)
        async def handler(request: Request, exception: Exception) -> HTTPResponse:
            return text(f"Exception raised: {exception}")
        ```

        ```python
        @app.all_exceptions
        async def handler(request: Request, exception: Exception) -> HTTPResponse:
            return text(f"Exception raised: {exception}")
        ```

        Args:
            handler (Callable[..., Any]): A coroutine function to handle exceptions.

        Returns:
            Callable[..., Any]: A decorated method to handle global exceptions for
                any route registered under this blueprint.
        )r   	Exceptionr   r   r   r   all_exceptionsR   s   zExceptionMixin.all_exceptions)r   N)__name__
__module____qualname__r   r   r   r   typer    r   boolr   r   r   r!   r   r   r   r   r      s     

D

r   )	metaclassN)	typingr   r   r   sanic.base.metar   sanic.models.futuresr   r   r   r   r   r   <module>   s    