Skip to content

Parametrized chaotic::Error with Value for throwing format-derived exceptions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -8,6 +8,7 @@
#include <userver/formats/common/items.hpp>
#include <userver/formats/json/value_builder.hpp>
#include <userver/utils/trivial_map.hpp>
#include <userver/chaotic/exception.hpp>

USERVER_NAMESPACE_BEGIN

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
3 changes: 2 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,7 @@ 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
20 changes: 13 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,10 @@ 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 +82,10 @@ 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 +97,12 @@ 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 +116,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
17 changes: 11 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,8 @@ 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 +20,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 +31,28 @@ 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
17 changes: 11 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,16 @@ 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 +80,8 @@ 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 +144,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 +161,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 +186,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
Loading