Commit 9cb98f9b authored by Dominik Charousset's avatar Dominik Charousset

Do not propagate exceptions in `from_string`

Catch exception thrown in `from_string_impl` and do not propagate them. The
calling function, i.e., `from_string`, return `none` in case of a parser error.
Hence, exceptions are not expected (or allowed).
parent 2cfb52d8
...@@ -232,8 +232,8 @@ class string_deserializer : public deserializer, public dummy_backend { ...@@ -232,8 +232,8 @@ class string_deserializer : public deserializer, public dummy_backend {
while (*m_pos == ' ' || *m_pos == ',') ++m_pos; while (*m_pos == ' ' || *m_pos == ',') ++m_pos;
} }
void throw_malformed[[noreturn]](const string& error_msg) { void throw_malformed(const string& error_msg) {
throw std::logic_error("malformed string: " + error_msg); throw std::runtime_error("malformed string: " + error_msg);
} }
void consume(char c) { void consume(char c) {
...@@ -377,12 +377,12 @@ class string_deserializer : public deserializer, public dummy_backend { ...@@ -377,12 +377,12 @@ class string_deserializer : public deserializer, public dummy_backend {
what = static_cast<atom_value>(detail::atom_val(str.c_str(), 0xF)); what = static_cast<atom_value>(detail::atom_val(str.c_str(), 0xF));
} }
void operator()(u16string&) { void operator()(u16string&) {
throw std::logic_error( throw std::runtime_error(
"u16string currently not supported " "u16string currently not supported "
"by string_deserializer"); "by string_deserializer");
} }
void operator()(u32string&) { void operator()(u32string&) {
throw std::logic_error( throw std::runtime_error(
"u32string currently not supported " "u32string currently not supported "
"by string_deserializer"); "by string_deserializer");
} }
...@@ -428,7 +428,7 @@ class string_deserializer : public deserializer, public dummy_backend { ...@@ -428,7 +428,7 @@ class string_deserializer : public deserializer, public dummy_backend {
substr_end = find_if(m_pos, m_str.end(), find_if_cond); substr_end = find_if(m_pos, m_str.end(), find_if_cond);
} }
if (substr_end == m_str.end()) { if (substr_end == m_str.end()) {
throw std::logic_error("malformed string (unterminated value)"); throw std::runtime_error("malformed string (unterminated value)");
} }
string substr(m_pos, substr_end); string substr(m_pos, substr_end);
m_pos += static_cast<difference_type>(substr.size()); m_pos += static_cast<difference_type>(substr.size());
...@@ -442,7 +442,7 @@ class string_deserializer : public deserializer, public dummy_backend { ...@@ -442,7 +442,7 @@ class string_deserializer : public deserializer, public dummy_backend {
error_msg += "' found '"; error_msg += "' found '";
error_msg += *m_pos; error_msg += *m_pos;
error_msg += "'"; error_msg += "'";
throw std::logic_error(error_msg); throw std::runtime_error(error_msg);
} }
++m_pos; ++m_pos;
// replace '\"' by '"' // replace '\"' by '"'
...@@ -539,11 +539,15 @@ ostream& operator<<(ostream& out, skip_message_t) { ...@@ -539,11 +539,15 @@ ostream& operator<<(ostream& out, skip_message_t) {
uniform_value from_string_impl(const string& what) { uniform_value from_string_impl(const string& what) {
string_deserializer strd(what); string_deserializer strd(what);
auto utype = strd.begin_object(); try {
if (utype) { auto utype = strd.begin_object();
return utype->deserialize(&strd); if (utype) {
return utype->deserialize(&strd);
}
strd.end_object();
} catch (std::runtime_error&) {
// ignored, i.e., return an invalid value
} }
strd.end_object();
return {}; return {};
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment