Skip to content

Commit 233365c

Browse files
committed
feat chaotic: optimize error reporting in validators
Tests: протестировано CI commit_hash:75c04abb79be3c2be80396466a7d012672fe266d
1 parent 47105c3 commit 233365c

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

chaotic/include/userver/chaotic/sax_parser.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ class WithValidators final : private formats::json::parser::Subscriber<typename
6666

6767
private:
6868
void OnSend(ResultType&& value) override {
69-
try {
70-
(Validator::Validate(value), ...);
71-
} catch (const std::exception& e) {
69+
[[maybe_unused]] const auto error_reporter = [this](std::string_view error) {
7270
formats::json::parser::BaseParser& base = parser_;
73-
chaotic::ThrowForPath<formats::json::Value>(e.what(), base.GetCurrentPath());
74-
}
71+
chaotic::ThrowForPath<formats::json::Value>(error, base.GetCurrentPath());
72+
};
73+
(Validator::Validate(value, error_reporter), ...);
74+
7575
subscriber_->OnSend(std::move(value));
7676
}
7777

chaotic/include/userver/chaotic/validators.hpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <cstdint>
4-
#include <stdexcept>
54
#include <string>
65

76
#include <userver/chaotic/exception.hpp>
@@ -15,99 +14,100 @@ namespace chaotic {
1514

1615
template <const auto& Value>
1716
struct Minimum final {
18-
template <typename T>
19-
static void Validate(T value) {
17+
template <typename T, typename ErrorReporter>
18+
static void Validate(T value, ErrorReporter report_error) {
2019
static_assert(std::is_arithmetic_v<T>);
2120

2221
if (value < Value) {
23-
throw std::runtime_error(fmt::format("Invalid value, minimum={}, given={}", Value, value));
22+
report_error(fmt::format("Invalid value, minimum={}, given={}", Value, value));
2423
}
2524
}
2625
};
2726

2827
template <const auto& Value>
2928
struct Maximum final {
30-
template <typename T>
31-
static void Validate(T value) {
29+
template <typename T, typename ErrorReporter>
30+
static void Validate(T value, ErrorReporter report_error) {
3231
static_assert(std::is_arithmetic_v<T>);
3332

3433
if (value > Value) {
35-
throw std::runtime_error(fmt::format("Invalid value, maximum={}, given={}", Value, value));
34+
report_error(fmt::format("Invalid value, maximum={}, given={}", Value, value));
3635
}
3736
}
3837
};
3938

4039
template <const auto& Value>
4140
struct ExclusiveMinimum final {
42-
template <typename T>
43-
static void Validate(T value) {
41+
template <typename T, typename ErrorReporter>
42+
static void Validate(T value, ErrorReporter report_error) {
4443
static_assert(std::is_arithmetic_v<T>);
4544

4645
if (value <= Value) {
47-
throw std::runtime_error(fmt::format("Invalid value, exclusive minimum={}, given={}", Value, value));
46+
report_error(fmt::format("Invalid value, exclusive minimum={}, given={}", Value, value));
4847
}
4948
}
5049
};
5150

5251
template <const auto& Value>
5352
struct ExclusiveMaximum final {
54-
template <typename T>
55-
static void Validate(T value) {
53+
template <typename T, typename ErrorReporter>
54+
static void Validate(T value, ErrorReporter report_error) {
5655
static_assert(std::is_arithmetic_v<T>);
5756

5857
if (value >= Value) {
59-
throw std::runtime_error(fmt::format("Invalid value, exclusive maximum={}, given={}", Value, value));
58+
report_error(fmt::format("Invalid value, exclusive maximum={}, given={}", Value, value));
6059
}
6160
}
6261
};
6362

6463
template <std::int64_t Value>
6564
struct MinItems final {
66-
template <typename T>
67-
static void Validate(const T& value) {
65+
template <typename T, typename ErrorReporter>
66+
static void Validate(const T& value, ErrorReporter report_error) {
6867
if (value.size() < Value) {
69-
throw std::runtime_error(fmt::format("Too short array, minimum length={}, given={}", Value, value.size()));
68+
report_error(fmt::format("Too short array, minimum length={}, given={}", Value, value.size()));
7069
}
7170
}
7271
};
7372

7473
template <std::int64_t Value>
7574
struct MaxItems final {
76-
template <typename T>
77-
static void Validate(const T& value) {
75+
template <typename T, typename ErrorReporter>
76+
static void Validate(const T& value, ErrorReporter report_error) {
7877
if (value.size() > Value) {
79-
throw std::runtime_error(fmt::format("Too long array, maximum length={}, given={}", Value, value.size()));
78+
report_error(fmt::format("Too long array, maximum length={}, given={}", Value, value.size()));
8079
}
8180
}
8281
};
8382

8483
template <std::int64_t Value>
8584
struct MinLength final {
86-
static void Validate(std::string_view value) {
87-
auto length = utils::text::utf8::GetCodePointsCount(value);
85+
template <typename ErrorReporter>
86+
static void Validate(std::string_view value, ErrorReporter report_error) {
87+
const auto length = utils::text::utf8::GetCodePointsCount(value);
8888
if (length < Value) {
89-
throw std::runtime_error(fmt::format("Too short string, minimum length={}, given={}", Value, length));
89+
report_error(fmt::format("Too short string, minimum length={}, given={}", Value, length));
9090
}
9191
}
9292
};
9393

9494
template <std::int64_t Value>
9595
struct MaxLength final {
96-
static void Validate(std::string_view value) {
97-
auto length = utils::text::utf8::GetCodePointsCount(value);
96+
template <typename ErrorReporter>
97+
static void Validate(std::string_view value, ErrorReporter report_error) {
98+
const auto length = utils::text::utf8::GetCodePointsCount(value);
9899
if (length > Value) {
99-
throw std::runtime_error(fmt::format("Too long string, maximum length={}, given={}", Value, length));
100+
report_error(fmt::format("Too long string, maximum length={}, given={}", Value, length));
100101
}
101102
}
102103
};
103104

104105
template <typename... Validators, typename Obj, typename Value>
105106
void Validate(const Obj& obj, const Value& value) {
106-
try {
107-
(Validators::Validate(obj), ...);
108-
} catch (const std::exception& e) {
109-
chaotic::ThrowForValue(e.what(), value);
110-
}
107+
[[maybe_unused]] const auto error_reporter = [&value](std::string_view error) {
108+
chaotic::ThrowForValue(error, value);
109+
};
110+
(Validators::Validate(obj, error_reporter), ...);
111111
}
112112

113113
} // namespace chaotic

chaotic/include/userver/chaotic/validators_pattern.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ template <const std::string_view& Regex>
1313
struct Pattern final {
1414
static const utils::regex kRegex;
1515

16-
static void Validate(const std::string& value) {
16+
template <typename ErrorReporter>
17+
static void Validate(std::string_view value, ErrorReporter report_error) {
1718
if (!utils::regex_search(value, kRegex)) {
18-
throw std::runtime_error("doesn't match regex");
19+
report_error("doesn't match regex");
1920
}
2021
}
2122
};
2223

2324
template <const std::string_view& Regex>
24-
inline const utils::regex Pattern<Regex>::kRegex{std::string{Regex}};
25+
inline const utils::regex Pattern<Regex>::kRegex{Regex};
2526

2627
} // namespace chaotic
2728

0 commit comments

Comments
 (0)