Skip to content

Commit 865b26d

Browse files
committed
Improved error messages.
1 parent 34a1f41 commit 865b26d

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

querying.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,6 @@ std::vector<std::string> QueryImpl::Run(const std::uint8_t* proto_data,
961961
void QueryImpl::CompileQuery(const descriptor_db::DescDb& desc_db,
962962
const std::string& query,
963963
std::optional<uint64_t> limit) {
964-
// TODO: improve error messages
965-
966964
visitors_.clear();
967965
emitter_ = nullptr;
968966
type_resolver_ = nullptr;
@@ -1059,15 +1057,15 @@ const pb::Descriptor* QueryImpl::GetDesc(const descriptor_db::DescSet& desc_set,
10591057
std::string::size_type* query_start) {
10601058
std::string::size_type i = query.find(':', *query_start);
10611059
if (i == std::string::npos) {
1062-
throw BadQuery("no protobuf name given");
1060+
throw BadQuery("invalid protobuf query - expected: [<descriptor_set>:]<message_name>:<path>");
10631061
}
10641062
std::string desc_name(query.substr(*query_start, i - *query_start));
10651063
*query_start = i + 1;
10661064

10671065
const pb::Descriptor* desc = desc_set.pool->FindMessageTypeByName(desc_name);
10681066
if (desc == nullptr) {
10691067
throw BadQuery(
1070-
"unknown protobuf (did you remember to give the full package name?)");
1068+
"unknown protobuf (did you remember to include the package name?)");
10711069
}
10721070
return desc;
10731071
}
@@ -1077,11 +1075,11 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
10771075
const pb::EnumDescriptor** ed,
10781076
const std::string& part) {
10791077
if (*desc == nullptr) {
1080-
throw BadQuery("query does not refer to a known field");
1078+
throw BadQuery(std::string("query does not refer to a known field: ") + part);
10811079
}
10821080

10831081
if (part.empty()) {
1084-
throw BadQuery("empty query part");
1082+
throw BadQuery("unexpected empty query part");
10851083
}
10861084

10871085
std::string::size_type bracket = part.find('[');
@@ -1096,7 +1094,7 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
10961094
char* end;
10971095
long l = std::strtol(part.c_str(), &end, 10);
10981096
if (end != &part[field_selector_end]) {
1099-
throw BadQuery("malformed field number in query");
1097+
throw BadQuery(std::string("invalid field number in query: ") + part);
11001098
}
11011099
fd = (*desc)->FindFieldByNumber(static_cast<int>(l));
11021100
} else {
@@ -1148,7 +1146,7 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
11481146
}
11491147

11501148
if (keys_selector && !fd->is_map()) {
1151-
throw BadQuery("|keys can only be used for maps");
1149+
throw BadQuery("'|keys' can only be used on maps");
11521150
}
11531151

11541152
if (fd->is_map()) {
@@ -1211,10 +1209,10 @@ void QueryImpl::CompileQueryPart(const pb::Descriptor** desc,
12111209
size_t end;
12121210
long n = std::stol(rep_selector.c_str(), &end, 10);
12131211
if (end != rep_selector.size()) {
1214-
throw BadQuery("expected numeric indexer");
1212+
throw BadQuery(std::string("expected numeric indexer at: ") + rep_selector);
12151213
}
12161214
if (n < 0 || n > static_cast<long>(std::numeric_limits<int>::max())) {
1217-
throw BadQuery("index out of bounds");
1215+
throw BadQuery(std::string("index out of bounds: " + rep_selector));
12181216
}
12191217

12201218
field_selector->SetWantedIndex(static_cast<int>(n));

0 commit comments

Comments
 (0)