Skip to content

Commit 0db2d3a

Browse files
committed
Unsubscribe and rethrow on resolver exception
1 parent 64ae642 commit 0db2d3a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/GraphQLService.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,13 +2110,11 @@ std::future<SubscriptionKey> Request::subscribe(std::launch launch, Subscription
21102110
{
21112111
const auto key = spThis->subscribe(std::move(paramsFuture), std::move(callbackFuture));
21122112
const auto itrOperation = spThis->_operations.find(std::string { strSubscription });
2113-
const auto itrSubscription = spThis->_subscriptions.find(key);
21142113

2115-
if (itrOperation != spThis->_operations.cend()
2116-
&& itrSubscription != spThis->_subscriptions.cend())
2114+
if (itrOperation != spThis->_operations.cend())
21172115
{
21182116
const auto& operation = itrOperation->second;
2119-
const auto& registration = itrSubscription->second;
2117+
const auto& registration = spThis->_subscriptions.at(key);
21202118
response::Value emptyFragmentDirectives(response::Type::Map);
21212119
const SelectionSetParams selectionSetParams {
21222120
ResolverContext::NotifySubscribe,
@@ -2129,7 +2127,16 @@ std::future<SubscriptionKey> Request::subscribe(std::launch launch, Subscription
21292127
launch,
21302128
};
21312129

2132-
operation->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables).get();
2130+
try
2131+
{
2132+
operation->resolve(selectionSetParams, registration->selection, registration->data->fragments, registration->data->variables).get();
2133+
}
2134+
catch (const std::exception& ex)
2135+
{
2136+
// Rethrow the exception, but don't leave it subscribed if the resolver failed.
2137+
spThis->unsubscribe(key);
2138+
throw ex;
2139+
}
21332140
}
21342141

21352142
return key;
@@ -2170,13 +2177,11 @@ std::future<void> Request::unsubscribe(std::launch launch, SubscriptionKey key)
21702177
return std::async(launch, [spThis = shared_from_this(), launch, key]()
21712178
{
21722179
const auto itrOperation = spThis->_operations.find(std::string { strSubscription });
2173-
const auto itrSubscription = spThis->_subscriptions.find(key);
21742180

2175-
if (itrOperation != spThis->_operations.cend()
2176-
&& itrSubscription != spThis->_subscriptions.cend())
2181+
if (itrOperation != spThis->_operations.cend())
21772182
{
21782183
const auto& operation = itrOperation->second;
2179-
const auto& registration = itrSubscription->second;
2184+
const auto& registration = spThis->_subscriptions.at(key);
21802185
response::Value emptyFragmentDirectives(response::Type::Map);
21812186
const SelectionSetParams selectionSetParams {
21822187
ResolverContext::NotifyUnsubscribe,

0 commit comments

Comments
 (0)