Skip to content

Commit

Permalink
deprecate add_custom_404_page (#4505)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Lendemor and masenf authored Dec 12, 2024
1 parent a7151cd commit a86d2c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
36 changes: 28 additions & 8 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def _generate_component(component: Component | ComponentCallable) -> Component:

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,
Expand All @@ -491,17 +491,33 @@ def add_page(
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)

Expand All @@ -517,7 +533,7 @@ def add_page(
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"
)
Expand Down Expand Up @@ -634,10 +650,14 @@ def add_custom_404_page(
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,
Expand Down Expand Up @@ -838,7 +858,7 @@ def get_compilation_time() -> str:

# 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)
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/core/client_side_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion reflex/components/core/client_side_routing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down

0 comments on commit a86d2c6

Please sign in to comment.