Skip to content

Commit

Permalink
load_plugins: Preserve original exception with failure information
Browse files Browse the repository at this point in the history
The original implementation threw an exception with a list of plugins
that failed to load. Usually we got the name of one configuration file.
Information about the reason for the failure was not included
in the exception.

Now the code throws an exception on the first failed plugin. This
exception includes the original exception from the source of the error.
  • Loading branch information
jrohel committed Feb 10, 2025
1 parent 071cd7e commit 672ea33
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
9 changes: 1 addition & 8 deletions dnf5/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,14 @@ void Plugins::load_plugins(const std::string & dir_path) {
}
std::sort(lib_paths.begin(), lib_paths.end());

std::string failed_filenames;
for (const auto & path : lib_paths) {
try {
load_plugin(path);
} catch (const std::exception & ex) {
logger->error("Cannot load dnf5 plugin \"{}\": {}", path.string(), ex.what());
if (!failed_filenames.empty()) {
failed_filenames += ", ";
}
failed_filenames += path.filename();
std::throw_with_nested(std::runtime_error("Cannot load dnf5 plugin: " + path.filename().string()));
}
}
if (!failed_filenames.empty()) {
throw std::runtime_error("Cannot load dnf5 plugins: " + failed_filenames);
}
}

bool Plugins::init() {
Expand Down
10 changes: 1 addition & 9 deletions libdnf5/plugin/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,15 @@ void Plugins::load_plugins(
}
std::sort(config_paths.begin(), config_paths.end());

std::string failed_filenames;
for (const auto & path : config_paths) {
try {
load_plugin(path, plugin_enablement);
} catch (const std::exception & ex) {
logger.error("Cannot load plugin \"{}\": {}", path.string(), ex.what());
if (!failed_filenames.empty()) {
failed_filenames += ", ";
}
failed_filenames += path.filename();
std::throw_with_nested(PluginError(M_("Cannot load plugin: {}"), path.filename().string()));
}
}

if (!failed_filenames.empty()) {
throw PluginError(M_("Cannot load plugins: {}"), failed_filenames);
}

// Creates a PluginInfo for each loaded plugin.
auto & plugins_info = InternalBaseUser::get_plugins_info(base);
for (const auto & plugin : plugins) {
Expand Down

0 comments on commit 672ea33

Please sign in to comment.