Skip to content

Commit 601c8da

Browse files
Make sure that read does not abort when the default values do not match the validators
1 parent 594707d commit 601c8da

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

include/rfl/parsing/Parser_default.hpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct Parser {
8585
* @param _var The input variable to read from.
8686
* @return A Result containing the parsed value or an error.
8787
*/
88-
static auto read(const R& _r, const InputVarType& _var) noexcept {
88+
static auto read(const R& _r, const InputVarType& _var) {
8989
if constexpr (internal::is_basic_type_v<T>) {
9090
return ParserBasicType<R, W, T, ProcessorsType>::read(_r, _var);
9191

@@ -644,9 +644,10 @@ struct Parser {
644644
(*_definitions)[name] =
645645
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(
646646
_definitions, &view);
647-
}else {
647+
} else {
648648
(*_definitions)[name] =
649-
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(_definitions);
649+
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(
650+
_definitions);
650651
}
651652
}
652653
}
@@ -692,16 +693,22 @@ struct Parser {
692693
/// so we only use it when the DefaultIfMissing preprocessor is added.
693694
static Result<T> read_struct_with_default(const R& _r,
694695
const InputVarType& _var) {
695-
auto t = T{};
696-
auto view = ProcessorsType::template process<T>(to_view(t));
697-
using ViewType = decltype(view);
698-
const auto err =
699-
Parser<R, W, ViewType, ProcessorsType>::read_view_with_default(_r, _var,
700-
&view);
701-
if (err) [[unlikely]] {
702-
return error(*err);
696+
try {
697+
auto t = T{}; // This might fail, for instance if the default value does
698+
// not satisfy the validator, but in that case we will just
699+
// return the error.
700+
auto view = ProcessorsType::template process<T>(to_view(t));
701+
using ViewType = decltype(view);
702+
const auto err =
703+
Parser<R, W, ViewType, ProcessorsType>::read_view_with_default(
704+
_r, _var, &view);
705+
if (err) [[unlikely]] {
706+
return error(*err);
707+
}
708+
return t;
709+
} catch (std::exception& e) {
710+
return error(e.what());
703711
}
704-
return t;
705712
}
706713
};
707714

0 commit comments

Comments
 (0)