so1n/pait

Pait[0.9.0] Support custom enable `TipException` to wrap exceptions that occur in pait

so1n opened this issue · 0 comments

so1n commented

TipException can be a good indication to the developer of which route the current error is generated by, as follows:

from starlette.responses import JSONResponse

from pait.app.starlette import pait
from pait.field import Header


@pait()
def demo(token: str = Header.i()) -> JSONResponse:
    return JSONResponse({"token": token})


if __name__ == "__main__":
    import uvicorn
    from starlette.applications import Starlette


    app: Starlette = Starlette()
    app.add_route("/api/demo", demo, methods=["GET"])
    uvicorn.run(app)

This route throws a TipException when handling a request without a token parameter:
image
If you set the Logging Debug to True, some logs will be output to tell the developer the exact location of the exception, as follows
image

Although the original exception can be obtained through TipException.exc, in some cases it is necessary to get the original exception directly or to be able to use a custom TipException.