Skip to content

Commit e353b01

Browse files
committed
Fix misc. issues with schema_exception handling
1 parent 2278cc3 commit e353b01

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ namespace graphql::service {
2929
class schema_exception : public std::exception
3030
{
3131
public:
32-
schema_exception() = delete;
33-
schema_exception(const schema_exception&) = delete;
34-
explicit schema_exception(schema_exception&&) = default;
35-
3632
explicit schema_exception(std::vector<std::string>&& messages);
3733

34+
schema_exception() = delete;
35+
3836
const char* what() const noexcept override;
3937

4038
const response::Value& getErrors() const noexcept;
@@ -555,6 +553,16 @@ struct ModifiedResult
555553
}
556554
}
557555
}
556+
catch (schema_exception& scx)
557+
{
558+
auto messages = scx.getErrors().release<response::ListType>();
559+
560+
errors.reserve(errors.size() + messages.size());
561+
for (auto& error : messages)
562+
{
563+
errors.emplace_back(std::move(error));
564+
}
565+
}
558566
catch (const std::exception & ex)
559567
{
560568
std::ostringstream message;
@@ -595,11 +603,16 @@ struct ModifiedResult
595603
return std::async(std::launch::deferred,
596604
[](auto && resultFuture, ResolverParams && paramsFuture, ResolverCallback && resolverFuture) noexcept
597605
{
598-
response::Value document(response::Type::Map);
606+
response::Value data;
607+
response::Value errors(response::Type::List);
599608

600609
try
601610
{
602-
document.emplace_back(std::string{ strData }, resolverFuture(resultFuture.get(), paramsFuture));
611+
data = resolverFuture(resultFuture.get(), paramsFuture);
612+
}
613+
catch (schema_exception& scx)
614+
{
615+
errors = scx.getErrors();
603616
}
604617
catch (const std::exception & ex)
605618
{
@@ -608,11 +621,18 @@ struct ModifiedResult
608621
message << "Field name: " << paramsFuture.fieldName
609622
<< " unknown error: " << ex.what();
610623

611-
response::Value errors(response::Type::List);
612624
response::Value error(response::Type::Map);
613625

614626
error.emplace_back(std::string{ strMessage }, response::Value(message.str()));
615627
errors.emplace_back(std::move(error));
628+
}
629+
630+
response::Value document(response::Type::Map);
631+
632+
document.emplace_back(std::string{ strData }, std::move(data));
633+
634+
if (errors.size() > 0)
635+
{
616636
document.emplace_back(std::string{ strErrors }, std::move(errors));
617637
}
618638

src/GraphQLResponse.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,36 @@ struct TypedData : std::variant<
6969

7070
Value::Value(Type type /*= Type::Null*/)
7171
: _type(type)
72-
, _data(std::make_unique<TypedData>())
7372
{
7473
switch (type)
7574
{
7675
case Type::Map:
77-
*_data = { std::make_optional<MapData>() };
76+
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<MapData>() });
7877
break;
7978

8079
case Type::List:
81-
*_data = { std::make_optional<ListData>() };
80+
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<ListData>() });
8281
break;
8382

8483
case Type::String:
8584
case Type::EnumValue:
86-
*_data = { std::make_optional<StringOrEnumData>() };
85+
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<StringOrEnumData>() });
8786
break;
8887

8988
case Type::Scalar:
90-
*_data = { std::make_optional<ScalarData>() };
89+
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<ScalarData>() });
9190
break;
9291

9392
case Type::Boolean:
94-
*_data = { BooleanType{ false } };
93+
_data = std::make_unique<TypedData>(TypedData{ BooleanType{ false } });
9594
break;
9695

9796
case Type::Int:
98-
*_data = { IntType{ 0 } };
97+
_data = std::make_unique<TypedData>(TypedData{ IntType{ 0 } });
9998
break;
10099

101100
case Type::Float:
102-
*_data = { FloatType{ 0.0 } };
101+
_data = std::make_unique<TypedData>(TypedData{ FloatType{ 0.0 } });
103102
break;
104103

105104
default:
@@ -158,8 +157,12 @@ Value::Value(const Value& other)
158157

159158
Value& Value::operator=(Value&& rhs) noexcept
160159
{
161-
const_cast<Type&>(_type) = rhs._type;
162-
_data = std::move(rhs._data);
160+
if (&rhs != this)
161+
{
162+
const_cast<Type&>(_type) = rhs._type;
163+
_data = std::move(rhs._data);
164+
}
165+
163166
return *this;
164167
}
165168

src/GraphQLService.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,16 @@ std::future<response::Value> Object::resolve(const SelectionSetParams & selectio
10811081
}
10821082
}
10831083
}
1084+
catch (schema_exception & scx)
1085+
{
1086+
auto messages = scx.getErrors().release<response::ListType>();
1087+
1088+
errors.reserve(errors.size() + messages.size());
1089+
for (auto& error : messages)
1090+
{
1091+
errors.emplace_back(std::move(error));
1092+
}
1093+
}
10841094
catch (const std::exception & ex)
10851095
{
10861096
std::ostringstream message;

0 commit comments

Comments
 (0)