Skip to content

Commit 686da82

Browse files
Lookingforcommitsegoon
authored andcommitted
[chaotic] exceptions should not depend on json
Parametrized chaotic exceptions with typename value. Chaotic validators now throw exceptions derived from formats::Value::Exception Tests: протестировано CI Pull Request resolved: #825 commit_hash:27cf69ecf852ecca12ac6c840a22f6524929d3c7
1 parent bbf5a29 commit 686da82

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

chaotic/include/userver/chaotic/exception.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ USERVER_NAMESPACE_BEGIN
1010

1111
namespace chaotic {
1212

13-
class Error final : public formats::json::Exception {
14-
using Exception::Exception;
13+
template <typename Value>
14+
class Error final : public Value::Exception {
15+
using Value::Exception::Exception;
1516
};
1617

1718
template <typename Value>
1819
[[noreturn]] inline void ThrowForValue(std::string_view str, Value value) {
19-
throw Error(fmt::format("Error at path '{}': {}", value.GetPath(), str));
20+
throw Error<Value>(fmt::format("Error at path '{}': {}", value.GetPath(), str));
2021
}
2122

2223
} // namespace chaotic

chaotic/include/userver/chaotic/object.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <fmt/format.h>
77

8+
#include <userver/chaotic/exception.hpp>
89
#include <userver/formats/common/items.hpp>
910
#include <userver/formats/json/value_builder.hpp>
1011
#include <userver/utils/trivial_map.hpp>
@@ -30,7 +31,7 @@ void ValidateNoAdditionalProperties(const Value& json, const utils::TrivialSet<B
3031
for (const auto& [name, value] : formats::common::Items(json)) {
3132
if (names_to_exclude.Contains(name)) continue;
3233

33-
throw std::runtime_error(fmt::format("Unknown property '{}'", name));
34+
throw Error<Value>(fmt::format("Unknown property '{}'", name));
3435
}
3536
}
3637

chaotic/integration_tests/tests/lib/array.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <userver/chaotic/array.hpp>
88
#include <userver/chaotic/primitive.hpp>
99
#include <userver/chaotic/validators.hpp>
10+
#include <userver/formats/json.hpp>
1011

1112
USERVER_NAMESPACE_BEGIN
1213

@@ -37,7 +38,9 @@ TEST(Array, OfIntWithValidators) {
3738

3839
const auto kJson1 = formats::json::MakeArray("foo");
3940
UEXPECT_THROW_MSG(
40-
kJson1.As<Arr>(), chaotic::Error, "Error at path '/': Too short array, minimum length=2, given=1"
41+
kJson1.As<Arr>(),
42+
chaotic::Error<formats::json::Value>,
43+
"Error at path '/': Too short array, minimum length=2, given=1"
4144
);
4245
}
4346

chaotic/integration_tests/tests/lib/primitive.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ TEST(Primitive, IntMinMax) {
6969
int x = kJson["foo"].As<Int>();
7070
EXPECT_EQ(x, 1);
7171

72-
UEXPECT_THROW_MSG(kJson["bar"].As<Int>(), chaotic::Error, "Error at path 'bar': Invalid value, minimum=1, given=0");
73-
UEXPECT_THROW_MSG(kJson["zoo"].As<Int>(), chaotic::Error, "Error at path 'zoo': Invalid value, maximum=5, given=6");
72+
UEXPECT_THROW_MSG(
73+
kJson["bar"].As<Int>(),
74+
chaotic::Error<formats::json::Value>,
75+
"Error at path 'bar': Invalid value, minimum=1, given=0"
76+
);
77+
UEXPECT_THROW_MSG(
78+
kJson["zoo"].As<Int>(),
79+
chaotic::Error<formats::json::Value>,
80+
"Error at path 'zoo': Invalid value, maximum=5, given=6"
81+
);
7482
}
7583

7684
TEST(Primitive, UserTypeMinMax) {
@@ -80,8 +88,16 @@ TEST(Primitive, UserTypeMinMax) {
8088
MyInt x = kJson["foo"].As<Int>();
8189
EXPECT_EQ(x.value, 1);
8290

83-
UEXPECT_THROW_MSG(kJson["bar"].As<Int>(), chaotic::Error, "Error at path 'bar': Invalid value, minimum=1, given=0");
84-
UEXPECT_THROW_MSG(kJson["zoo"].As<Int>(), chaotic::Error, "Error at path 'zoo': Invalid value, maximum=5, given=6");
91+
UEXPECT_THROW_MSG(
92+
kJson["bar"].As<Int>(),
93+
chaotic::Error<formats::json::Value>,
94+
"Error at path 'bar': Invalid value, minimum=1, given=0"
95+
);
96+
UEXPECT_THROW_MSG(
97+
kJson["zoo"].As<Int>(),
98+
chaotic::Error<formats::json::Value>,
99+
"Error at path 'zoo': Invalid value, maximum=5, given=6"
100+
);
85101
}
86102

87103
TEST(Primitive, StringMinMaxLength) {
@@ -93,10 +109,14 @@ TEST(Primitive, StringMinMaxLength) {
93109
EXPECT_EQ(x, "12");
94110

95111
UEXPECT_THROW_MSG(
96-
kLocalJson["1"].As<Str>(), chaotic::Error, "Error at path '1': Too short string, minimum length=2, given=1"
112+
kLocalJson["1"].As<Str>(),
113+
chaotic::Error<formats::json::Value>,
114+
"Error at path '1': Too short string, minimum length=2, given=1"
97115
);
98116
UEXPECT_THROW_MSG(
99-
kLocalJson["6"].As<Str>(), chaotic::Error, "Error at path '6': Too long string, maximum length=5, given=6"
117+
kLocalJson["6"].As<Str>(),
118+
chaotic::Error<formats::json::Value>,
119+
"Error at path '6': Too long string, maximum length=5, given=6"
100120
);
101121
}
102122

@@ -110,7 +130,7 @@ TEST(Primitive, StringPattern) {
110130
std::string x = kLocalJson["1"].As<Str>();
111131
EXPECT_EQ(x, "foo");
112132

113-
UEXPECT_THROW_MSG(kLocalJson["2"].As<Str>(), chaotic::Error, "doesn't match regex");
133+
UEXPECT_THROW_MSG(kLocalJson["2"].As<Str>(), chaotic::Error<formats::json::Value>, "doesn't match regex");
114134
}
115135

116136
USERVER_NAMESPACE_END

chaotic/integration_tests/tests/render/minmax.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ USERVER_NAMESPACE_BEGIN
1010
TEST(MinMax, ExclusiveInt) {
1111
auto json = formats::json::MakeObject("foo", 1);
1212
UEXPECT_THROW_MSG(
13-
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'foo': Invalid value, exclusive minimum=1, given=1"
13+
json.As<ns::IntegerObject>(),
14+
chaotic::Error<formats::json::Value>,
15+
"Error at path 'foo': Invalid value, exclusive minimum=1, given=1"
1416
);
1517

1618
json = formats::json::MakeObject("foo", 2);
@@ -19,7 +21,7 @@ TEST(MinMax, ExclusiveInt) {
1921
json = formats::json::MakeObject("foo", 20);
2022
UEXPECT_THROW_MSG(
2123
json.As<ns::IntegerObject>(),
22-
chaotic::Error,
24+
chaotic::Error<formats::json::Value>,
2325
"Error at path 'foo': Invalid value, exclusive maximum=20, given=20"
2426
);
2527

@@ -30,24 +32,32 @@ TEST(MinMax, ExclusiveInt) {
3032
TEST(MinMax, String) {
3133
auto json = formats::json::MakeObject("bar", "");
3234
UEXPECT_THROW_MSG(
33-
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'bar': Too short string, minimum length=2, given=0"
35+
json.As<ns::IntegerObject>(),
36+
chaotic::Error<formats::json::Value>,
37+
"Error at path 'bar': Too short string, minimum length=2, given=0"
3438
);
3539

3640
json = formats::json::MakeObject("bar", "longlonglong");
3741
UEXPECT_THROW_MSG(
38-
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'bar': Too long string, maximum length=5, given=12"
42+
json.As<ns::IntegerObject>(),
43+
chaotic::Error<formats::json::Value>,
44+
"Error at path 'bar': Too long string, maximum length=5, given=12"
3945
);
4046
}
4147

4248
TEST(MinMax, Array) {
4349
auto json = formats::json::MakeObject("zoo", formats::json::MakeArray(1));
4450
UEXPECT_THROW_MSG(
45-
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'zoo': Too short array, minimum length=2, given=1"
51+
json.As<ns::IntegerObject>(),
52+
chaotic::Error<formats::json::Value>,
53+
"Error at path 'zoo': Too short array, minimum length=2, given=1"
4654
);
4755

4856
json = formats::json::MakeObject("zoo", formats::json::MakeArray(1, 2, 3, 4, 5, 6, 7, 8));
4957
UEXPECT_THROW_MSG(
50-
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'zoo': Too long array, maximum length=5, given=8"
58+
json.As<ns::IntegerObject>(),
59+
chaotic::Error<formats::json::Value>,
60+
"Error at path 'zoo': Too long array, maximum length=5, given=8"
5161
);
5262
}
5363

chaotic/integration_tests/tests/render/simple.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ TEST(Simple, DefaultFieldValue) {
4545
TEST(Simple, IntegerMinimum) {
4646
auto json = formats::json::MakeObject("int3", 1, "int", -10);
4747
UEXPECT_THROW_MSG(
48-
json.As<ns::SimpleObject>(), chaotic::Error, "Error at path 'int': Invalid value, minimum=-1, given=-10"
48+
json.As<ns::SimpleObject>(),
49+
chaotic::Error<formats::json::Value>,
50+
"Error at path 'int': Invalid value, minimum=-1, given=-10"
4951
);
5052
}
5153

5254
TEST(Simple, IntegerMaximum) {
5355
auto json = formats::json::MakeObject("int3", 1, "int", 11);
5456
UEXPECT_THROW_MSG(
55-
json.As<ns::SimpleObject>(), chaotic::Error, "Error at path 'int': Invalid value, maximum=10, given=11"
57+
json.As<ns::SimpleObject>(),
58+
chaotic::Error<formats::json::Value>,
59+
"Error at path 'int': Invalid value, maximum=10, given=11"
5660
);
5761
}
5862

@@ -78,7 +82,9 @@ TEST(Simple, IntegerFormat) {
7882
TEST(Simple, ObjectWithRefType) {
7983
auto json = formats::json::MakeObject("integer", 0);
8084
UEXPECT_THROW_MSG(
81-
json.As<ns::ObjectWithRef>(), chaotic::Error, "Error at path 'integer': Invalid value, minimum=1, given=0"
85+
json.As<ns::ObjectWithRef>(),
86+
chaotic::Error<formats::json::Value>,
87+
"Error at path 'integer': Invalid value, minimum=1, given=0"
8288
);
8389
}
8490

@@ -141,7 +147,9 @@ TEST(Simple, ObjectExtraMemberFalse) {
141147
TEST(Simple, ObjectWithAdditionalPropertiesFalseStrict) {
142148
auto json = formats::json::MakeObject("foo", 1, "bar", 2);
143149
UEXPECT_THROW_MSG(
144-
json.As<ns::ObjectWithAdditionalPropertiesFalseStrict>(), std::runtime_error, "Unknown property 'bar'"
150+
json.As<ns::ObjectWithAdditionalPropertiesFalseStrict>(),
151+
chaotic::Error<formats::json::Value>,
152+
"Unknown property 'bar'"
145153
);
146154
}
147155

@@ -156,7 +164,7 @@ TEST(Simple, IntegerEnum) {
156164
auto json2 = formats::json::MakeObject("one", 5);
157165
UEXPECT_THROW_MSG(
158166
json2["one"].As<ns::IntegerEnum>(),
159-
chaotic::Error,
167+
chaotic::Error<formats::json::Value>,
160168
"Error at path 'one': Invalid enum value (5) for type ns::IntegerEnum"
161169
);
162170

@@ -181,7 +189,7 @@ TEST(Simple, StringEnum) {
181189
auto json2 = formats::json::MakeObject("one", "zoo");
182190
UEXPECT_THROW_MSG(
183191
json2["one"].As<ns::StringEnum>(),
184-
chaotic::Error,
192+
chaotic::Error<formats::json::Value>,
185193
"Error at path 'one': Invalid enum value (zoo) for type ns::StringEnum"
186194
);
187195

0 commit comments

Comments
 (0)