Skip to content

Commit

Permalink
[chaotic] exceptions should not depend on json
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Lookingforcommit authored and segoon committed Jan 20, 2025
1 parent bbf5a29 commit 686da82
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 24 deletions.
7 changes: 4 additions & 3 deletions chaotic/include/userver/chaotic/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ USERVER_NAMESPACE_BEGIN

namespace chaotic {

class Error final : public formats::json::Exception {
using Exception::Exception;
template <typename Value>
class Error final : public Value::Exception {
using Value::Exception::Exception;
};

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

} // namespace chaotic
Expand Down
3 changes: 2 additions & 1 deletion chaotic/include/userver/chaotic/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <fmt/format.h>

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

throw std::runtime_error(fmt::format("Unknown property '{}'", name));
throw Error<Value>(fmt::format("Unknown property '{}'", name));
}
}

Expand Down
5 changes: 4 additions & 1 deletion chaotic/integration_tests/tests/lib/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <userver/chaotic/array.hpp>
#include <userver/chaotic/primitive.hpp>
#include <userver/chaotic/validators.hpp>
#include <userver/formats/json.hpp>

USERVER_NAMESPACE_BEGIN

Expand Down Expand Up @@ -37,7 +38,9 @@ TEST(Array, OfIntWithValidators) {

const auto kJson1 = formats::json::MakeArray("foo");
UEXPECT_THROW_MSG(
kJson1.As<Arr>(), chaotic::Error, "Error at path '/': Too short array, minimum length=2, given=1"
kJson1.As<Arr>(),
chaotic::Error<formats::json::Value>,
"Error at path '/': Too short array, minimum length=2, given=1"
);
}

Expand Down
34 changes: 27 additions & 7 deletions chaotic/integration_tests/tests/lib/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ TEST(Primitive, IntMinMax) {
int x = kJson["foo"].As<Int>();
EXPECT_EQ(x, 1);

UEXPECT_THROW_MSG(kJson["bar"].As<Int>(), chaotic::Error, "Error at path 'bar': Invalid value, minimum=1, given=0");
UEXPECT_THROW_MSG(kJson["zoo"].As<Int>(), chaotic::Error, "Error at path 'zoo': Invalid value, maximum=5, given=6");
UEXPECT_THROW_MSG(
kJson["bar"].As<Int>(),
chaotic::Error<formats::json::Value>,
"Error at path 'bar': Invalid value, minimum=1, given=0"
);
UEXPECT_THROW_MSG(
kJson["zoo"].As<Int>(),
chaotic::Error<formats::json::Value>,
"Error at path 'zoo': Invalid value, maximum=5, given=6"
);
}

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

UEXPECT_THROW_MSG(kJson["bar"].As<Int>(), chaotic::Error, "Error at path 'bar': Invalid value, minimum=1, given=0");
UEXPECT_THROW_MSG(kJson["zoo"].As<Int>(), chaotic::Error, "Error at path 'zoo': Invalid value, maximum=5, given=6");
UEXPECT_THROW_MSG(
kJson["bar"].As<Int>(),
chaotic::Error<formats::json::Value>,
"Error at path 'bar': Invalid value, minimum=1, given=0"
);
UEXPECT_THROW_MSG(
kJson["zoo"].As<Int>(),
chaotic::Error<formats::json::Value>,
"Error at path 'zoo': Invalid value, maximum=5, given=6"
);
}

TEST(Primitive, StringMinMaxLength) {
Expand All @@ -93,10 +109,14 @@ TEST(Primitive, StringMinMaxLength) {
EXPECT_EQ(x, "12");

UEXPECT_THROW_MSG(
kLocalJson["1"].As<Str>(), chaotic::Error, "Error at path '1': Too short string, minimum length=2, given=1"
kLocalJson["1"].As<Str>(),
chaotic::Error<formats::json::Value>,
"Error at path '1': Too short string, minimum length=2, given=1"
);
UEXPECT_THROW_MSG(
kLocalJson["6"].As<Str>(), chaotic::Error, "Error at path '6': Too long string, maximum length=5, given=6"
kLocalJson["6"].As<Str>(),
chaotic::Error<formats::json::Value>,
"Error at path '6': Too long string, maximum length=5, given=6"
);
}

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

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

USERVER_NAMESPACE_END
22 changes: 16 additions & 6 deletions chaotic/integration_tests/tests/render/minmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ USERVER_NAMESPACE_BEGIN
TEST(MinMax, ExclusiveInt) {
auto json = formats::json::MakeObject("foo", 1);
UEXPECT_THROW_MSG(
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'foo': Invalid value, exclusive minimum=1, given=1"
json.As<ns::IntegerObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'foo': Invalid value, exclusive minimum=1, given=1"
);

json = formats::json::MakeObject("foo", 2);
Expand All @@ -19,7 +21,7 @@ TEST(MinMax, ExclusiveInt) {
json = formats::json::MakeObject("foo", 20);
UEXPECT_THROW_MSG(
json.As<ns::IntegerObject>(),
chaotic::Error,
chaotic::Error<formats::json::Value>,
"Error at path 'foo': Invalid value, exclusive maximum=20, given=20"
);

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

json = formats::json::MakeObject("bar", "longlonglong");
UEXPECT_THROW_MSG(
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'bar': Too long string, maximum length=5, given=12"
json.As<ns::IntegerObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'bar': Too long string, maximum length=5, given=12"
);
}

TEST(MinMax, Array) {
auto json = formats::json::MakeObject("zoo", formats::json::MakeArray(1));
UEXPECT_THROW_MSG(
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'zoo': Too short array, minimum length=2, given=1"
json.As<ns::IntegerObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'zoo': Too short array, minimum length=2, given=1"
);

json = formats::json::MakeObject("zoo", formats::json::MakeArray(1, 2, 3, 4, 5, 6, 7, 8));
UEXPECT_THROW_MSG(
json.As<ns::IntegerObject>(), chaotic::Error, "Error at path 'zoo': Too long array, maximum length=5, given=8"
json.As<ns::IntegerObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'zoo': Too long array, maximum length=5, given=8"
);
}

Expand Down
20 changes: 14 additions & 6 deletions chaotic/integration_tests/tests/render/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ TEST(Simple, DefaultFieldValue) {
TEST(Simple, IntegerMinimum) {
auto json = formats::json::MakeObject("int3", 1, "int", -10);
UEXPECT_THROW_MSG(
json.As<ns::SimpleObject>(), chaotic::Error, "Error at path 'int': Invalid value, minimum=-1, given=-10"
json.As<ns::SimpleObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'int': Invalid value, minimum=-1, given=-10"
);
}

TEST(Simple, IntegerMaximum) {
auto json = formats::json::MakeObject("int3", 1, "int", 11);
UEXPECT_THROW_MSG(
json.As<ns::SimpleObject>(), chaotic::Error, "Error at path 'int': Invalid value, maximum=10, given=11"
json.As<ns::SimpleObject>(),
chaotic::Error<formats::json::Value>,
"Error at path 'int': Invalid value, maximum=10, given=11"
);
}

Expand All @@ -78,7 +82,9 @@ TEST(Simple, IntegerFormat) {
TEST(Simple, ObjectWithRefType) {
auto json = formats::json::MakeObject("integer", 0);
UEXPECT_THROW_MSG(
json.As<ns::ObjectWithRef>(), chaotic::Error, "Error at path 'integer': Invalid value, minimum=1, given=0"
json.As<ns::ObjectWithRef>(),
chaotic::Error<formats::json::Value>,
"Error at path 'integer': Invalid value, minimum=1, given=0"
);
}

Expand Down Expand Up @@ -141,7 +147,9 @@ TEST(Simple, ObjectExtraMemberFalse) {
TEST(Simple, ObjectWithAdditionalPropertiesFalseStrict) {
auto json = formats::json::MakeObject("foo", 1, "bar", 2);
UEXPECT_THROW_MSG(
json.As<ns::ObjectWithAdditionalPropertiesFalseStrict>(), std::runtime_error, "Unknown property 'bar'"
json.As<ns::ObjectWithAdditionalPropertiesFalseStrict>(),
chaotic::Error<formats::json::Value>,
"Unknown property 'bar'"
);
}

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

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

Expand Down

0 comments on commit 686da82

Please sign in to comment.