Update parser error messages, fix parser tests

This commit is contained in:
ado
2024-03-13 19:39:07 +01:00
parent b9f4afdd5f
commit 809939d0e2
3 changed files with 47 additions and 43 deletions

View File

@@ -52,7 +52,7 @@ public:
parser(const char* const csv_data_buffer, size_t csv_data_size,
const std::string& delim = ss::default_delimiter)
: file_name_{"buffer line"},
: file_name_{"CSV data buffer"},
reader_{csv_data_buffer, csv_data_size, delim} {
if (csv_data_buffer) {
read_line();
@@ -219,7 +219,7 @@ public:
auto fields = std::vector<std::string>{fields_args...};
if (fields.empty()) {
handle_error_empty_mapping();
handle_error_invalid_use_fields_argument();
return;
}
@@ -537,7 +537,7 @@ private:
try {
splitter.split(header.data(), reader_.delim_);
} catch (const ss::exception& e) {
decorate_rethrow_no_line(e);
decorate_rethrow_invalid_header_split(e);
}
} else {
splitter.split(header.data(), reader_.delim_);
@@ -598,7 +598,7 @@ private:
}
void handle_error_failed_check() {
constexpr static auto error_msg = " failed check";
constexpr static auto error_msg = ": failed check";
if constexpr (string_error) {
error_.clear();
@@ -611,7 +611,7 @@ private:
}
void handle_error_null_buffer() {
constexpr static auto error_msg = " received null data buffer";
constexpr static auto error_msg = ": received null data buffer";
if constexpr (string_error) {
error_.clear();
@@ -624,7 +624,7 @@ private:
}
void handle_error_file_not_open() {
constexpr static auto error_msg = " could not be opened";
constexpr static auto error_msg = ": could not be opened";
if constexpr (string_error) {
error_.clear();
@@ -637,7 +637,7 @@ private:
}
void handle_error_eof_reached() {
constexpr static auto error_msg = " read on end of file";
constexpr static auto error_msg = ": read on end of file";
if constexpr (string_error) {
error_.clear();
@@ -689,8 +689,9 @@ private:
}
}
void handle_error_empty_mapping() {
constexpr static auto error_msg = "received empty mapping";
void handle_error_invalid_use_fields_argument() {
constexpr static auto error_msg =
"received invalid argument for 'use_fields'";
if constexpr (string_error) {
error_.clear();
@@ -703,20 +704,20 @@ private:
}
void handle_error_invalid_header_field() {
constexpr static auto error_msg = " header contains empty field";
constexpr static auto error_msg = ": header contains empty field";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{error_msg};
throw ss::exception{file_name_ + error_msg};
} else {
error_ = true;
}
}
void handle_error_duplicate_header_field(const std::string& field) {
constexpr static auto error_msg = " header contains duplicate: ";
constexpr static auto error_msg = ": header contains duplicate: ";
if constexpr (string_error) {
error_.clear();
@@ -729,7 +730,7 @@ private:
}
void handle_error_invalid_header_split(const header_splitter& splitter) {
constexpr static auto error_msg = " failed header split: ";
constexpr static auto error_msg = ": failed header parsing: ";
if constexpr (string_error) {
error_.clear();
@@ -741,6 +742,14 @@ private:
}
}
void decorate_rethrow_invalid_header_split(const ss::exception& e) const {
static_assert(throw_on_error,
"throw_on_error needs to be enabled to use this method");
throw ss::exception{std::string{file_name_}
.append(": failed header parsing: ")
.append(e.what())};
}
void decorate_rethrow(const ss::exception& e) const {
static_assert(throw_on_error,
"throw_on_error needs to be enabled to use this method");
@@ -751,14 +760,6 @@ private:
.append(e.what())};
}
void decorate_rethrow_no_line(const ss::exception& e) const {
static_assert(throw_on_error,
"throw_on_error needs to be enabled to use this method");
throw ss::exception{std::string{file_name_}
.append(": ")
.append(e.what())};
}
////////////////
// line reading
////////////////