Skip to content

add_exception_handler have type annotation problems #2391

Discussion options

You must be logged in to vote

Nevermind, this works:

from starlette.applications import Request, Starlette
from starlette.responses import JSONResponse

class MyException(Exception):
    def __init__(self, message: str) -> None:
        self.message = message
        self.extra = "extra"

def my_exception_handler(request: Request, exc: MyException) -> JSONResponse:
    return JSONResponse({"detail": exc.message, "extra": exc.extra}, status_code=400)

handlers = {MyException: my_exception_handler}
app = Starlette(debug=True, routes=[], exception_handlers=handlers)

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@pohmelie
Comment options

@adriangb
Comment options

@pohmelie
Comment options

@pohmelie
Comment options

Answer selected by adriangb
@pohmelie
Comment options

@tekumara
Comment options

@tekumara
Comment options

@pohmelie
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants