Commit 56a2009b authored by John Sherwood's avatar John Sherwood Committed by facebook-github-bot-4

add type info to broken promise what()

Summary: when a promise is broken, add type info to the message. This will allow for some small improvements to debugging errors from broken promises, namely giving developers a slightly more refined target to grep for.

Reviewed By: fugalh

Differential Revision: D2725575

fb-gh-sync-id: b92edbde874e63eaeab97395da7bb52f76968800
parent df9360f4
......@@ -48,8 +48,9 @@ public:
class BrokenPromise : public FutureException {
public:
explicit BrokenPromise() :
FutureException("Broken promise") { }
explicit BrokenPromise(std::string type) :
FutureException(
(std::string("Broken promise for type name `") + type) + '`') { }
};
class NoState : public FutureException {
......
......@@ -216,7 +216,7 @@ class Core {
// detachPromise() and setResult() should never be called in parallel
// so we don't need to protect this.
if (UNLIKELY(!result_)) {
setResult(Try<T>(exception_wrapper(BrokenPromise())));
setResult(Try<T>(exception_wrapper(BrokenPromise(typeid(T).name()))));
}
detachOne();
}
......
......@@ -126,3 +126,34 @@ TEST(Promise, isFulfilledWithFuture) {
p.setValue(42); // after here
EXPECT_TRUE(p.isFulfilled());
}
TEST(Promise, brokenOnDelete) {
auto p = folly::make_unique<Promise<int>>();
auto f = p->getFuture();
EXPECT_FALSE(f.isReady());
p.reset();
EXPECT_TRUE(f.isReady());
auto t = f.getTry();
EXPECT_TRUE(t.hasException<BrokenPromise>());
}
TEST(Promise, brokenPromiseHasTypeInfo) {
auto pInt = folly::make_unique<Promise<int>>();
auto fInt = pInt->getFuture();
auto pFloat = folly::make_unique<Promise<float>>();
auto fFloat = pFloat->getFuture();
pInt.reset();
pFloat.reset();
auto whatInt = fInt.getTry().exception().what();
auto whatFloat = fFloat.getTry().exception().what();
EXPECT_NE(whatInt, whatFloat);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment