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

More details in error message when failing to parse json from a python command's output #3604

Merged
merged 4 commits into from
Nov 14, 2024
Merged
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
4 changes: 2 additions & 2 deletions libmamba/include/mamba/specs/build_number_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace mamba::specs
friend auto equal(free_interval, free_interval) -> bool;
friend auto
operator==(const BuildNumberPredicate& lhs, const BuildNumberPredicate& rhs) -> bool;
friend class ::fmt::formatter<BuildNumberPredicate>;
friend struct ::fmt::formatter<BuildNumberPredicate>;
};

auto operator==(const BuildNumberPredicate& lhs, const BuildNumberPredicate& rhs) -> bool;
Expand Down Expand Up @@ -140,7 +140,7 @@ namespace mamba::specs

BuildNumberPredicate m_predicate;

friend class ::fmt::formatter<BuildNumberSpec>;
friend struct ::fmt::formatter<BuildNumberSpec>;
};

namespace build_number_spec_literals
Expand Down
4 changes: 2 additions & 2 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace mamba::specs
friend auto operator==(not_starts_with, not_starts_with) -> bool;
friend auto operator==(compatible_with, compatible_with) -> bool;
friend auto operator==(const VersionPredicate& lhs, const VersionPredicate& rhs) -> bool;
friend class ::fmt::formatter<VersionPredicate>;
friend struct ::fmt::formatter<VersionPredicate>;
};

auto operator==(const VersionPredicate& lhs, const VersionPredicate& rhs) -> bool;
Expand Down Expand Up @@ -210,7 +210,7 @@ namespace mamba::specs

tree_type m_tree;

friend class ::fmt::formatter<VersionSpec>;
friend struct ::fmt::formatter<VersionSpec>;
};

namespace version_spec_literals
Expand Down
35 changes: 33 additions & 2 deletions libmamba/src/core/prefix_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <unordered_map>
#include <utility>

#include <fmt/ranges.h>
Hind-M marked this conversation as resolved.
Show resolved Hide resolved
#include <reproc++/run.hpp>

#include "mamba/core/channel_context.hpp"
#include "mamba/core/error_handling.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/prefix_data.hpp"
#include "mamba/core/util.hpp"
Expand Down Expand Up @@ -223,15 +225,27 @@ namespace mamba
reproc::options run_options;
run_options.env.extra = reproc::env{ env };

LOG_TRACE << "Running command: "
<< fmt::format("{}\n env options:{}", fmt::join(args, " "), fmt::join(env, " "));

auto [status, ec] = reproc::run(
args,
run_options,
reproc::sink::string(out),
reproc::sink::string(err)
);

if (ec)
{
throw std::runtime_error(ec.message());
const auto message = fmt::format(
Hind-M marked this conversation as resolved.
Show resolved Hide resolved
"failed to run python command :\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}",
ec.message(),
fmt::join(args, " "),
fmt::join(env, " "),
out,
err
);
throw mamba_error{ message, mamba_error_code::internal_failure };
}

// Nothing installed with `pip`
Expand All @@ -241,7 +255,24 @@ namespace mamba
return;
}

nlohmann::json j = nlohmann::json::parse(out);
LOG_TRACE << "Parsing `pip inspect` output:\n" << out;
nlohmann::json j;
try
{
j = nlohmann::json::parse(out);
}
catch (const std::exception& ec)
{
const auto message = fmt::format(
"failed to parse python command output:\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}",
ec.what(),
fmt::join(args, " "),
fmt::join(env, " "),
out,
err
);
throw mamba_error{ message, mamba_error_code::internal_failure };
}

if (j.contains("installed") && j["installed"].is_array())
{
Expand Down
Loading