Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(litestar): add default NotFoundError exception handler #294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions advanced_alchemy/extensions/litestar/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from litestar.plugins import InitPluginProtocol

from advanced_alchemy.exceptions import NotFoundError
from advanced_alchemy.extensions.litestar.plugins import _slots_base
from advanced_alchemy.extensions.litestar.plugins.exception_handlers import not_found_error_handler
from advanced_alchemy.extensions.litestar.plugins.init import (
EngineConfig,
SQLAlchemyAsyncConfig,
Expand Down Expand Up @@ -44,6 +46,10 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
app_config: The :class:`AppConfig <.config.app.AppConfig>` instance.
"""
app_config.plugins.extend([SQLAlchemyInitPlugin(config=self._config), SQLAlchemySerializationPlugin()])

if not app_config.exception_handlers.get(NotFoundError):
app_config.exception_handlers[NotFoundError] = not_found_error_handler

return app_config


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from litestar import MediaType, Request, Response

from advanced_alchemy.exceptions import NotFoundError


def not_found_error_handler(_request: Request, _exc: NotFoundError) -> Response:
return Response(
media_type=MediaType.JSON,
content={"detail": "Not Found", "status_code": 404},
status_code=404,
)
Loading