From 809939d0e2cd42ca836c139041a24bda8fb98ac6 Mon Sep 17 00:00:00 2001 From: ado Date: Wed, 13 Mar 2024 19:39:07 +0100 Subject: [PATCH] Update parser error messages, fix parser tests --- include/ss/parser.hpp | 43 ++++++++++++++++++++++--------------------- ssp.hpp | 43 ++++++++++++++++++++++--------------------- test/test_parser2.hpp | 4 +++- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index be3266a..5305a29 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -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{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 //////////////// diff --git a/ssp.hpp b/ssp.hpp index 378c38b..6d6513a 100644 --- a/ssp.hpp +++ b/ssp.hpp @@ -2304,7 +2304,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(); @@ -2471,7 +2471,7 @@ public: auto fields = std::vector{fields_args...}; if (fields.empty()) { - handle_error_empty_mapping(); + handle_error_invalid_use_fields_argument(); return; } @@ -2789,7 +2789,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_); @@ -2850,7 +2850,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(); @@ -2863,7 +2863,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(); @@ -2876,7 +2876,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(); @@ -2889,7 +2889,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(); @@ -2941,8 +2941,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(); @@ -2955,20 +2956,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(); @@ -2981,7 +2982,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(); @@ -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 { static_assert(throw_on_error, "throw_on_error needs to be enabled to use this method"); @@ -3003,14 +3012,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 //////////////// diff --git a/test/test_parser2.hpp b/test/test_parser2.hpp index a692dd3..2fbb876 100644 --- a/test/test_parser2.hpp +++ b/test/test_parser2.hpp @@ -399,7 +399,9 @@ void test_data_combinations(const std::vector& input_data, fields.push_back(header[index]); } - p.use_fields(fields); + if constexpr (!setup::ignore_header) { + p.use_fields(fields); + } if (!p.valid()) { if constexpr (setup::string_error) {