4.5. ‫Exceptionها‬

Backendpy exceptions are the type of responses used to return HTTP errors and can also be raised.


As mentioned, Backendpy exceptions are the type of Response and their content is returned as a response and displayed to the user. Therefore, these exceptions should be used only for errors that must be displayed to users, and any kind of internal system error should be created with normal Python exceptions, in which case, the ServerError response is displayed to the user with a public message and does not contain sensitive system information that may be contained in the internal exception message.

The list of default exception response classes are as follows:

Example usage:

from backendpy.router import Routes
from backendpy.exception import BadRequest

routes = Routes()

async def login(request):
    raise BadRequest({'message': 'Login failed!'})