1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-06-10 10:22:34 +02:00

Improved exception handling

This commit is contained in:
Ralf Ramsauer 2015-09-25 00:13:30 +02:00
parent 80d8610559
commit 4c05636ff0
2 changed files with 8 additions and 7 deletions

View File

@ -8,7 +8,7 @@ int getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value());
if (val.isInt())
return val.asInt();
throw "Json Type error";
throw std::runtime_error("Json Type error");
}
template <>
@ -17,7 +17,7 @@ string getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value());
if (val.isString())
return val.asString();
throw "Json Type error";
throw std::runtime_error("Json Type error");
}
template <>
@ -26,7 +26,7 @@ size_t getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value());
if (val.isInt())
return val.asUInt64();
throw "Json Type error";
throw std::runtime_error("Json Type error");
}
template <>
@ -35,7 +35,7 @@ bool getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value());
if (val.isBool())
return val.asBool();
throw "Json Type error";
throw std::runtime_error("Json Type error");
}
template <>
@ -90,14 +90,14 @@ unsigned char hex2uchar(const char input)
} else if(input >= 'a' && input <= 'f') {
return input - 'a' + 10;
}
throw("Malformed hexadecimal input");
throw std::runtime_error("Malformed hexadecimal input");
}
uint64_t toUint64(const string &s)
{
if (s.length() != (64/4))
{
throw("Hex string has invalid length");
throw std::runtime_error("Hex string has invalid length");
}
uint64_t retval = 0;

View File

@ -2,6 +2,7 @@
#define UTIL_H
#include <algorithm>
#include <exception>
#include <json/json.h>
template <typename T>
@ -13,7 +14,7 @@ T getJsonOrFail(const Json::Value &root, const std::string &key)
const auto members = root.getMemberNames();
if (std::find(members.begin(), members.end(), key) == members.end())
{
throw "Json key not existing";
throw std::runtime_error("Json key not existing");
}
return getJson<T>(root, key);