Improve CD file parsing error messages.
#include <catboost/libs/helpers/exception.h>
#include <catboost/libs/logging/logging.h>
+#include <util/generic/cast.h>
#include <util/generic/set.h>
#include <util/stream/file.h>
#include <util/stream/input.h>
@@ -17,8 +18,8 @@ using namespace NCB;
namespace {
- inline void CheckAllFeaturesPresent(const TVector<TColumn>& columns, const TSet<int>& parsedColumns) {
- for (int i = 0; i < columns.ysize(); ++i) {
+ inline void CheckAllFeaturesPresent(const TVector<TColumn>& columns, const TSet<size_t>& parsedColumns) {
+ for (size_t i = 0; i < columns.size(); ++i) {
CB_ENSURE(parsedColumns.has(i), "column not present in cd file: " << i);
}
}
@@ -26,14 +27,15 @@ namespace {
template <class TReadLineFunc>
TVector<TColumn> ReadCDImpl(TReadLineFunc readLineFunc, const TCdParserDefaults& defaults) {
- int columnsCount = defaults.UseDefaultType ? defaults.ColumnCount : 0;
+ size_t columnsCount = defaults.UseDefaultType ? SafeIntegerCast<size_t>(defaults.ColumnCount) : 0;
TVector<TColumn> columns(columnsCount, TColumn{defaults.DefaultColumnType, TString()});
- TSet<int> parsedColumns;
+ TSet<size_t> parsedColumns;
TString line;
for (size_t lineNumber = 0; readLineFunc(&line); lineNumber++) {
TVector<TString> tokens;
+ try {
try {
Split(line, "\t", tokens);
} catch (const yexception& e) {
@@ -44,10 +46,14 @@ namespace {
continue;
}
CB_ENSURE(tokens.ysize() == 2 || tokens.ysize() == 3,
- "Incorrect CD file. Each line should have two or three columns. " <<
- "Invalid line number #" << lineNumber << ": " << line);
- int index = FromString<int>(tokens[0]);
- CB_ENSURE(index >= 0, "Invalid column index " << index);
+ "Each line should have two or three columns. This line has " << tokens.size()
+ );
+
+ size_t index = 0;
+ CB_ENSURE(
+ TryFromString(tokens[0], index),
+ "Invalid column index: \"" << tokens[0] << "\""
+ );
if (defaults.UseDefaultType) {
CB_ENSURE(
index < columnsCount,
@@ -55,7 +61,7 @@ namespace {
}
CB_ENSURE(!parsedColumns.has(index), "column specified twice in cd file: " << index);
parsedColumns.insert(index);
- columns.resize(Max(columns.ysize(), index + 1));
+ columns.resize(Max(columns.size(), index + 1));
TStringBuf type = tokens[1];
if (type == "QueryId") {
@@ -68,6 +74,10 @@ namespace {
if (tokens.ysize() == 3) {
columns[index].Id = tokens[2];
}
+ } catch (const TCatboostException& e) {
+ throw TCatboostException() << "Incorrect CD file. Invalid line number #" << lineNumber
+ << ": " << e.what();
+ }
}
if (!defaults.UseDefaultType) {
CheckAllFeaturesPresent(columns, parsedColumns);
