Commit da459ff0 authored by Alexander Lapshin's avatar Alexander Lapshin

added try catch blocks to json loading functions

parent 831edfb3
This diff is collapsed.
......@@ -6,6 +6,7 @@
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<TimeJD>& vec, const bool exist = true, bool* loaded = nullptr)
{
try {
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
......@@ -14,7 +15,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value;
if (!a.IsArray()) {
throw Exp() << "error: element is not an array";
throw Exp() << "element is not an array";
}
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
......@@ -22,15 +23,20 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
vec.emplace_back(StrToDate(it->GetString()));
}
else {
throw Exp() << "error: unsupported type";
throw Exp() << "unsupported type";
}
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
static void loadjson(const jsonval& parent, const std::string& key, TimeJD& value, bool exist = true, bool* loaded = nullptr)
{
try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
......@@ -54,16 +60,21 @@ static void loadjson(const jsonval& parent, const std::string& key, TimeJD& valu
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
}
static void loadjson(const jsonval& parent, const std::string& key, TimeGD& value, bool exist = true)
{
try {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found";
throw Exp() << "element not found";
}
const jsonval& a = itr->value;
......@@ -72,7 +83,11 @@ static void loadjson(const jsonval& parent, const std::string& key, TimeGD& valu
StrToDate(a.GetString(), value);
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
}
......@@ -93,6 +108,7 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co
static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{
try {
jsonval::ConstMemberIterator itr;
bool* loaded = nullptr;
......@@ -106,7 +122,11 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
value = StrToDate(a.GetString());
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
throw Exp() << "unexpected type";
}
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
......@@ -114,6 +134,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{
try {
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
......@@ -122,12 +143,17 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
value = StrToDate(itr->second);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, TimeJD& value, const bool exist = true, bool* loaded = nullptr)
{
try {
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
......@@ -144,6 +170,10 @@ static bool loadjson(const std::map<std::string, std::string>& parent, const std
}
value = StrToDate(str);
}
catch (std::exception& e) {
throw Exp() << "failed to load key '" << key << "'" << " - " << trim(e.what()) << "\n";
}
return true;
}
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