Open adrianbroher opened 4 years ago
There are some usability issues with error handling that you've run into.
YAML::InvalidNode
should probably have the node.Mark()
properly set; I don't remember what YAML::KeyNotFound
was.what()
should have more information, right? What kind of more information are you looking for that you wouldn't need a TypedBadConversion<T>
for instead?
- The
YAML::InvalidNode
should probably have thenode.Mark()
properly set; I don't remember whatYAML::KeyNotFound
was.
The KeyNotFound
and TypedKeyNotFound
are defined in yaml-cpp/exceptions.h
but are not instantiated anywhere directly or via MakeTypedKeyNotFound
function.
- The
what()
should have more information, right? What kind of more information are you looking for that you wouldn't need aTypedBadConversion<T>
for instead?
Ideally I would expect a what()
message similar to Expected a TypeName
when the decode<TypeName>
raises an exception or returns false, which in turn emits a TypedBadConversion<TypeName>
. I also believe those exception should be written for immediate human consumption (log files, error messages) without insight into the emitting source code, so convert
struct maybe should have a dedicated type_name
string literal which contains the pretty name of the converted type.
Given the previous code:
template <>
struct convert<PlanetData> {
static bool decode(const Node& node, PlanetData& rhs);
static char const* type_name = "Planet model";
};
The emitted what message for convert<PlanetData>decode
returning false should look like:
Expected a Planet model
When the decode function emits an exception (here for a missing key called surface
) I would suggest the following message:
Failed to decode Planet model: missing key "surface"
.
Hello, I'm currently testing out this library for usage in another project and I'm wondering if the current implementation matches the intended usage of
YAML::convert
. I think my current usage is more verbose than it needs to be (intentionally leaving out some code here):planets.yml
Datastructure
Loading code
Custom type converter
For now I have several problems with this code and I'm not sure if those stem from improper use or are bugs:
node["missing_key"].as<some_type>()
would automatically raise aYAML::KeyNotFound
exception with a validnode.Mark()
value. Instead aYAML::InvalidNode
with "missing_key" as key set, but a null node.Mark() value, making it impossible to trace the wrong line. It also seems likeYAML::KeyNotFound
isn't used anywhere. Would it be better to introduce aYAML::Node::at(key)
method to add a boundary check similar tostd::map::at(key)
?YAML::Load
and initialnode["root_key"].as<structure>()
need a separate catch block for eachYAML::TypedBadConversion<T>
to get a more meaningful message instead of "bad conversion". This becomes very complex pretty fast depending on the data structure built. Should I ignore the return value of convertYAML::RepresentationException
instead whenever the conversion cannot occur? What about the builtin conversions for std containers?YAML::KeyNotFound
constructor discards the key when it is only a plain string literal, making the error message unusable.YAML::Exception::what
method doesn't contain the row or column where the exception occurred.