From bfa7ca639f9f266c199a7ef89476b0f33ac2bd39 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 6 Nov 2024 09:21:04 -0800 Subject: [PATCH] better missing system package message (#4306) * better missing system package message * change error type --- reflex/constants/__init__.py | 2 ++ reflex/constants/base.py | 2 ++ reflex/utils/exceptions.py | 24 ++++++++++++++++++++++++ reflex/utils/prerequisites.py | 13 ++++++------- tests/units/utils/test_utils.py | 4 ++-- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/reflex/constants/__init__.py b/reflex/constants/__init__.py index a1233a3fa..a939cf273 100644 --- a/reflex/constants/__init__.py +++ b/reflex/constants/__init__.py @@ -2,6 +2,8 @@ from .base import ( COOKIES, + IS_LINUX, + IS_MACOS, IS_WINDOWS, LOCAL_STORAGE, POLLING_MAX_HTTP_BUFFER_SIZE, diff --git a/reflex/constants/base.py b/reflex/constants/base.py index 6ec73cdf0..5042b453e 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -13,6 +13,8 @@ from platformdirs import PlatformDirs from .utils import classproperty IS_WINDOWS = platform.system() == "Windows" +IS_MACOS = platform.system() == "Darwin" +IS_LINUX = platform.system() == "Linux" class Dirs(SimpleNamespace): diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index dc25a09e0..7611e5a4d 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -1,5 +1,7 @@ """Custom Exceptions.""" +from typing import NoReturn + class ReflexError(Exception): """Base exception for all Reflex exceptions.""" @@ -147,3 +149,25 @@ class DynamicComponentInvalidSignature(ReflexError, TypeError): class InvalidPropValueError(ReflexError): """Raised when a prop value is invalid.""" + + +class SystemPackageMissingError(ReflexError): + """Raised when a system package is missing.""" + + +def raise_system_package_missing_error(package: str) -> NoReturn: + """Raise a SystemPackageMissingError. + + Args: + package: The name of the missing system package. + + Raises: + SystemPackageMissingError: The raised exception. + """ + from reflex.constants import IS_MACOS + + raise SystemPackageMissingError( + f"System package '{package}' is missing." + " Please install it through your system package manager." + + (f" You can do so by running 'brew install {package}'." if IS_MACOS else "") + ) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 7124d46c4..063c2f594 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -35,7 +35,10 @@ from reflex import constants, model from reflex.compiler import templates from reflex.config import Config, environment, get_config from reflex.utils import console, net, path_ops, processes -from reflex.utils.exceptions import GeneratedCodeHasNoFunctionDefs +from reflex.utils.exceptions import ( + GeneratedCodeHasNoFunctionDefs, + raise_system_package_missing_error, +) from reflex.utils.format import format_library_name from reflex.utils.registry import _get_npm_registry @@ -820,11 +823,7 @@ def install_node(): def install_bun(): - """Install bun onto the user's system. - - Raises: - FileNotFoundError: If required packages are not found. - """ + """Install bun onto the user's system.""" win_supported = is_windows_bun_supported() one_drive_in_path = windows_check_onedrive_in_path() if constants.IS_WINDOWS and not win_supported or one_drive_in_path: @@ -863,7 +862,7 @@ def install_bun(): else: unzip_path = path_ops.which("unzip") if unzip_path is None: - raise FileNotFoundError("Reflex requires unzip to be installed.") + raise_system_package_missing_error("unzip") # Run the bun install script. download_and_run( diff --git a/tests/units/utils/test_utils.py b/tests/units/utils/test_utils.py index cc98c3ace..c04df9c75 100644 --- a/tests/units/utils/test_utils.py +++ b/tests/units/utils/test_utils.py @@ -19,7 +19,7 @@ from reflex.utils import ( types, ) from reflex.utils import exec as utils_exec -from reflex.utils.exceptions import ReflexError +from reflex.utils.exceptions import ReflexError, SystemPackageMissingError from reflex.vars.base import Var @@ -503,7 +503,7 @@ def test_bun_install_without_unzip(mocker): mocker.patch("pathlib.Path.exists", return_value=False) mocker.patch("reflex.utils.prerequisites.constants.IS_WINDOWS", False) - with pytest.raises(FileNotFoundError): + with pytest.raises(SystemPackageMissingError): prerequisites.install_bun()