Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVojtech Bubnik <bubnikv@gmail.com>2021-06-29 16:41:47 +0300
committerVojtech Bubnik <bubnikv@gmail.com>2021-06-29 16:41:57 +0300
commit745aa3536db8e305416db87dd3cdb9e0610acf56 (patch)
tree10ba7f8824ec41bd395a84e8f474c2d8d0c5e79f /src/libslic3r/Config.hpp
parent3cf73f6a066050bdf53bfb5eb67dfeff9ca533d5 (diff)
Improved error reporting when importing various configuration files:
1) Slic3r::RuntimeError was replaced with ConfigurationError, all exceptions thrown by the configuration layer are derived from ConfigurationError. 2) When parsing configuration files, ConfigurationError is catched and rethrown extended with the file name being parsed.
Diffstat (limited to 'src/libslic3r/Config.hpp')
-rw-r--r--src/libslic3r/Config.hpp121
1 files changed, 68 insertions, 53 deletions
diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp
index 6155c1012..d17d8befe 100644
--- a/src/libslic3r/Config.hpp
+++ b/src/libslic3r/Config.hpp
@@ -37,32 +37,47 @@ extern bool unescape_strings_cstyle(const std::string &str, std::vector<
extern std::string escape_ampersand(const std::string& str);
-/// Specialization of std::exception to indicate that an unknown config option has been encountered.
-class UnknownOptionException : public Slic3r::RuntimeError {
+// Base for all exceptions thrown by the configuration layer.
+class ConfigurationError : public Slic3r::RuntimeError {
+public:
+ using RuntimeError::RuntimeError;
+};
+
+// Specialization of std::exception to indicate that an unknown config option has been encountered.
+class UnknownOptionException : public ConfigurationError {
public:
UnknownOptionException() :
- Slic3r::RuntimeError("Unknown option exception") {}
+ ConfigurationError("Unknown option exception") {}
UnknownOptionException(const std::string &opt_key) :
- Slic3r::RuntimeError(std::string("Unknown option exception: ") + opt_key) {}
+ ConfigurationError(std::string("Unknown option exception: ") + opt_key) {}
};
-/// Indicate that the ConfigBase derived class does not provide config definition (the method def() returns null).
-class NoDefinitionException : public Slic3r::RuntimeError
+// Indicate that the ConfigBase derived class does not provide config definition (the method def() returns null).
+class NoDefinitionException : public ConfigurationError
{
public:
NoDefinitionException() :
- Slic3r::RuntimeError("No definition exception") {}
+ ConfigurationError("No definition exception") {}
NoDefinitionException(const std::string &opt_key) :
- Slic3r::RuntimeError(std::string("No definition exception: ") + opt_key) {}
+ ConfigurationError(std::string("No definition exception: ") + opt_key) {}
+};
+
+// Indicate that an unsupported accessor was called on a config option.
+class BadOptionTypeException : public ConfigurationError
+{
+public:
+ BadOptionTypeException() : ConfigurationError("Bad option type exception") {}
+ BadOptionTypeException(const std::string &message) : ConfigurationError(message) {}
+ BadOptionTypeException(const char* message) : ConfigurationError(message) {}
};
-/// Indicate that an unsupported accessor was called on a config option.
-class BadOptionTypeException : public Slic3r::RuntimeError
+// Indicate that an option has been deserialized from an invalid value.
+class BadOptionValueException : public ConfigurationError
{
public:
- BadOptionTypeException() : Slic3r::RuntimeError("Bad option type exception") {}
- BadOptionTypeException(const std::string &message) : Slic3r::RuntimeError(message) {}
- BadOptionTypeException(const char* message) : Slic3r::RuntimeError(message) {}
+ BadOptionValueException() : ConfigurationError("Bad option value exception") {}
+ BadOptionValueException(const std::string &message) : ConfigurationError(message) {}
+ BadOptionValueException(const char* message) : ConfigurationError(message) {}
};
// Type of a configuration value.
@@ -215,7 +230,7 @@ public:
void set(const ConfigOption *rhs) override
{
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionSingle: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionSingle: Assigning an incompatible type");
assert(dynamic_cast<const ConfigOptionSingle<T>*>(rhs));
this->value = static_cast<const ConfigOptionSingle<T>*>(rhs)->value;
}
@@ -223,7 +238,7 @@ public:
bool operator==(const ConfigOption &rhs) const override
{
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionSingle: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionSingle: Comparing incompatible types");
assert(dynamic_cast<const ConfigOptionSingle<T>*>(&rhs));
return this->value == static_cast<const ConfigOptionSingle<T>*>(&rhs)->value;
}
@@ -287,7 +302,7 @@ public:
void set(const ConfigOption *rhs) override
{
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionVector: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionVector: Assigning an incompatible type");
assert(dynamic_cast<const ConfigOptionVector<T>*>(rhs));
this->values = static_cast<const ConfigOptionVector<T>*>(rhs)->values;
}
@@ -304,12 +319,12 @@ public:
if (opt->type() == this->type()) {
auto other = static_cast<const ConfigOptionVector<T>*>(opt);
if (other->values.empty())
- throw Slic3r::RuntimeError("ConfigOptionVector::set(): Assigning from an empty vector");
+ throw ConfigurationError("ConfigOptionVector::set(): Assigning from an empty vector");
this->values.emplace_back(other->values.front());
} else if (opt->type() == this->scalar_type())
this->values.emplace_back(static_cast<const ConfigOptionSingle<T>*>(opt)->value);
else
- throw Slic3r::RuntimeError("ConfigOptionVector::set():: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionVector::set():: Assigning an incompatible type");
}
}
@@ -328,12 +343,12 @@ public:
// Assign the first value of the rhs vector.
auto other = static_cast<const ConfigOptionVector<T>*>(rhs);
if (other->values.empty())
- throw Slic3r::RuntimeError("ConfigOptionVector::set_at(): Assigning from an empty vector");
+ throw ConfigurationError("ConfigOptionVector::set_at(): Assigning from an empty vector");
this->values[i] = other->get_at(j);
} else if (rhs->type() == this->scalar_type())
this->values[i] = static_cast<const ConfigOptionSingle<T>*>(rhs)->value;
else
- throw Slic3r::RuntimeError("ConfigOptionVector::set_at(): Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionVector::set_at(): Assigning an incompatible type");
}
const T& get_at(size_t i) const
@@ -358,9 +373,9 @@ public:
else if (n > this->values.size()) {
if (this->values.empty()) {
if (opt_default == nullptr)
- throw Slic3r::RuntimeError("ConfigOptionVector::resize(): No default value provided.");
+ throw ConfigurationError("ConfigOptionVector::resize(): No default value provided.");
if (opt_default->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionVector::resize(): Extending with an incompatible type.");
+ throw ConfigurationError("ConfigOptionVector::resize(): Extending with an incompatible type.");
this->values.resize(n, static_cast<const ConfigOptionVector<T>*>(opt_default)->values.front());
} else {
// Resize by duplicating the last value.
@@ -377,7 +392,7 @@ public:
bool operator==(const ConfigOption &rhs) const override
{
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionVector: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionVector: Comparing incompatible types");
assert(dynamic_cast<const ConfigOptionVector<T>*>(&rhs));
return this->values == static_cast<const ConfigOptionVector<T>*>(&rhs)->values;
}
@@ -389,9 +404,9 @@ public:
// An option overrides another option if it is not nil and not equal.
bool overriden_by(const ConfigOption *rhs) const override {
if (this->nullable())
- throw Slic3r::RuntimeError("Cannot override a nullable ConfigOption.");
+ throw ConfigurationError("Cannot override a nullable ConfigOption.");
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionVector.overriden_by() applied to different types.");
+ throw ConfigurationError("ConfigOptionVector.overriden_by() applied to different types.");
auto rhs_vec = static_cast<const ConfigOptionVector<T>*>(rhs);
if (! rhs->nullable())
// Overridding a non-nullable object with another non-nullable object.
@@ -409,9 +424,9 @@ public:
// Apply an override option, possibly a nullable one.
bool apply_override(const ConfigOption *rhs) override {
if (this->nullable())
- throw Slic3r::RuntimeError("Cannot override a nullable ConfigOption.");
+ throw ConfigurationError("Cannot override a nullable ConfigOption.");
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionVector.apply_override() applied to different types.");
+ throw ConfigurationError("ConfigOptionVector.apply_override() applied to different types.");
auto rhs_vec = static_cast<const ConfigOptionVector<T>*>(rhs);
if (! rhs->nullable()) {
// Overridding a non-nullable object with another non-nullable object.
@@ -500,7 +515,7 @@ public:
bool operator==(const ConfigOptionFloatsTempl &rhs) const { return vectors_equal(this->values, rhs.values); }
bool operator==(const ConfigOption &rhs) const override {
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionFloatsTempl: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionFloatsTempl: Comparing incompatible types");
assert(dynamic_cast<const ConfigOptionVector<double>*>(&rhs));
return vectors_equal(this->values, static_cast<const ConfigOptionVector<double>*>(&rhs)->values);
}
@@ -547,7 +562,7 @@ public:
if (NULLABLE)
this->values.push_back(nil_value());
else
- throw Slic3r::RuntimeError("Deserializing nil into a non-nullable object");
+ throw ConfigurationError("Deserializing nil into a non-nullable object");
} else {
std::istringstream iss(item_str);
double value;
@@ -572,9 +587,9 @@ protected:
if (NULLABLE)
ss << "nil";
else
- throw Slic3r::RuntimeError("Serializing NaN");
+ throw ConfigurationError("Serializing NaN");
} else
- throw Slic3r::RuntimeError("Serializing invalid number");
+ throw ConfigurationError("Serializing invalid number");
}
static bool vectors_equal(const std::vector<double> &v1, const std::vector<double> &v2) {
if (NULLABLE) {
@@ -693,7 +708,7 @@ public:
if (NULLABLE)
this->values.push_back(nil_value());
else
- throw Slic3r::RuntimeError("Deserializing nil into a non-nullable object");
+ throw ConfigurationError("Deserializing nil into a non-nullable object");
} else {
std::istringstream iss(item_str);
int value;
@@ -710,7 +725,7 @@ private:
if (NULLABLE)
ss << "nil";
else
- throw Slic3r::RuntimeError("Serializing NaN");
+ throw ConfigurationError("Serializing NaN");
} else
ss << v;
}
@@ -895,7 +910,7 @@ public:
bool operator==(const ConfigOption &rhs) const override
{
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionFloatOrPercent: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionFloatOrPercent: Comparing incompatible types");
assert(dynamic_cast<const ConfigOptionFloatOrPercent*>(&rhs));
return *this == *static_cast<const ConfigOptionFloatOrPercent*>(&rhs);
}
@@ -906,7 +921,7 @@ public:
void set(const ConfigOption *rhs) override {
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionFloatOrPercent: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionFloatOrPercent: Assigning an incompatible type");
assert(dynamic_cast<const ConfigOptionFloatOrPercent*>(rhs));
*this = *static_cast<const ConfigOptionFloatOrPercent*>(rhs);
}
@@ -971,7 +986,7 @@ public:
bool operator==(const ConfigOptionFloatsOrPercentsTempl &rhs) const { return vectors_equal(this->values, rhs.values); }
bool operator==(const ConfigOption &rhs) const override {
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionFloatsOrPercentsTempl: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionFloatsOrPercentsTempl: Comparing incompatible types");
assert(dynamic_cast<const ConfigOptionVector<FloatOrPercent>*>(&rhs));
return vectors_equal(this->values, static_cast<const ConfigOptionVector<FloatOrPercent>*>(&rhs)->values);
}
@@ -1018,7 +1033,7 @@ public:
if (NULLABLE)
this->values.push_back(nil_value());
else
- throw Slic3r::RuntimeError("Deserializing nil into a non-nullable object");
+ throw ConfigurationError("Deserializing nil into a non-nullable object");
} else {
bool percent = item_str.find_first_of("%") != std::string::npos;
std::istringstream iss(item_str);
@@ -1046,9 +1061,9 @@ protected:
if (NULLABLE)
ss << "nil";
else
- throw Slic3r::RuntimeError("Serializing NaN");
+ throw ConfigurationError("Serializing NaN");
} else
- throw Slic3r::RuntimeError("Serializing invalid number");
+ throw ConfigurationError("Serializing invalid number");
}
static bool vectors_equal(const std::vector<FloatOrPercent> &v1, const std::vector<FloatOrPercent> &v2) {
if (NULLABLE) {
@@ -1318,7 +1333,7 @@ public:
if (NULLABLE)
this->values.push_back(nil_value());
else
- throw Slic3r::RuntimeError("Deserializing nil into a non-nullable object");
+ throw ConfigurationError("Deserializing nil into a non-nullable object");
} else
this->values.push_back(item_str.compare("1") == 0);
}
@@ -1331,7 +1346,7 @@ protected:
if (NULLABLE)
ss << "nil";
else
- throw Slic3r::RuntimeError("Serializing NaN");
+ throw ConfigurationError("Serializing NaN");
} else
ss << (v ? "1" : "0");
}
@@ -1367,14 +1382,14 @@ public:
bool operator==(const ConfigOption &rhs) const override
{
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionEnum<T>: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionEnum<T>: Comparing incompatible types");
// rhs could be of the following type: ConfigOptionEnumGeneric or ConfigOptionEnum<T>
return this->value == (T)rhs.getInt();
}
void set(const ConfigOption *rhs) override {
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionEnum<T>: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionEnum<T>: Assigning an incompatible type");
// rhs could be of the following type: ConfigOptionEnumGeneric or ConfigOptionEnum<T>
this->value = (T)rhs->getInt();
}
@@ -1451,14 +1466,14 @@ public:
bool operator==(const ConfigOption &rhs) const override
{
if (rhs.type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionEnumGeneric: Comparing incompatible types");
+ throw ConfigurationError("ConfigOptionEnumGeneric: Comparing incompatible types");
// rhs could be of the following type: ConfigOptionEnumGeneric or ConfigOptionEnum<T>
return this->value == rhs.getInt();
}
void set(const ConfigOption *rhs) override {
if (rhs->type() != this->type())
- throw Slic3r::RuntimeError("ConfigOptionEnumGeneric: Assigning an incompatible type");
+ throw ConfigurationError("ConfigOptionEnumGeneric: Assigning an incompatible type");
// rhs could be of the following type: ConfigOptionEnumGeneric or ConfigOptionEnum<T>
this->value = rhs->getInt();
}
@@ -1513,7 +1528,7 @@ public:
case coInts: { auto opt = new ConfigOptionIntsNullable(); archive(*opt); return opt; }
case coPercents: { auto opt = new ConfigOptionPercentsNullable();archive(*opt); return opt; }
case coBools: { auto opt = new ConfigOptionBoolsNullable(); archive(*opt); return opt; }
- default: throw Slic3r::RuntimeError(std::string("ConfigOptionDef::load_option_from_archive(): Unknown nullable option type for option ") + this->opt_key);
+ default: throw ConfigurationError(std::string("ConfigOptionDef::load_option_from_archive(): Unknown nullable option type for option ") + this->opt_key);
}
} else {
switch (this->type) {
@@ -1532,7 +1547,7 @@ public:
case coBool: { auto opt = new ConfigOptionBool(); archive(*opt); return opt; }
case coBools: { auto opt = new ConfigOptionBools(); archive(*opt); return opt; }
case coEnum: { auto opt = new ConfigOptionEnumGeneric(this->enum_keys_map); archive(*opt); return opt; }
- default: throw Slic3r::RuntimeError(std::string("ConfigOptionDef::load_option_from_archive(): Unknown option type for option ") + this->opt_key);
+ default: throw ConfigurationError(std::string("ConfigOptionDef::load_option_from_archive(): Unknown option type for option ") + this->opt_key);
}
}
}
@@ -1544,7 +1559,7 @@ public:
case coInts: archive(*static_cast<const ConfigOptionIntsNullable*>(opt)); break;
case coPercents: archive(*static_cast<const ConfigOptionPercentsNullable*>(opt));break;
case coBools: archive(*static_cast<const ConfigOptionBoolsNullable*>(opt)); break;
- default: throw Slic3r::RuntimeError(std::string("ConfigOptionDef::save_option_to_archive(): Unknown nullable option type for option ") + this->opt_key);
+ default: throw ConfigurationError(std::string("ConfigOptionDef::save_option_to_archive(): Unknown nullable option type for option ") + this->opt_key);
}
} else {
switch (this->type) {
@@ -1563,7 +1578,7 @@ public:
case coBool: archive(*static_cast<const ConfigOptionBool*>(opt)); break;
case coBools: archive(*static_cast<const ConfigOptionBools*>(opt)); break;
case coEnum: archive(*static_cast<const ConfigOptionEnumGeneric*>(opt)); break;
- default: throw Slic3r::RuntimeError(std::string("ConfigOptionDef::save_option_to_archive(): Unknown option type for option ") + this->opt_key);
+ default: throw ConfigurationError(std::string("ConfigOptionDef::save_option_to_archive(): Unknown option type for option ") + this->opt_key);
}
}
// Make the compiler happy, shut up the warnings.
@@ -1686,7 +1701,7 @@ public:
return out;
}
- /// Iterate through all of the CLI options and write them to a stream.
+ // Iterate through all of the CLI options and write them to a stream.
std::ostream& print_cli_help(
std::ostream& out, bool show_defaults,
std::function<bool(const ConfigOptionDef &)> filter = [](const ConfigOptionDef &){ return true; }) const;
@@ -2031,9 +2046,9 @@ private:
template<class Archive> void serialize(Archive &ar) { ar(options); }
};
-/// Configuration store with a static definition of configuration values.
-/// In Slic3r, the static configuration stores are during the slicing / g-code generation for efficiency reasons,
-/// because the configuration values could be accessed directly.
+// Configuration store with a static definition of configuration values.
+// In Slic3r, the static configuration stores are during the slicing / g-code generation for efficiency reasons,
+// because the configuration values could be accessed directly.
class StaticConfig : public virtual ConfigBase
{
public: