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

43
ssp.hpp
View File

@ -2304,7 +2304,7 @@ public:
parser(const char* const csv_data_buffer, size_t csv_data_size, parser(const char* const csv_data_buffer, size_t csv_data_size,
const std::string& delim = ss::default_delimiter) const std::string& delim = ss::default_delimiter)
: file_name_{"buffer line"}, : file_name_{"CSV data buffer"},
reader_{csv_data_buffer, csv_data_size, delim} { reader_{csv_data_buffer, csv_data_size, delim} {
if (csv_data_buffer) { if (csv_data_buffer) {
read_line(); read_line();
@ -2471,7 +2471,7 @@ public:
auto fields = std::vector<std::string>{fields_args...}; auto fields = std::vector<std::string>{fields_args...};
if (fields.empty()) { if (fields.empty()) {
handle_error_empty_mapping(); handle_error_invalid_use_fields_argument();
return; return;
} }
@ -2789,7 +2789,7 @@ private:
try { try {
splitter.split(header.data(), reader_.delim_); splitter.split(header.data(), reader_.delim_);
} catch (const ss::exception& e) { } catch (const ss::exception& e) {
decorate_rethrow_no_line(e); decorate_rethrow_invalid_header_split(e);
} }
} else { } else {
splitter.split(header.data(), reader_.delim_); splitter.split(header.data(), reader_.delim_);
@ -2850,7 +2850,7 @@ private:
} }
void handle_error_failed_check() { void handle_error_failed_check() {
constexpr static auto error_msg = " failed check"; constexpr static auto error_msg = ": failed check";
if constexpr (string_error) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2863,7 +2863,7 @@ private:
} }
void handle_error_null_buffer() { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2876,7 +2876,7 @@ private:
} }
void handle_error_file_not_open() { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2889,7 +2889,7 @@ private:
} }
void handle_error_eof_reached() { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2941,8 +2941,9 @@ private:
} }
} }
void handle_error_empty_mapping() { void handle_error_invalid_use_fields_argument() {
constexpr static auto error_msg = "received empty mapping"; constexpr static auto error_msg =
"received invalid argument for 'use_fields'";
if constexpr (string_error) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2955,20 +2956,20 @@ private:
} }
void handle_error_invalid_header_field() { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
error_.append(file_name_).append(error_msg); error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) { } else if constexpr (throw_on_error) {
throw ss::exception{error_msg}; throw ss::exception{file_name_ + error_msg};
} else { } else {
error_ = true; error_ = true;
} }
} }
void handle_error_duplicate_header_field(const std::string& field) { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2981,7 +2982,7 @@ private:
} }
void handle_error_invalid_header_split(const header_splitter& splitter) { 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) { if constexpr (string_error) {
error_.clear(); error_.clear();
@ -2993,6 +2994,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 { void decorate_rethrow(const ss::exception& e) const {
static_assert(throw_on_error, static_assert(throw_on_error,
"throw_on_error needs to be enabled to use this method"); "throw_on_error needs to be enabled to use this method");
@ -3003,14 +3012,6 @@ private:
.append(e.what())}; .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 // line reading
//////////////// ////////////////

View File

@ -399,7 +399,9 @@ void test_data_combinations(const std::vector<column>& input_data,
fields.push_back(header[index]); fields.push_back(header[index]);
} }
p.use_fields(fields); if constexpr (!setup::ignore_header) {
p.use_fields(fields);
}
if (!p.valid()) { if (!p.valid()) {
if constexpr (setup::string_error) { if constexpr (setup::string_error) {