From: John Sherwood Date: Mon, 7 Dec 2015 20:05:28 +0000 (-0800) Subject: add type info to broken promise what() X-Git-Tag: deprecate-dynamic-initializer~204 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=56a2009b5016f207906f0fa80831b82769fd1f6a;p=folly.git 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 --- diff --git a/folly/futures/FutureException.h b/folly/futures/FutureException.h index 1e004dcf..b7effcd3 100644 --- a/folly/futures/FutureException.h +++ b/folly/futures/FutureException.h @@ -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 { diff --git a/folly/futures/detail/Core.h b/folly/futures/detail/Core.h index b0bb2ff7..773da5e0 100644 --- a/folly/futures/detail/Core.h +++ b/folly/futures/detail/Core.h @@ -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(exception_wrapper(BrokenPromise()))); + setResult(Try(exception_wrapper(BrokenPromise(typeid(T).name())))); } detachOne(); } diff --git a/folly/futures/test/PromiseTest.cpp b/folly/futures/test/PromiseTest.cpp index 6338d685..743095b8 100644 --- a/folly/futures/test/PromiseTest.cpp +++ b/folly/futures/test/PromiseTest.cpp @@ -126,3 +126,34 @@ TEST(Promise, isFulfilledWithFuture) { p.setValue(42); // after here EXPECT_TRUE(p.isFulfilled()); } + +TEST(Promise, brokenOnDelete) { + auto p = folly::make_unique>(); + auto f = p->getFuture(); + + EXPECT_FALSE(f.isReady()); + + p.reset(); + + EXPECT_TRUE(f.isReady()); + + auto t = f.getTry(); + + EXPECT_TRUE(t.hasException()); +} + +TEST(Promise, brokenPromiseHasTypeInfo) { + auto pInt = folly::make_unique>(); + auto fInt = pInt->getFuture(); + + auto pFloat = folly::make_unique>(); + auto fFloat = pFloat->getFuture(); + + pInt.reset(); + pFloat.reset(); + + auto whatInt = fInt.getTry().exception().what(); + auto whatFloat = fFloat.getTry().exception().what(); + + EXPECT_NE(whatInt, whatFloat); +}