Update error handling methods

This commit is contained in:
ado 2023-08-05 11:45:31 +02:00
parent 81484f737f
commit a7ea9e42e5
3 changed files with 67 additions and 58 deletions

View File

@ -124,7 +124,7 @@ public:
if (splitter_.valid()) {
return convert<Ts...>(splitter_.split_data_);
} else {
set_error_bad_split();
handle_error_bad_split();
return {};
}
}
@ -234,7 +234,7 @@ private:
return error;
}
void set_error_bad_split() {
void handle_error_bad_split() {
if constexpr (string_error) {
error_.clear();
error_.append(splitter_.error_msg());
@ -243,31 +243,31 @@ private:
}
}
void set_error_unterminated_escape() {
void handle_error_unterminated_escape() {
if constexpr (string_error) {
error_.clear();
splitter_.set_error_unterminated_escape();
splitter_.handle_error_unterminated_escape();
error_.append(splitter_.error_msg());
} else if constexpr (throw_on_error) {
splitter_.set_error_unterminated_escape();
splitter_.handle_error_unterminated_escape();
} else {
error_ = true;
}
}
void set_error_unterminated_quote() {
void handle_error_unterminated_quote() {
if constexpr (string_error) {
error_.clear();
splitter_.set_error_unterminated_quote();
splitter_.handle_error_unterminated_quote();
error_.append(splitter_.error_msg());
} else if constexpr (throw_on_error) {
splitter_.set_error_unterminated_quote();
splitter_.handle_error_unterminated_quote();
} else {
error_ = true;
}
}
void set_error_multiline_limit_reached() {
void handle_error_multiline_limit_reached() {
constexpr static auto error_msg = "multiline limit reached";
if constexpr (string_error) {
@ -280,7 +280,7 @@ private:
}
}
void set_error_invalid_conversion(const string_range msg, size_t pos) {
void handle_error_invalid_conversion(const string_range msg, size_t pos) {
constexpr static auto error_msg = "invalid conversion for parameter ";
if constexpr (string_error) {
@ -293,8 +293,8 @@ private:
}
}
void set_error_validate(const char* const error, const string_range msg,
size_t pos) {
void handle_error_validation_failed(const char* const error,
const string_range msg, size_t pos) {
if constexpr (string_error) {
error_.clear();
error_.append(error).append(" ").append(error_sufix(msg, pos));
@ -305,7 +305,7 @@ private:
}
}
void set_error_number_of_columns(size_t expected_pos, size_t pos) {
void handle_error_number_of_columns(size_t expected_pos, size_t pos) {
constexpr static auto error_msg1 =
"invalid number of columns, expected: ";
constexpr static auto error_msg2 = ", got: ";
@ -324,8 +324,8 @@ private:
}
}
void set_error_incompatible_mapping(size_t argument_size,
size_t mapping_size) {
void handle_error_incompatible_mapping(size_t argument_size,
size_t mapping_size) {
constexpr static auto error_msg1 =
"number of arguments does not match mapping, expected: ";
constexpr static auto error_msg2 = ", got: ";
@ -353,24 +353,25 @@ private:
clear_error();
if (!splitter_.valid()) {
set_error_bad_split();
handle_error_bad_split();
return {};
}
if (!columns_mapped()) {
if (sizeof...(Ts) != elems.size()) {
set_error_number_of_columns(sizeof...(Ts), elems.size());
handle_error_number_of_columns(sizeof...(Ts), elems.size());
return {};
}
} else {
if (sizeof...(Ts) != column_mappings_.size()) {
set_error_incompatible_mapping(sizeof...(Ts),
column_mappings_.size());
handle_error_incompatible_mapping(sizeof...(Ts),
column_mappings_.size());
return {};
}
if (elems.size() != number_of_columns_) {
set_error_number_of_columns(number_of_columns_, elems.size());
handle_error_number_of_columns(number_of_columns_,
elems.size());
return {};
}
}
@ -429,16 +430,17 @@ private:
}
if (!extract(msg.first, msg.second, dst)) {
set_error_invalid_conversion(msg, pos);
handle_error_invalid_conversion(msg, pos);
return;
}
if constexpr (has_m_ss_valid_t<T>) {
if (T validator; !validator.ss_valid(dst)) {
if constexpr (has_m_error_t<T>) {
set_error_validate(validator.error(), msg, pos);
handle_error_validation_failed(validator.error(), msg, pos);
} else {
set_error_validate("validation error", msg, pos);
handle_error_validation_failed("validation error", msg,
pos);
}
return;
}

View File

@ -39,7 +39,7 @@ public:
if (reader_.file_) {
read_line();
if (eof_) {
set_error_eof_reached();
handle_error_eof_reached();
return;
}
if constexpr (ignore_header) {
@ -48,7 +48,7 @@ public:
header_ = reader_.get_header();
}
} else {
set_error_file_not_open();
handle_error_file_not_open();
eof_ = true;
}
}
@ -100,7 +100,7 @@ public:
reader_.update();
if (!reader_.converter_.valid()) {
set_error_invalid_conversion();
handle_error_invalid_conversion();
read_line();
return {};
}
@ -108,14 +108,14 @@ public:
clear_error();
if (eof_) {
set_error_eof_reached();
handle_error_eof_reached();
return {};
}
auto value = reader_.converter_.template convert<T, Ts...>();
if (!reader_.converter_.valid()) {
set_error_invalid_conversion();
handle_error_invalid_conversion();
}
read_line();
@ -129,7 +129,7 @@ public:
template <typename... Ts>
void use_fields(const Ts&... fields_args) {
if constexpr (ignore_header) {
set_error_header_ignored();
handle_error_header_ignored();
return;
}
@ -140,7 +140,7 @@ public:
auto fields = std::vector<std::string>{fields_args...};
if (fields.empty()) {
set_error_empty_mapping();
handle_error_empty_mapping();
return;
}
@ -148,14 +148,14 @@ public:
for (const auto& field : fields) {
if (std::count(fields.begin(), fields.end(), field) != 1) {
set_error_field_used_multiple_times(field);
handle_error_field_used_multiple_times(field);
return;
}
auto index = header_index(field);
if (!index) {
set_error_invalid_field(field);
handle_error_invalid_field(field);
return;
}
@ -344,7 +344,7 @@ public:
auto value =
parser_.reader_.converter_.template convert<U, Us...>();
if (!parser_.reader_.converter_.valid()) {
parser_.set_error_invalid_conversion();
parser_.handle_error_invalid_conversion();
}
return value;
}
@ -391,7 +391,7 @@ private:
constexpr bool returns_void = std::is_same_v<Ret, void>;
if constexpr (!returns_void) {
if (!try_invoke_impl(arg, std::forward<Fun>(fun))) {
set_error_failed_check();
handle_error_failed_check();
}
} else {
try_invoke_impl(arg, std::forward<Fun>(fun));
@ -454,10 +454,11 @@ private:
}
}
void set_error_failed_check() {
void handle_error_failed_check() {
constexpr static auto error_msg = " failed check";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg};
@ -466,10 +467,11 @@ private:
}
}
void set_error_file_not_open() {
void handle_error_file_not_open() {
constexpr static auto error_msg = " could not be opened";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg};
@ -478,10 +480,11 @@ private:
}
}
void set_error_eof_reached() {
void handle_error_eof_reached() {
constexpr static auto error_msg = " read on end of file";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg};
@ -490,8 +493,9 @@ private:
}
}
void set_error_invalid_conversion() {
void handle_error_invalid_conversion() {
if constexpr (string_error) {
error_.clear();
error_.append(file_name_)
.append(" ")
.append(std::to_string(reader_.line_number_))
@ -502,12 +506,13 @@ private:
}
}
void set_error_header_ignored() {
void handle_error_header_ignored() {
constexpr static auto error_msg =
": \"the header row is ignored within the setup it cannot be "
"used\"";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg};
@ -516,11 +521,12 @@ private:
}
}
void set_error_invalid_field(const std::string& field) {
void handle_error_invalid_field(const std::string& field) {
constexpr static auto error_msg =
": header does not contain given field: ";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg).append(field);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg + field};
@ -529,10 +535,11 @@ private:
}
}
void set_error_field_used_multiple_times(const std::string& field) {
void handle_error_field_used_multiple_times(const std::string& field) {
constexpr static auto error_msg = ": given field used multiple times: ";
if constexpr (string_error) {
error_.clear();
error_.append(file_name_).append(error_msg).append(field);
} else if constexpr (throw_on_error) {
throw ss::exception{file_name_ + error_msg + field};
@ -541,7 +548,7 @@ private:
}
}
void set_error_empty_mapping() {
void handle_error_empty_mapping() {
constexpr static auto error_msg = "received empty mapping";
if constexpr (string_error) {
@ -664,7 +671,7 @@ private:
if (!append_next_line_to_buffer(next_line_buffer_,
next_line_size_)) {
next_line_converter_.set_error_unterminated_escape();
next_line_converter_.handle_error_unterminated_escape();
return;
}
}
@ -682,7 +689,7 @@ private:
if (!append_next_line_to_buffer(next_line_buffer_,
next_line_size_)) {
next_line_converter_.set_error_unterminated_quote();
next_line_converter_.handle_error_unterminated_quote();
return;
}
@ -695,7 +702,7 @@ private:
if (!append_next_line_to_buffer(next_line_buffer_,
next_line_size_)) {
next_line_converter_
.set_error_unterminated_escape();
.handle_error_unterminated_escape();
return;
}
}
@ -716,7 +723,7 @@ private:
bool multiline_limit_reached(size_t& limit) {
if constexpr (multiline::size > 0) {
if (limit++ >= multiline::size) {
next_line_converter_.set_error_multiline_limit_reached();
next_line_converter_.handle_error_multiline_limit_reached();
return true;
}
}

View File

@ -82,7 +82,7 @@ private:
// resplitting, continue from last slice
if (!quote::enabled || !multiline::enabled || split_data_.empty() ||
!unterminated_quote()) {
set_error_invalid_resplit();
handle_error_invalid_resplit();
return split_data_;
}
@ -91,7 +91,7 @@ private:
// safety measure
if (new_size != -1 && static_cast<size_t>(new_size) < begin) {
set_error_invalid_resplit();
handle_error_invalid_resplit();
return split_data_;
}
@ -123,7 +123,7 @@ private:
unterminated_quote_ = false;
}
void set_error_empty_delimiter() {
void handle_error_empty_delimiter() {
constexpr static auto error_msg = "empty delimiter";
if constexpr (string_error) {
@ -136,7 +136,7 @@ private:
}
}
void set_error_mismatched_quote(size_t n) {
void handle_error_mismatched_quote(size_t n) {
constexpr static auto error_msg = "mismatched quote at position: ";
if constexpr (string_error) {
@ -150,7 +150,7 @@ private:
}
// TODO rename with handle error
void set_error_unterminated_escape() {
void handle_error_unterminated_escape() {
constexpr static auto error_msg =
"unterminated escape at the end of the line";
@ -165,7 +165,7 @@ private:
}
// TODO handle this efficiently (if multiline is enabled)
void set_error_unterminated_quote() {
void handle_error_unterminated_quote() {
constexpr static auto error_msg = "unterminated quote";
if constexpr (string_error) {
@ -178,7 +178,7 @@ private:
}
}
void set_error_invalid_resplit() {
void handle_error_invalid_resplit() {
constexpr static auto error_msg =
"invalid resplit, new line must be longer"
"than the end of the last slice";
@ -263,7 +263,7 @@ private:
if (escape::match(*curr)) {
if (curr[1] == '\0') {
if constexpr (!multiline::enabled) {
set_error_unterminated_escape();
handle_error_unterminated_escape();
}
done_ = true;
return;
@ -311,7 +311,7 @@ private:
clear_error();
switch (delimiter.size()) {
case 0:
set_error_empty_delimiter();
handle_error_empty_delimiter();
return split_data_;
case 1:
return split_impl(delimiter[0]);
@ -392,7 +392,7 @@ private:
// eol, unterminated escape
// eg: ... "hel\\0
if constexpr (!multiline::enabled) {
set_error_unterminated_escape();
handle_error_unterminated_escape();
}
done_ = true;
break;
@ -412,7 +412,7 @@ private:
shift_and_set_current();
unterminated_quote_ = true;
if constexpr (!multiline::enabled) {
set_error_unterminated_quote();
handle_error_unterminated_quote();
}
split_data_.emplace_back(line_, begin_);
done_ = true;
@ -452,7 +452,7 @@ private:
} else {
// mismatched quote
// eg: ...,"hel"lo,... -> error
set_error_mismatched_quote(end_ - line_);
handle_error_mismatched_quote(end_ - line_);
split_data_.emplace_back(line_, begin_);
}
done_ = true;