From 3c0cd3cbc8bbd1a38582fe37803228f2413de118 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Tue, 28 Nov 2017 10:08:01 +0100 Subject: [PATCH] Improve error handling of loading Slic3r profiles. --- lib/Slic3r/GUI.pm | 10 ++++++++-- xs/src/slic3r/GUI/Preset.cpp | 18 +++++++++++------- xs/src/slic3r/GUI/PresetBundle.cpp | 29 ++++++++++++++++++++++------- xs/xsp/GUI_Preset.xsp | 2 +- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/Slic3r/GUI.pm b/lib/Slic3r/GUI.pm index baba3baa7..c65793d4b 100644 --- a/lib/Slic3r/GUI.pm +++ b/lib/Slic3r/GUI.pm @@ -93,9 +93,15 @@ sub OnInit { $self->{preset_bundle}->set_default_suppressed($self->{app_config}->get('no_defaults') ? 1 : 0); eval { $self->{preset_bundle}->load_presets(Slic3r::data_dir); - $self->{preset_bundle}->load_selections($self->{app_config}); - $run_wizard = 1 if $self->{preset_bundle}->has_defauls_only; }; + if ($@) { + warn $@ . "\n"; + show_error(undef, $@); + } + eval { + $self->{preset_bundle}->load_selections($self->{app_config}); + }; + $run_wizard = 1 if $self->{preset_bundle}->has_defauls_only; # application frame Wx::Image::AddHandler(Wx::PNGHandler->new); diff --git a/xs/src/slic3r/GUI/Preset.cpp b/xs/src/slic3r/GUI/Preset.cpp index 5a9ceb04b..214c111a4 100644 --- a/xs/src/slic3r/GUI/Preset.cpp +++ b/xs/src/slic3r/GUI/Preset.cpp @@ -119,10 +119,10 @@ DynamicPrintConfig& Preset::load(const std::vector &keys) try { this->config.load_from_ini(this->file); Preset::normalize(this->config); - } catch (const std::ifstream::failure&) { - throw std::runtime_error(std::string("The selected preset does not exist anymore: ") + this->file); - } catch (const std::runtime_error&) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + this->file); + } catch (const std::ifstream::failure &err) { + throw std::runtime_error(std::string("The selected preset cannot be loaded: ") + this->file + "\n\tReason: " + err.what()); + } catch (const std::runtime_error &err) { + throw std::runtime_error(std::string("Failed loading the preset file: ") + this->file + "\n\tReason: " + err.what()); } } this->loaded = true; @@ -250,6 +250,7 @@ void PresetCollection::load_presets(const std::string &dir_path, const std::stri m_dir_path = dir.string(); m_presets.erase(m_presets.begin()+1, m_presets.end()); t_config_option_keys keys = this->default_preset().config.keys(); + std::string errors_cummulative; for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) if (boost::filesystem::is_regular_file(dir_entry.status()) && boost::algorithm::iends_with(dir_entry.path().filename().string(), ".ini")) { std::string name = dir_entry.path().filename().string(); @@ -260,12 +261,15 @@ void PresetCollection::load_presets(const std::string &dir_path, const std::stri preset.file = dir_entry.path().string(); preset.load(keys); m_presets.emplace_back(preset); - } catch (const boost::filesystem::filesystem_error &err) { - - } + } catch (const std::runtime_error &err) { + errors_cummulative += err.what(); + errors_cummulative += "\n"; + } } std::sort(m_presets.begin() + 1, m_presets.end()); this->select_preset(first_visible_idx()); + if (! errors_cummulative.empty()) + throw std::runtime_error(errors_cummulative); } // Load a preset from an already parsed config file, insert it into the sorted sequence of presets diff --git a/xs/src/slic3r/GUI/PresetBundle.cpp b/xs/src/slic3r/GUI/PresetBundle.cpp index 00e35683a..67a7c15cc 100644 --- a/xs/src/slic3r/GUI/PresetBundle.cpp +++ b/xs/src/slic3r/GUI/PresetBundle.cpp @@ -78,10 +78,25 @@ void PresetBundle::setup_directories() void PresetBundle::load_presets(const std::string &dir_path) { - this->prints .load_presets(dir_path, "print"); - this->filaments.load_presets(dir_path, "filament"); - this->printers .load_presets(dir_path, "printer"); + std::string errors_cummulative; + try { + this->prints.load_presets(dir_path, "print"); + } catch (const std::runtime_error &err) { + errors_cummulative += err.what(); + } + try { + this->filaments.load_presets(dir_path, "filament"); + } catch (const std::runtime_error &err) { + errors_cummulative += err.what(); + } + try { + this->printers.load_presets(dir_path, "printer"); + } catch (const std::runtime_error &err) { + errors_cummulative += err.what(); + } this->update_multi_material_filament_presets(); + if (! errors_cummulative.empty()) + throw std::runtime_error(errors_cummulative); } static inline std::string remove_ini_suffix(const std::string &name) @@ -233,10 +248,10 @@ void PresetBundle::load_config_file(const std::string &path) try { boost::nowide::ifstream ifs(path); boost::property_tree::read_ini(ifs, tree); - } catch (const std::ifstream::failure&) { - throw std::runtime_error(std::string("The config file cannot be loaded: ") + path); - } catch (const std::runtime_error&) { - throw std::runtime_error(std::string("Failed loading the preset file: ") + path); + } catch (const std::ifstream::failure &err) { + throw std::runtime_error(std::string("The config file cannot be loaded: ") + path + "\n\tReason: " + err.what()); + } catch (const std::runtime_error &err) { + throw std::runtime_error(std::string("Failed loading the preset file: ") + path + "\n\tReason: " + err.what()); } // 2) Continue based on the type of the configuration file. diff --git a/xs/xsp/GUI_Preset.xsp b/xs/xsp/GUI_Preset.xsp index e1f8a72c4..a7fbdd07f 100644 --- a/xs/xsp/GUI_Preset.xsp +++ b/xs/xsp/GUI_Preset.xsp @@ -111,7 +111,7 @@ PresetCollection::arrayref() try { THIS->load_presets(dir_path); } catch (std::exception& e) { - croak("Loading of Slic3r presets from %s failed:\n%s\n", dir_path, e.what()); + croak("Loading of Slic3r presets from %s failed.\n\n%s\n", dir_path, e.what()); } %}; void load_config_file(const char *path)