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: adds 404 redirect option #46

Open
wants to merge 2 commits 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
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ default_language_version:
python: "3.11"
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.3.0
rev: v3.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-case-conflict
Expand All @@ -17,7 +17,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.5.4"
rev: "v0.6.9"
hooks:
- id: ruff
args: ["--fix"]
Expand All @@ -27,7 +27,7 @@ repos:
hooks:
- id: codespell
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.11.0"
rev: "v1.11.2"
hooks:
- id: mypy
exclude: "docs"
Expand All @@ -41,6 +41,6 @@ repos:
"litestar[cli,jinja]",
]
- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: "v0.9.1"
rev: "v1.0.0"
hooks:
- id: sphinx-lint
2 changes: 2 additions & 0 deletions litestar_vite/inertia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class InertiaConfig:
"""An identifier to use on routes to exclude a route from the generated routes typescript file."""
redirect_unauthorized_to: str | None = None
"""Optionally supply a path where unauthorized requests should redirect."""
redirect_404: str | None = None
"""Optionally supply a path where unauthorized requests should redirect."""
extra_static_page_props: dict[str, Any] = field(default_factory=dict)
"""A dictionary of values to automatically add in to page props on every response."""
extra_session_page_props: set[str] = field(default_factory=set)
Expand Down
17 changes: 13 additions & 4 deletions litestar_vite/inertia/exception_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, cast

from litestar import MediaType
from litestar.connection import Request
from litestar.connection.base import AuthT, StateT, UserT
from litestar.exceptions import (
HTTPException,
ImproperlyConfiguredException,
Expand All @@ -22,10 +22,11 @@
NotFoundError, # pyright: ignore[reportUnknownVariableType,reportAttributeAccessIssue]
RepositoryError, # pyright: ignore[reportUnknownVariableType,reportAttributeAccessIssue]
)
from litestar.response import Response
from litestar.status_codes import (
HTTP_400_BAD_REQUEST,
HTTP_401_UNAUTHORIZED,
HTTP_404_NOT_FOUND,
HTTP_405_METHOD_NOT_ALLOWED,
HTTP_409_CONFLICT,
HTTP_422_UNPROCESSABLE_ENTITY,
HTTP_500_INTERNAL_SERVER_ERROR,
Expand All @@ -34,6 +35,10 @@
from litestar_vite.inertia.response import InertiaBack, InertiaRedirect, InertiaResponse, error

if TYPE_CHECKING:
from litestar.connection import Request
from litestar.connection.base import AuthT, StateT, UserT
from litestar.response import Response

from litestar_vite.inertia.plugin import InertiaPlugin

FIELD_ERR_RE = re.compile(r"field `(.+)`$")
Expand Down Expand Up @@ -69,7 +74,7 @@ def create_inertia_exception_response(request: Request[UserT, AuthT, StateT], ex
preferred_type = MediaType.HTML if not is_inertia else MediaType.JSON
detail = getattr(exc, "detail", "") # litestar exceptions
extras = getattr(exc, "extra", "") # msgspec exceptions
content = {"status_code": status_code, "message": getattr(exc, "detail", "")}
content: dict[str, Any] = {"status_code": status_code, "message": getattr(exc, "detail", "")}
inertia_plugin = cast("InertiaPlugin", request.app.plugins.get("InertiaPlugin"))
if extras:
content.update({"extra": extras})
Expand All @@ -95,6 +100,10 @@ def create_inertia_exception_response(request: Request[UserT, AuthT, StateT], ex
and str(request.url) != inertia_plugin.config.redirect_unauthorized_to
):
return InertiaRedirect(request, redirect_to=inertia_plugin.config.redirect_unauthorized_to)
if status_code in {HTTP_404_NOT_FOUND, HTTP_405_METHOD_NOT_ALLOWED} and (
inertia_plugin.config.redirect_404 is not None and str(request.url) != inertia_plugin.config.redirect_404
):
return InertiaRedirect(request, redirect_to=inertia_plugin.config.redirect_404)
return InertiaResponse[Any](
media_type=preferred_type,
content=content,
Expand Down
Loading