From a86d2c612ac00598da2adb1eea33e5ce3f0579f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Wed, 11 Dec 2024 18:29:39 -0800 Subject: [PATCH] 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 * change removal version to 0.8.0 --------- Co-authored-by: Masen Furer --- reflex/app.py | 36 ++++++++++++++----- reflex/components/core/client_side_routing.py | 4 +-- .../components/core/client_side_routing.pyi | 2 +- reflex/utils/exceptions.py | 4 +++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/reflex/app.py b/reflex/app.py index cf4eefa19..42808823a 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -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) diff --git a/reflex/components/core/client_side_routing.py b/reflex/components/core/client_side_routing.py index 342c69632..a10b90de8 100644 --- a/reflex/components/core/client_side_routing.py +++ b/reflex/components/core/client_side_routing.py @@ -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) diff --git a/reflex/components/core/client_side_routing.pyi b/reflex/components/core/client_side_routing.pyi index bb853e2c7..581b0e120 100644 --- a/reflex/components/core/client_side_routing.pyi +++ b/reflex/components/core/client_side_routing.pyi @@ -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 diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index a89c4d4aa..6c378e159 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -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."""