Skip to content

Commit

Permalink
Handle exceptions when parsing replay JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kontura committed Jun 18, 2024
1 parent 7e4d571 commit e57adc9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion include/libdnf5/base/goal_elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ enum class GoalProblem : uint32_t {
MODULE_CANNOT_SWITH_STREAMS = (1 << 21),
/// Error when transaction contains additional unexpected elements.
/// Used when replaying transactions.
EXTRA = (1 << 22)
EXTRA = (1 << 22),
MALFORMED = (1 << 23)
};

/// Types of Goal actions
Expand All @@ -144,6 +145,7 @@ enum class GoalAction {
ENABLE,
DISABLE,
RESET,
REPLAY_PARSE,
REPLAY_INSTALL,
REPLAY_REMOVE,
REPLAY_UPGRADE,
Expand Down
23 changes: 19 additions & 4 deletions libdnf5/base/goal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ GoalProblem Goal::Impl::add_specs_to_goal(base::Transaction & transaction) {
case GoalAction::RESET: {
libdnf_throw_assertion("Unsupported action \"RESET\"");
}
case GoalAction::REPLAY_PARSE: {
libdnf_throw_assertion("Unsupported action \"REPLAY PARSE\"");
}
case GoalAction::REPLAY_INSTALL: {
libdnf_throw_assertion("Unsupported action \"REPLAY INSTALL\"");
}
Expand Down Expand Up @@ -661,10 +664,22 @@ GoalProblem Goal::Impl::add_serialized_transaction_to_goal(base::Transaction & t

auto & [replay_path, settings] = *serialized_transaction;
utils::fs::File replay_file(replay_path, "r");
auto replay_location = replay_path.remove_filename();
auto replay = transaction::parse_transaction_replay(replay_file.read());

return add_replay_to_goal(transaction, replay, settings, replay_location);
auto replay_location = replay_path;
replay_location.remove_filename();
try {
auto replay = transaction::parse_transaction_replay(replay_file.read());
return add_replay_to_goal(transaction, replay, settings, replay_location);
} catch (const libdnf5::transaction::TransactionReplayError & ex) {
transaction.p_impl->add_resolve_log(
GoalAction::REPLAY_PARSE,
libdnf5::GoalProblem::MALFORMED,
settings,
libdnf5::transaction::TransactionItemType::PACKAGE,
replay_path,
{ex.what()},
libdnf5::Logger::Level::ERROR);
return libdnf5::GoalProblem::MALFORMED;
}
}

static std::set<std::string> query_to_vec_of_nevra_str(const libdnf5::rpm::PackageQuery & query) {
Expand Down
2 changes: 2 additions & 0 deletions libdnf5/base/goal_elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ std::string goal_action_to_string(GoalAction action) {
return _("Disable");
case GoalAction::RESET:
return _("Reset");
case GoalAction::REPLAY_PARSE:
return _("Parse serialized transaction");
case GoalAction::REPLAY_INSTALL:
return _("Install action");
case GoalAction::REPLAY_REMOVE:
Expand Down
3 changes: 3 additions & 0 deletions libdnf5/base/log_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ std::string LogEvent::to_string(
*spec,
*additional_data.begin()));
}
case GoalProblem::MALFORMED: {
return ret.append(utils::sformat(_("Cannot parse file: '{0}': {1}.\n"), *spec, *additional_data.begin()));
}
}
return ret;
}
Expand Down

0 comments on commit e57adc9

Please sign in to comment.