Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
expected support error message (#970)
Browse files Browse the repository at this point in the history
Signed-off-by: Yusheng.Ma <[email protected]>
  • Loading branch information
Presburger authored Jun 29, 2023
1 parent 8362b92 commit e305090
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
58 changes: 56 additions & 2 deletions include/knowhere/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ class EntryAccess {
class Config {
public:
static Status
FormatAndCheck(const Config& cfg, Json& json);
FormatAndCheck(const Config& cfg, Json& json, std::string* const err_msg = nullptr);

static Status
Load(Config& cfg, const Json& json, PARAM_TYPE type) {
Load(Config& cfg, const Json& json, PARAM_TYPE type, std::string* const err_msg = nullptr) {
for (const auto& it : cfg.__DICT__) {
const auto& var = it.second;

Expand All @@ -278,6 +278,9 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}
return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -286,12 +289,18 @@ class Config {
}
if (!json[it.first].is_number_integer()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be integer.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be integer";
}
return Status::type_conflict_in_json;
}
if (ptr->range.has_value()) {
if (json[it.first].get<long>() > std::numeric_limits<CFG_INT::value_type>::max()) {
LOG_KNOWHERE_ERROR_ << "Arithmetic overflow: param [" << it.first << "] should be at most "
<< std::numeric_limits<CFG_INT::value_type>::max();
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be at most 2147483647";
}
return Status::arithmetic_overflow;
}
CFG_INT::value_type v = json[it.first];
Expand All @@ -300,6 +309,11 @@ class Config {
} else {
LOG_KNOWHERE_ERROR_ << "Out of range in json: param [" << it.first << "] should be in ["
<< ptr->range.value().first << ", " << ptr->range.value().second << "].";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " out of range " + "[ " +
std::to_string(ptr->range.value().first) + "," +
std::to_string(ptr->range.value().second) + " ]";
}
return Status::out_of_range_in_json;
}
} else {
Expand All @@ -316,6 +330,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -324,12 +342,20 @@ class Config {
}
if (!json[it.first].is_number()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a number.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a number";
}

return Status::type_conflict_in_json;
}
if (ptr->range.has_value()) {
if (json[it.first].get<double>() > std::numeric_limits<CFG_FLOAT::value_type>::max()) {
LOG_KNOWHERE_ERROR_ << "Arithmetic overflow: param [" << it.first << "] should be at most "
<< std::numeric_limits<CFG_FLOAT::value_type>::max();
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be at most 3.402823e+38";
}

return Status::arithmetic_overflow;
}
CFG_FLOAT::value_type v = json[it.first];
Expand All @@ -338,6 +364,12 @@ class Config {
} else {
LOG_KNOWHERE_ERROR_ << "Out of range in json: param [" << it.first << "] should be in ["
<< ptr->range.value().first << ", " << ptr->range.value().second << "].";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " out of range " + "[ " +
std::to_string(ptr->range.value().first) + "," +
std::to_string(ptr->range.value().second) + " ]";
}

return Status::out_of_range_in_json;
}
} else {
Expand All @@ -354,6 +386,9 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}
return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -362,6 +397,9 @@ class Config {
}
if (!json[it.first].is_string()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a string.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a string";
}
return Status::type_conflict_in_json;
}
*ptr->val = json[it.first];
Expand All @@ -376,6 +414,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -384,6 +426,10 @@ class Config {
}
if (!json[it.first].is_array()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be an array.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be an array";
}

return Status::type_conflict_in_json;
}
*ptr->val = CFG_LIST();
Expand All @@ -401,6 +447,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -409,6 +459,10 @@ class Config {
}
if (!json[it.first].is_boolean()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a boolean.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a boolean";
}

return Status::type_conflict_in_json;
}
*ptr->val = json[it.first];
Expand Down
11 changes: 11 additions & 0 deletions include/knowhere/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ class expected {
return val.value();
}

const std::string&
what() const {
return msg;
}

void
operator<<(const std::string& msg) {
this->msg += msg;
}

expected<T>&
operator=(const Status& err) {
assert(err != Status::success);
Expand All @@ -93,6 +103,7 @@ class expected {
private:
std::optional<T> val = std::nullopt;
std::optional<Status> err = std::nullopt;
std::string msg;
};

// Evaluates expr that returns a Status. Does nothing if the returned Status is
Expand Down
46 changes: 27 additions & 19 deletions src/common/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,32 @@ static const std::unordered_set<std::string> ext_legal_json_keys = {"metric_type
"for_tuning"};

Status
Config::FormatAndCheck(const Config& cfg, Json& json) {
for (auto& it : json.items()) {
bool status = true;
{
auto it_ = cfg.__DICT__.find(it.key());
if (it_ == cfg.__DICT__.end()) {
status = false;
Config::FormatAndCheck(const Config& cfg, Json& json, std::string* const err_msg) {
try {
for (auto& it : json.items()) {
bool status = true;
{
auto it_ = cfg.__DICT__.find(it.key());
if (it_ == cfg.__DICT__.end()) {
status = false;
}
}
}
{
auto it_ = ext_legal_json_keys.find(it.key());
if (it_ == ext_legal_json_keys.end()) {
status |= false;
} else {
status |= true;
{
auto it_ = ext_legal_json_keys.find(it.key());
if (it_ != ext_legal_json_keys.end()) {
status |= true;
}
}
if (!status) {
throw KnowhereException(std::string("invalid json key ") + it.key());
}
}
if (!status) {
LOG_KNOWHERE_ERROR_ << "invalid json key: " << it.key();
return Status::invalid_param_in_json;
} catch (std::exception& e) {
LOG_KNOWHERE_ERROR_ << e.what();
if (err_msg) {
*err_msg = e.what();
}
return Status::invalid_param_in_json;
}

try {
Expand All @@ -82,7 +87,7 @@ Config::FormatAndCheck(const Config& cfg, Json& json) {
auto value_str = json[it.first].get<std::string>();
CFG_INT::value_type v = std::stoi(value_str.c_str(), &sz);
if (sz < value_str.length()) {
throw KnowhereException("wrong data type in json");
throw KnowhereException(std::string("wrong data type in json ") + value_str);
}
json[it.first] = v;
}
Expand All @@ -102,7 +107,10 @@ Config::FormatAndCheck(const Config& cfg, Json& json) {
}
}
} catch (std::exception& e) {
LOG_KNOWHERE_ERROR_ << "Invalid value in json: " << e.what();
LOG_KNOWHERE_ERROR_ << e.what();
if (err_msg) {
*err_msg = e.what();
}
return Status::invalid_value_in_json;
}
return Status::success;
Expand Down

0 comments on commit e305090

Please sign in to comment.