1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-16 20:19:13 +01: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()); auto val = root.get(key, Json::Value());
if (val.isInt()) if (val.isInt())
return val.asInt(); return val.asInt();
throw "Json Type error"; throw std::runtime_error("Json Type error");
} }
template <> template <>
@ -17,7 +17,7 @@ string getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value()); auto val = root.get(key, Json::Value());
if (val.isString()) if (val.isString())
return val.asString(); return val.asString();
throw "Json Type error"; throw std::runtime_error("Json Type error");
} }
template <> template <>
@ -26,7 +26,7 @@ size_t getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value()); auto val = root.get(key, Json::Value());
if (val.isInt()) if (val.isInt())
return val.asUInt64(); return val.asUInt64();
throw "Json Type error"; throw std::runtime_error("Json Type error");
} }
template <> template <>
@ -35,7 +35,7 @@ bool getJson(const Json::Value &root, const string &key)
auto val = root.get(key, Json::Value()); auto val = root.get(key, Json::Value());
if (val.isBool()) if (val.isBool())
return val.asBool(); return val.asBool();
throw "Json Type error"; throw std::runtime_error("Json Type error");
} }
template <> template <>
@ -90,14 +90,14 @@ unsigned char hex2uchar(const char input)
} else if(input >= 'a' && input <= 'f') { } else if(input >= 'a' && input <= 'f') {
return input - 'a' + 10; return input - 'a' + 10;
} }
throw("Malformed hexadecimal input"); throw std::runtime_error("Malformed hexadecimal input");
} }
uint64_t toUint64(const string &s) uint64_t toUint64(const string &s)
{ {
if (s.length() != (64/4)) if (s.length() != (64/4))
{ {
throw("Hex string has invalid length"); throw std::runtime_error("Hex string has invalid length");
} }
uint64_t retval = 0; uint64_t retval = 0;

View File

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