sanic.handlers.content_range.ContentRangeHandler#

Parse and process the incoming request headers to extract the content range information.

Inherits from: Range, Protocol, Generic

class ContentRangeHandler(request: Request, stats: os.stat_result): -> None

Parameters
request
Request

The incoming request object.

stats
os.stat_result

The stats of the file being served.

sanic.handlers.directory.DirectoryHandler#

Serve files from a directory.

class DirectoryHandler(uri: str, directory: Path, directory_view: bool = False, index: Optional[Union[str, Sequence[str]]] = None): -> None

Parameters
uri
str

The URI to serve the files at.

directory
Path

The directory to serve files from.

directory_view
bool

Whether to show a directory listing or not.

index
Optional[Union[str, Sequence[str]]]

The index file(s) to serve if the directory is requested. Defaults to None.

handle#

Handle the request.

async def handle(self, request: Request, path: str)

Parameters
request
Request

The incoming request object.

path
str

The path to the file to serve.

Return
Response

The response object.

Raises
NotFound

If the file is not found.

IsADirectoryError

If the path is a directory and directory_view is False.

sanic.handlers.error.ErrorHandler#

Process and handle all uncaught exceptions.

class ErrorHandler(base: Type[BaseRenderer] = <class 'sanic.errorpages.TextRenderer'>)

This error handling framework is built into the core that can be extended by the developers to perform a wide range of tasks from recording the error stats to reporting them to an external service that can be used for realtime alerting system.

Parameters
base
BaseRenderer

The renderer to use for the error pages.

add#

Add a new exception handler to an already existing handler object.

def add(self, exception, handler, route_names: Optional[List[str]] = None)

Parameters
exception
sanic.exceptions.SanicException or Exception

Type of exception that needs to be handled.

handler
function

Reference to the function that will handle the exception.

default#

Provide a default behavior for the objects of ErrorHandler.

def default(self, request: Request, exception: Exception): -> HTTPResponse

If a developer chooses to extend the ErrorHandler, they can provide a custom implementation for this method to behave in a way they see fit.

Parameters
request
sanic.request.Request

Incoming request.

exception
sanic.exceptions.SanicException or Exception

Exception object.

Return
HTTPResponse

The response object.

Examples
class CustomErrorHandler(ErrorHandler):
    def default(self, request: Request, exception: Exception) -> HTTPResponse:
        # Custom logic for handling the exception and creating a response
        custom_response = my_custom_logic(request, exception)
        return custom_response

app = Sanic("MyApp", error_handler=CustomErrorHandler())

log#

Logs information about an incoming request and the associated exception.

@staticmethod
def log(request: Request, exception: Exception): -> None

Parameters
request
Request

The incoming request to be logged.

exception
Exception

The exception that occurred during the handling of the request.

lookup#

Lookup the existing instance of ErrorHandler and fetch the registered handler for a specific type of exception.

def lookup(self, exception, route_name: Optional[str] = None)

This method leverages a dict lookup to speedup the retrieval process.

Parameters
exception
sanic.exceptions.SanicException or Exception

Type of exception.

response#

Fetch and executes an exception handler and returns a response object.

def response(self, request, exception)

Parameters
request
sanic.request.Request

Instance of the request.

exception
sanic.exceptions.SanicException or Exception

Exception to handle.