deprecate add_custom_404_page (#4505)

* deprecate add_custom_404_page

* show raw value in deprecate message

* fix typo

* Update reflex/app.py

Co-authored-by: Masen Furer <m_github@0x26.net>

* change removal version to 0.8.0

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
This commit is contained in:
Thomas Brandého 2024-12-11 18:29:39 -08:00 committed by GitHub
parent a7151cd6c8
commit a86d2c612a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 11 deletions

View File

@ -468,7 +468,7 @@ class App(MiddlewareMixin, LifespanMixin):
def add_page(
self,
component: Component | ComponentCallable,
component: Component | ComponentCallable | None = None,
route: str | None = None,
title: str | Var | None = None,
description: str | Var | None = None,
@ -491,17 +491,33 @@ class App(MiddlewareMixin, LifespanMixin):
meta: The metadata of the page.
Raises:
ValueError: When the specified route name already exists.
PageValueError: When the component is not set for a non-404 page.
RouteValueError: When the specified route name already exists.
"""
# If the route is not set, get it from the callable.
if route is None:
if not isinstance(component, Callable):
raise ValueError("Route must be set if component is not a callable.")
raise exceptions.RouteValueError(
"Route must be set if component is not a callable."
)
# Format the route.
route = format.format_route(component.__name__)
else:
route = format.format_route(route, format_case=False)
if route == constants.Page404.SLUG:
if component is None:
component = Default404Page.create()
component = wait_for_client_redirect(self._generate_component(component))
title = title or constants.Page404.TITLE
description = description or constants.Page404.DESCRIPTION
image = image or constants.Page404.IMAGE
else:
if component is None:
raise exceptions.PageValueError(
"Component must be set for a non-404 page."
)
# Check if the route given is valid
verify_route_validity(route)
@ -517,7 +533,7 @@ class App(MiddlewareMixin, LifespanMixin):
if route == constants.PageNames.INDEX_ROUTE
else f"`{route}`"
)
raise ValueError(
raise exceptions.RouteValueError(
f"Duplicate page route {route_name} already exists. Make sure you do not have two"
f" pages with the same route"
)
@ -634,10 +650,14 @@ class App(MiddlewareMixin, LifespanMixin):
on_load: The event handler(s) that will be called each time the page load.
meta: The metadata of the page.
"""
if component is None:
component = Default404Page.create()
console.deprecate(
feature_name="App.add_custom_404_page",
reason=f"Use app.add_page(component, route='/{constants.Page404.SLUG}') instead.",
deprecation_version="0.6.7",
removal_version="0.8.0",
)
self.add_page(
component=wait_for_client_redirect(self._generate_component(component)),
component=component,
route=constants.Page404.SLUG,
title=title or constants.Page404.TITLE,
image=image or constants.Page404.IMAGE,
@ -838,7 +858,7 @@ class App(MiddlewareMixin, LifespanMixin):
# Render a default 404 page if the user didn't supply one
if constants.Page404.SLUG not in self.unevaluated_pages:
self.add_custom_404_page()
self.add_page(route=constants.Page404.SLUG)
# Fix up the style.
self.style = evaluate_style_namespaces(self.style)

View File

@ -24,7 +24,7 @@ class ClientSideRouting(Component):
library = "$/utils/client_side_routing"
tag = "useClientSideRouting"
def add_hooks(self) -> list[str]:
def add_hooks(self) -> list[str | Var]:
"""Get the hooks to render.
Returns:
@ -66,4 +66,4 @@ class Default404Page(Component):
tag = "Error"
is_default = True
status_code: Var[int] = 404 # type: ignore
status_code: Var[int] = Var.create(404)

View File

@ -13,7 +13,7 @@ from reflex.vars.base import Var
route_not_found: Var
class ClientSideRouting(Component):
def add_hooks(self) -> list[str]: ...
def add_hooks(self) -> list[str | Var]: ...
def render(self) -> str: ...
@overload
@classmethod

View File

@ -63,6 +63,10 @@ class UploadValueError(ReflexError, ValueError):
"""Custom ValueError for upload related errors."""
class PageValueError(ReflexError, ValueError):
"""Custom ValueError for page related errors."""
class RouteValueError(ReflexError, ValueError):
"""Custom ValueError for route related errors."""