Skip to content

API Exception

The APIException is the root exception that you should inherit for errors that are thrown within action functions. This syntax allows your frontend to pick up on the error parameters. APIExceptions are just Pydantic BaseModels with a few helpful defaults, and a metaclass that allows them to work natively with HTTPExceptions.



Bases: HTTPException

Base class for user defined APIExceptions that can be thrown in server actions and should provide some metadata back to the client caller.

class PostNotFound(APIException):
    status_code: int = 404
    detail: str = "The post was not found"
    post_id: int
    is_deleted: bool

class MyController(ControllerBase):
    def get_post(self, post_id: int) -> Post:
        post = self.post_service.get_post(post_id)
        if not post:
            raise PostNotFound(post_id=post_id, is_deleted=True)
        return post

status_code class-attribute instance-attribute

status_code = 500

detail class-attribute instance-attribute

detail = 'A server error occurred'