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

fix: display system error message when rsconnect.environment inspection fails #639

Open
wants to merge 3 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
23 changes: 18 additions & 5 deletions rsconnect/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,16 +1716,29 @@ def inspect_environment(
flags: list[str] = []
if force_generate:
flags.append("f")

args = [python, "-m", "rsconnect.environment"]
if len(flags) > 0:
if flags:
args.append("-" + "".join(flags))
args.append(directory)

try:
environment_json = check_output(args, universal_newlines=True)
except subprocess.CalledProcessError as e:
raise RSConnectException("Error inspecting environment: %s" % e.output)
return MakeEnvironment(**json.loads(environment_json))
environment_json = check_output(args, text=True)
except Exception as e:
raise RSConnectException("Error inspecting environment (subprocess failed)") from e

try:
environment_data = json.loads(environment_json)
except json.JSONDecodeError as e:
raise RSConnectException("Error parsing environment JSON") from e

try:
return MakeEnvironment(**environment_data)
except TypeError as e:
system_error_message = environment_data.get("error")
if system_error_message:
raise RSConnectException(f"Error creating environment: {system_error_message}") from e
raise RSConnectException("Error constructing environment object") from e


def get_python_env_info(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,13 @@ def test_inspect_environment(self):
assert environment is not None
assert environment.python != ""

def test_inspect_environment_catches_type_error(self):
with pytest.raises(RSConnectException) as exec_info:
inspect_environment(sys.executable, None) # type: ignore

assert isinstance(exec_info.value, RSConnectException)
assert isinstance(exec_info.value.__cause__, TypeError)


@pytest.mark.parametrize(
(
Expand Down
Loading